Zuul的回退
下面我們來看一下Zuul的回退,默認情況下,經過Zuul的請求,他都會使用Hystrix進行包裹,所以Zuul本身就有斷路器的功能,我們在聊Zuul的fallback之前呢,一起來做一個實驗,啟動Eureka,啟動用戶微服務,啟動zuul10.40.8.152:8761訪問zuul的routeslocalhost:8040/routeslocalhost:8040/microservice-simple-provider-user/simple/1經過zuul的請求都會通過Hystrix包裹,那我們是否可以訪問hystrix.stream端點呢localhost:8040/hystrix.stream我們把dashboard啟一下localhost:8030/hystrix
我們發現Circuit closed,我們把用戶微服務停掉,然后拼命的刷http://localhost:8040/microservice-simple-provider-user/simple/1microservice-gateway-zuul-fallback@Component
public class MyFallbackProvider implements ZuulFallbackProvider {@Overridepublic String getRoute() {//route 如return "microservice-simple-provider-user";}@Overridepublic ClientHttpResponse fallbackResponse() {return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {return HttpStatus.OK;}@Overridepublic int getRawStatusCode() throws IOException {return 200;}@Overridepublic String getStatusText() throws IOException {return "OK";}@Overridepublic void close() {}@Overridepublic InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());}@Overridepublic HttpHeaders getHeaders() {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);return headers;}};}
}localhost:8040/routeslocalhost:8040/microservice-simple-provider-user/simple/1它會返回一個fallback給我fallback: ===========>: microservice-simple-provider-userfallback其實就是這里的@Override
public InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());
}首先是要返回HTTP的狀態碼,你不一定是返回OK,OK是200,你可能會返回其他的狀態碼,500,BAD_REQUEST,BAD_REQUEST.value(),HttpStatus他只一個枚舉類型org.springframework.http.HttpStatus/*** Java 5 enumeration of HTTP status codes.** <p>The HTTP status code series can be retrieved via {@link #series()}.** @author Arjen Poutsma* @author Sebastien Deleuze* @see HttpStatus.Series* @see <a href="http://www.iana.org/assignments/http-status-codes">HTTP Status Code Registry</a>* @see <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">List of HTTP status codes - Wikipedia</a>*/
public enum HttpStatus {構造方法是他private HttpStatus(int value, String reasonPhrase) {this.value = value;this.reasonPhrase = reasonPhrase;
}value這個就是name,name是int值的,還有reasonPhrase,我們知道Zuul它會度Eurka里面的數據,然后看他要代理哪些微服務,那現在Eureka里面已經沒有用戶微服務了,所以這邊會截一個404,但是你的routes里面沒有了localhost:8040/routeslocalhost:8040/hystrix.streamZuul fallback是一個微服務,這力度是不一樣的,可以簡單的進行一個對比,但是其實這個價值也不大,維度是什么樣子的,知道不知道又怎么樣呢
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn.cloud</groupId><artifactId>microservice-gateway-zuul-fallback</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server.port=8040
spring.application.name=microservice-gateway-zuul-fallback
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka
eureka.instance.appname=microservice-gateway-zuul-reg-exp
#zuul.prefix=/api
#zuul.routes.user-route.stripPrefix=false
#zuul.prefix=/simple
#zuul.stripPrefix=true
logging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%nmanagement.security.enabled=falsehystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
ribbon.ConnectTimeout=3000
ribbon.ReadTimeout=6000
package com.learn.cloud.fallback;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
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;@Component
public class MyFallbackProvider implements ZuulFallbackProvider {@Overridepublic String getRoute() {//route 如return "microservice-simple-provider-user";}@Overridepublic ClientHttpResponse fallbackResponse() {return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {return HttpStatus.OK;}@Overridepublic int getRawStatusCode() throws IOException {return 200;}@Overridepublic String getStatusText() throws IOException {return "OK";}@Overridepublic void close() {}@Overridepublic InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());}@Overridepublic HttpHeaders getHeaders() {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);return headers;}};}
}
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy
@SpringBootApplication
public class ZuulFallbackApplication {public static void main(String[] args) {SpringApplication.run(ZuulFallbackApplication.class, args);}
}
?
總結
- 上一篇: 通过Zuul上传文件,禁用Zuul的Fi
- 下一篇: 使用Sidecar支持异构平台的微服务