zoukankan      html  css  js  c++  java
  • 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结合起来?

  • 相关阅读:
    批量ping工具fping
    图形文件元数据管理工具exiv2
    JPG图片EXIF信息提取工具exif
    网络图片嗅探工具driftnet
    复杂密码生成工具apg
    前端面试题目准备
    JS中同步与异步的理解
    angular初体验
    媒体查询的两种方式
    CSS3多列布局
  • 原文地址:https://www.cnblogs.com/PPBoy/p/9340531.html
Copyright © 2011-2022 走看看