Zuul的回退
下面我們來(lái)看一下Zuul的回退,默認(rèn)情況下,經(jīng)過(guò)Zuul的請(qǐng)求,他都會(huì)使用Hystrix進(jìn)行包裹,所以Zuul本身就有斷路器的功能,我們?cè)诹腪uul的fallback之前呢,一起來(lái)做一個(gè)實(shí)驗(yàn),啟動(dòng)Eureka,啟動(dòng)用戶(hù)微服務(wù),啟動(dòng)zuul10.40.8.152:8761訪問(wèn)zuul的routeslocalhost:8040/routeslocalhost:8040/microservice-simple-provider-user/simple/1經(jīng)過(guò)zuul的請(qǐng)求都會(huì)通過(guò)Hystrix包裹,那我們是否可以訪問(wèn)hystrix.stream端點(diǎn)呢localhost:8040/hystrix.stream我們把dashboard啟一下localhost:8030/hystrix
我們發(fā)現(xiàn)Circuit closed,我們把用戶(hù)微服務(wù)停掉,然后拼命的刷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它會(huì)返回一個(gè)fallback給我fallback: ===========>: microservice-simple-provider-userfallback其實(shí)就是這里的@Override
public InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());
}首先是要返回HTTP的狀態(tài)碼,你不一定是返回OK,OK是200,你可能會(huì)返回其他的狀態(tài)碼,500,BAD_REQUEST,BAD_REQUEST.value(),HttpStatus他只一個(gè)枚舉類(lèi)型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 {構(gòu)造方法是他private HttpStatus(int value, String reasonPhrase) {this.value = value;this.reasonPhrase = reasonPhrase;
}value這個(gè)就是name,name是int值的,還有reasonPhrase,我們知道Zuul它會(huì)度Eurka里面的數(shù)據(jù),然后看他要代理哪些微服務(wù),那現(xiàn)在Eureka里面已經(jīng)沒(méi)有用戶(hù)微服務(wù)了,所以這邊會(huì)截一個(gè)404,但是你的routes里面沒(méi)有了localhost:8040/routeslocalhost:8040/hystrix.streamZuul fallback是一個(gè)微服務(wù),這力度是不一樣的,可以簡(jiǎn)單的進(jìn)行一個(gè)對(duì)比,但是其實(shí)這個(gè)價(jià)值也不大,維度是什么樣子的,知道不知道又怎么樣呢
<?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><!-- 這個(gè)插件,可以將應(yīng)用打包成一個(gè)可執(zhí)行的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);}
}
?
總結(jié)
- 上一篇: 通过Zuul上传文件,禁用Zuul的Fi
- 下一篇: 使用Sidecar支持异构平台的微服务