zoukankan      html  css  js  c++  java
  • Zuul整合Hystrix断路器

    在Zuul工程中

    1、增加Zuul的Hystrix的配置

     并且设置超时时间为2毫秒

    2、增加业务降级处理

    **
     * 业务降级处理
     */
    @Component
    public class MyFallback  implements FallbackProvider {
    
        //针对哪一个路由进行降级, return 可以写*
        @Override
        public String getRoute() {
            return "film-service";
        }
    
        //降级时处理方式
        @Override
        public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
            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 {
                    BaseResponseVO responseVO =  BaseResponseVO.serviceException(
                            new CommonServiceException(404,"System error!~"));
                    String result = JSONObject.toJSONString(responseVO);
    
                    return new ByteArrayInputStream(result.getBytes());
                }
    
                @Override
                public HttpHeaders getHeaders() {
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.APPLICATION_JSON);
                    return headers;
                }
            };
        }
    }
    

      

    3、测试

    因为设置超时时间为2毫秒,所以肯定会触发降级

  • 相关阅读:
    THUWC 2019 第二轮 纯口胡题解
    Codeforces Round #607 (Div. 1) Solution
    Codeforces Round #606 (Div. 1) Solution
    CSP-S 2019 简要题解
    NOIP 2018 简要题解
    luogu P5605 小 A 与两位神仙
    luogu P5606 小 K 与毕业旅行
    AtCoder Grand Contest 040 简要题解
    AtCoder Grand Contest 035 简要题解
    AtCoder Grand Contest 036 简要题解
  • 原文地址:https://www.cnblogs.com/linlf03/p/12548298.html
Copyright © 2011-2022 走看看