实例化RestTemplate并加入@SentinelRestTemplate注解@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
@SentinelRestTemplate(
fallbackClass = ExceptionUtil.class,fallback = "fallBack",
blockHandlerClass = ExceptionUtil.class, blockHandler = "handleBlock")
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static class ExceptionUtil {
public static ClientHttpResponse handleBlock(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){
RestResponse<Void> commonResult = RestResponse.error("500","降级处理函数 block 。。。。。");
return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));
}
public static ClientHttpResponse fallBack(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){
RestResponse<Void> commonResult = RestResponse.error("500","异常处理函数 fallback 。。。。。");
return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25