码迷,mamicode.com
首页 > 其他好文 > 详细

Zuul整合Hystrix断路器

时间:2020-03-22 21:17:01      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:bytearray   exception   ase   src   image   over   content   ToJson   head   

在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毫秒,所以肯定会触发降级

技术图片

 

Zuul整合Hystrix断路器

标签:bytearray   exception   ase   src   image   over   content   ToJson   head   

原文地址:https://www.cnblogs.com/linlf03/p/12548298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!