整合 Sentinel
第一步: 在 Spring Cloud 应用的 pom.xml 中引入 Spring Cloud Alibaba 的 Sentinel 模块:
- org.springframework.boot
- spring-boot-starter-web
- org.springframework.cloud
- spring-cloud-starter-alibaba-sentinel
- org.projectlombok
- lombok
- 1.18.2
- true
- org.springframework.boot
- spring-boot-starter-test
- test
第二步: 在 Spring Cloud 应用中通过 spring.cloud.sentinel.transport.dashboard 参数配置 sentinel dashboard 的访问地址, 比如:
- spring.application.name=alibaba-sentinel-rate-limiting
- server.port=8001
- # sentinel dashboard
- spring.cloud.sentinel.transport.dashboard=localhost:8080
第三步: 创建应用主类, 并提供一个 REST 接口, 比如:
- @SpringBootApplication
- public class TestApplication {
- public static void main(String[] args) {
- SpringApplication.run(TestApplication.class, args);
- }
- @Slf4j
- @RestController
- static class TestController {
- @GetMapping("/hello")
- public String hello() {
- return "didispace.com";
- }
- }
- }
第四步: 启动应用, 然后通过 postman 或者 curl 访问几下 localhost:8001/hello 接口.
- $ curl localhost:8001/hello
- didispace.com
此时, 在上一节启动的 Sentinel Dashboard 中就可以当前我们启动的 alibaba-sentinel-rate-limiting 这个服务以及接口调用的实时监控了. 具体如下图所示:
来源: https://www.2cto.com/kf/201904/801343.html