SpringCloudAlibaba - RestTemplate 整合 Sentinel
前言
记录下RestTemplate整合Sentinel的方式
Sentinel的整合查看系列文章
环境
Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE + Sentinel 1.8.1
具体实现
- 实现内容中心使用
RestTemplate调用用户中心接口限流
内容中心
- 使用
@SentinelRestTemplate注解为RestTemplate整合Sentinel
@Bean
@LoadBalanced
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}
TestController.java
@RestController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {
private final RestTemplate restTemplate;
/**
* 为RestTemplate 整合Sentinel
* @return
*/
@GetMapping("test5")
public String test5() {
return restTemplate.getForObject(
"http://user-center/test/{name}",
String.class,
"Coisini"
);
}
}
用户中心
TestController.java
@RestController
@Slf4j
public class TestController {
@GetMapping("/test/{name}")
public String test(@PathVariable String name) {
log.info("请求...");
return "hello " + name;
}
}
测试
- 接口调用

- 添加一条
QPS为1的流控规则

- 频繁访问接口被限流

关闭@SentinelRestTemplate注解
resttemplate:
sentinel:
enabled: false

浙公网安备 33010602011771号