标签:exception common return llb 配置 enc tin exce article
依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel</artifactId>
</dependency>
配置注入:
@Bean
@LoadBalanced
// 让 RestTemplate 支持 Sentinel 限流
@SentinelRestTemplate(
blockHandler = "blockHandlerFunc",
fallback = "fallbackFunc",
blockHandlerClass = MyBlockHandlerClass.class
)
public RestTemplate restTemplate(){
return new RestTemplate();
}
异常处理类:
@Slf4j
public class MyBlockHandlerClass {
public static String blockHandlerFunc(String a, BlockException e){
log.warn("限流了",e);
return "";
}
public static String fallbackFunc(String a){
log.warn("降级了");
return "";
}
}
调用:
@Autowired
RestTemplate restTemplate;
this.restTemplate.getForObject("http://user-center/users/{id}", Users.class,article.getId());
配置开关:
resttemplate:
# 关闭 @SentinelRestTemplate 作用,开发环境可以临时关闭: 降级,限流
sentinel:
enabled: false
标签:exception common return llb 配置 enc tin exce article
原文地址:https://blog.51cto.com/3168834/2451962