项目概述
- 需要启动nacos , 192.168.43.11:8848
- 需要启动sentinel-dashbord http://192.168.43.11:31808/#/login
- IDEA创建微服务项目,模块sentinel-service
- 启动微服务项目
- 然后去sentinel-dashbord查看情况
步骤1和步骤2都提前完成了,需要的查看其他博文
IDEA创建微服务项目
模块sentinel-service
- 导入依赖pom.xml
<!-- Nacos客户端依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
- 配置文件
server.port=8401
spring.application.name=sentinel-service
spring.cloud.nacos.discovery.server-addr=192.168.43.11:8848
# 配置Sentinel dashboard地址
spring.cloud.sentinel.transport.dashboard=192.168.43.11:31808
# 默认8719端口,键入被占用会自动从8719+1,直到找到未被占用的端口
spring.cloud.sentinel.transport.port=8719
# 暴露所有端点
management.endpoint.web.exposure.include=*
- 添加测试流量入口
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA(){
return "-----testA";
}
@GetMapping("/testB")
public String testB(){
return "-----testB";
}
}
- Application入口
@SpringBootApplication
@EnableDiscoveryClient //启用服务注册发现客户端,这样才能通过nacos获取和注册服务
public class SentinelServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceApplication.class, args);
}
}
查看结果
当以上的这些配置配置好以后,我们就可以进行测试了,那我们的测试方式就是,首先保证Nacos和Sentinel都是启动状态,然后再来启动项目,按照我们的理解这个时候,就应该在Sentinel的dashboard上能体现出它监控的服务,但是此时并没有,原因是因为Sentinel本身采用的是懒加载机制,所以我们需要首先访问服务对应的接口,Sentinel才能工作。