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::8761/eureka/,http://root::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网关实现服务熔断降级

当把服务关闭的情况下:

Zuul2.X网关实现服务熔断降级

相关推荐