Zuul2.X网关实现服务熔断降级
Posted arebirth
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zuul2.X网关实现服务熔断降级相关的知识,希望对你有一定的参考价值。
版本:
<properties> <spring-boot.version>2.1.9.RELEASE</spring-boot.version> <spring-cloud.version>Greenwich.SR4</spring-cloud.version> </properties>
所需依赖:
<properties>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件:
spring: application: name: zuul-gateway-fallback server: port: 16163 eureka: client: service-url: defaultZone: http://root:admin@eureka1:8761/eureka/,http://root:admin@eureka2:8761/eureka/
熔断降级处理类:
package cn.arebirth.fallback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.IOException; import java.io.InputStream; import java.nio.charset.Charset; @Component public class ProductProviderFallback implements FallbackProvider { private final Logger logger = LoggerFactory.getLogger(ProductProviderFallback.class); /** * getRoute方法的返回值就是要监听的挂掉的微服务的名字 * 如果需要所有服务都走这个熔断回退,则return "*" 或 return null * * @return */ @Override public String getRoute() { return "*"; } /** * 当服务无法执行的时候,返回托底信息 * * @param route * @param cause * @return */ @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { logger.info("--> route:{}进行熔断降级", route); return new ClientHttpResponse() { /** * ClientHttpResponse的fallback的状态码 * @return * @throws IOException */ @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } /** * ClientHttpResponse的fallback的状态码 * @return * @throws IOException */ @Override public int getRawStatusCode() throws IOException { return this.getStatusCode().value(); } /** * ClientHttpResponse的fallback的状态码 * @return * @throws IOException */ @Override public String getStatusText() throws IOException { return this.getStatusCode().getReasonPhrase(); } /** * Close this response, freeing any resources created. */ @Override public void close() { } /** * 设置响应体 * @return * @throws IOException */ @Override public InputStream getBody() throws IOException { String content = "商品服务不可用,请与管理员联系"; return new ByteArrayInputStream(content.getBytes()); } /** * 设置响应头信息 * @return */ @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); MediaType mt = new MediaType("application", "json", Charset.forName("utf-8")); headers.setContentType(mt); return headers; } }; } }
启动类:
package cn.arebirth; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class ZuulGatewayFallbackApp { public static void main(String[] args) { SpringApplication.run(ZuulGatewayFallbackApp.class, args); } }
这是正常请求服务的情况下:
当把服务关闭的情况下:
以上是关于Zuul2.X网关实现服务熔断降级的主要内容,如果未能解决你的问题,请参考以下文章
SpringCouldHystrix 实现服务降级和服务熔断
SpringCouldHystrix 实现服务降级和服务熔断