ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
**1. 创建微服务模块:cloudalibaba-sentinel-service8401** **2.在当前模块的 `pom.xml`添加 alibaba-sentinel 依赖** ```xml <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--后续做持久化用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> ... </dependencies> ``` **3. 当前模块的`application.yml`** ```yml server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 #nacos服务地址 sentinel: transport: dashboard: localhost:8080 #sentinel服务地址 port: 8719 #默认8719,假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口 management: endpoints: web: exposure: include: "*" ``` **4. 启动类** ```java @SpringBootApplication @EnableDiscoveryClient public class SentinelMain8401 { public static void main(String[] args) { SpringApplication.run(SentinelMain8401.class,args); } } ``` **5. controller层** ```java @RestController public class FlowLimitController { @RequestMapping("testA") public String testA() { return "testA-------"; } @RequestMapping("testB") public String testB() { return "testB-------"; } } ``` **6. 验证** 由于Sentinel采用的是懒加载,微服务只有被访问了才能被Sentinel监控到。 访问:http://localhost:8401/testA ,可以看到Sentinel监控到的请求如下。 ![](https://img.kancloud.cn/37/8b/378bc858680d4eeae8bea561aff04599_1830x664.png)