在Zuul工程中

1、增加Zuul的Hystrix的配置

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断路器

 

相关文章:

  • 2021-10-11
  • 2021-07-15
  • 2021-06-13
  • 2021-07-30
  • 2021-04-04
猜你喜欢
  • 2021-09-09
  • 2021-06-23
  • 2022-12-23
  • 2021-12-10
  • 2021-06-10
  • 2021-12-15
  • 2021-08-20
相关资源
相似解决方案