title: “Spring Cloud學習筆記6-Zuul集成Turbine”
url: “https://wsk1103.github.io/”
tags:
- Spring Cloud
- 學習筆記
備註:
官網:http://spring.io/projects/spring-cloud
總綱:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html
JAVA: 1.8 +
MAVEN: 3.5.0 +
Spring Boot:2.0.7.RELEASE
Spring Cloud:Finchley
說明:When a circuit for a given route in Zuul is tripped you can provide a fallback response by creating a bean of type ZuulFallbackProvider. Within this bean you need to specify the route ID the fallback is for and provide a ClientHttpResponse to return as a fallback. Here is a very simple ZuulFallbackProvider implementation.
If you would like to choose the response based on the cause of the failure use FallbackProvider which will replace ZuulFallbackProvder in future versions.
當Zuul中給定路徑的電路跳閘時,您可以通過創建ZuulFallbackProvider類型的bean來提供回退響應。在此bean中,您需要指定回退所針對的路由ID,並提供ClientHttpResponse作爲回退返回。這是一個非常簡單的ZuulFallbackProvider實現。
如果您想根據失敗原因選擇響應,請使用FallbackProvider,它將取代未來版本中的ZuulFallbackProvder。(谷歌翻譯)
本項目地址:https://github.com/wsk1103/my-spring-cloud
熔斷zuul
在使用zuul的過程中,有可能zuul因爲某些事故導致服務不可用,此時就可以添加hystrix進行熔斷。
zuul已經提供了熔斷的功能,只需要實現FallbackProvider接口便可。
1. 沿用zuul項目,實現FallbackProvider
新增包provider -> 新增類
package com.wsk.zuul.provider;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
/**
* @author WuShukai
* @version V1.0
* @description
* @date 2018/12/11 14:26
*/
@Component
@Slf4j
public class MyProvider implements FallbackProvider {
@Override
public String getRoute() {
return "*";
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
log.warn(String.format("route:%s,exceptionType:%s,stackTrace:%s", route, cause.getClass().getName(), Arrays.toString(cause.getStackTrace())));
if (cause instanceof HystrixTimeoutException) {
return response(HttpStatus.GATEWAY_TIMEOUT);
} else {
return response(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
private ClientHttpResponse response(final HttpStatus status) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() {
return status;
}
@Override
public int getRawStatusCode() {
return status.value();
}
@Override
public String getStatusText() {
return status.getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() {
String msg = String.format("{\"code\": 1103,\"message\": \"%s\"}", status.value());
return new ByteArrayInputStream(msg.getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}
FallbackProvider 說明
項 | 說明 |
---|---|
getRoute() | 該Provider應用的Route ID,例如:testservice,如果設置爲 * ,那就對所有路由生效 |
fallbackResponse(String route, Throwable cause) | 快速回退失敗/響應,即處理異常並返回對應輸出/響應內容。route:發生異常的RouteID,cause:觸發快速回退/失敗的異常/錯誤 |
ClientHttpResponse | Spring提供的HttpResponse接口。可以通過實現該接口自定義Http status、body、header |
集成Turbine
1. 在Turbine項目的application.yml中添加server-zuul
turbine:
# app-config: service-hi,service-hi2
#指定需要監控的servicename,多個service以,間隔
app-config: service-feign,service-zuul
#指定集羣名稱,默認爲default,當設立了多個集羣時,可以在Hystrix指定集羣名稱來查看監控
clusterNameExpression: new String("default")
#合併同一個host多個端口的數據
combine-host-port: true
2. 爲zuul項目添加包 configuration -> 類 HystrixConfiguration
其實和feign裏面的HystrixConfiguration是一樣的。
package com.wsk.zuul.configuration;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author WuShukai
* @version V1.0
* @description
* @date 2018/12/11 11:48
*/
@Configuration
public class HystrixConfiguration {
@Bean(name = "hystrixRegistrationBean")
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/hystrix.stream");
registration.setName("hystrixServlet");
registration.setLoadOnStartup(1);
return registration;
}
@Bean(name = "hystrixForTurbineRegistrationBean")
public ServletRegistrationBean servletTurbineRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
registration.setName("hystrixForTurbineServlet");
registration.setLoadOnStartup(1);
return registration;
}
}
3. 依次啓動server,client,feign,zuul,turbine這5個項目
訪問:http://localhost:8764/hystrix
,然後是輸入框輸入:http://localhost:8769/actuator/hystrix.stream
,來監控zuul項目。
然後訪問:http://localhost:8769/a/hi?name=sky&token=www
,和:http://localhost:8765/hi?name=wsk