zuul熔断代码

package com.sun.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

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;

@Component
public class ServiceHiFallbackProvider implements FallbackProvider {
    private final Logger logger = LoggerFactory.getLogger(FallbackProvider.class);
    
    //指定要处理的 service。
    @Override
    public String getRoute() {
        return "service-ribbon";
    }
    
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        if (cause != null && cause.getCause() != null) {
            String reason = cause.getCause().getMessage();
            logger.info("Excption {}",reason);
        }
        return fallbackResponse();
    }

    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("The service is unavailable.".getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

zuul详细讲解请看https://www.cnblogs.com/PPBoy/p/9395151.html

根据https://blog.csdn.net/forezp/article/details/81041012#commentsedit

调整zuul熔断。亲测可用,

参考文档:

https://blog.csdn.net/ityouknow/article/details/79215698

https://www.cnblogs.com/yjmyzz/p/spring-cloud-zuul-demo.html

有个问题:每个服务都需要些熔断么?那成百上千的服务,怎么弄?

而且原贴是调用ribbon负载均衡,ribbon的熔断如何和zuul结合起来?

原文地址:https://www.cnblogs.com/PPBoy/p/9340531.html