• 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结合起来?

  • 相关阅读:
    二进制中1的个数
    原码、反码、补码,计算机中负数的表示
    win10安装MySQL
    X86、X64、X86_64
    windows搭建深度学习环境
    驱动
    cpu、gpu
    常见的文件系统
    UltralSO制作U盘启动盘
    save、load
  • 原文地址:https://www.cnblogs.com/PPBoy/p/9340531.html
Copyright © 2020-2023  润新知