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毫秒,所以肯定會觸發降級

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM