Zuul2.0网关实现服务的熔断和降级

版本2.0.1

Spring Boot 和 Spring Cloud的版本

       <spring.boot.verison>2.1.6.RELEASEspring.boot.verison>
       <spring-cloud.version>Greenwich.RELEASEspring-cloud.version>

需要的引用信息

 		
        <dependency>
            <groupId>org.springframework.retrygroupId>
            <artifactId>spring-retryartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-zuulartifactId>
        dependency>

使用说明

这里是指网关转发服务ID不可用时实现的服务降级写法。

新建 MyFallbackProvider.java

package com.jwt.capability.config;

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.StandardCharsets;

@Component
public class MyFallbackProvider implements FallbackProvider {
    private final Logger logger = LoggerFactory.getLogger(MyFallbackProvider.class);

    /**
     * getRoute方法的返回值就是要监听的挂掉的微服务名字
     * api服务id,如果需要所有调用都支持回退,则return "*"或return null
     *
     * @return
     */
    @Override
    public String getRoute() {
        return "*";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        logger.info("--> route:{}进行熔断降级", route);
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return this.getStatusCode().value();
            }

            @Override
            public String getStatusText() throws IOException {
                return this.getStatusCode().getReasonPhrase();
            }

            @Override
            public void close() {

            }

            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream("服务不可用".getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                MediaType mt = new MediaType("application", "json", StandardCharsets.UTF_8);
                headers.setContentType(mt);
                return headers;
            }
        };
    }
}

配置文件(节选)

ribbon:
  MaxAutoRetries: 3
  MaxAutoRetriesNextServer: 0
  ConnectTimeout: 500
  ReadTimeout: 5000

feign:
  hystrix:
    enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000

zuul:
  host:
    connect-timeout-millis: 15000        #HTTP连接超时要比Hystrix的大
    socket-timeout-millis: 60000         #socket超时
  retryable: true

按照上述写法可以实现

你可能感兴趣的:(Java开发,大数据,生产环境,开发工具)