下面我们来看一下Zuul的回退,默认情况下,经过Zuul的请求,他都会使用Hystrix进行包裹,
所以Zuul本身就有断路器的功能,我们在聊Zuul的fallback之前呢,一起来做一个实验,启动Eureka,启动用户
微服务,启动zuul
10.40.8.152:8761
访问zuul的routes
localhost:8040/routes
localhost: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/1
microservice-gateway-zuul-fallback
@Component
public class MyFallbackProvider implements ZuulFallbackProvider {
@Override
public String getRoute() {
//route 如
return "microservice-simple-provider-user";
}
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}
localhost:8040/routes
localhost:8040/microservice-simple-provider-user/simple/1
它会返回一个fallback给我
fallback: ===========>: microservice-simple-provider-user
fallback其实就是这里的
@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.
*
* The HTTP status code series can be retrieved via {@link #series()}.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @see HttpStatus.Series
* @see HTTP Status Code Registry
* @see List of HTTP status codes - Wikipedia
*/
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/routes
localhost:8040/hystrix.stream
Zuul fallback是一个微服务,这力度是不一样的,可以简单的进行一个对比,但是其实这个价值也不大,
维度是什么样子的,知道不知道又怎么样呢
4.0.0
com.learn.cloud
microservice-gateway-zuul-fallback
0.0.1-SNAPSHOT
jar
cn.learn
microcloud02
0.0.1
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.boot
spring-boot-maven-plugin
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:[email protected]: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%n
management.security.enabled=false
hystrix.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 {
@Override
public String getRoute() {
//route 如
return "microservice-simple-provider-user";
}
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());
}
@Override
public 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);
}
}