大致步骤如下
- 启动Nacos8848成功
2.编写微服务
3.启动Sentinel8080
- 启动微服务
5.启动8401微服务查看sentinel控制台
编写微服务
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 注册中心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>
application.yml
server:
port: 8005
spring:
application:
name: sentinel-service
cloud:
nacos:
discovery:
# nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
# 配置sentinel dashboard地址
dashboard: localhost:8080
# 默认8719端口,假如被占用会自动从8719开始依次+1扫描,直到找到未被占用的端口
port: 8719
controller
@RestController
public class FlowLimitController {
@GetMapping("testA")
public String testA(){
return "----------------- testA ------------------";
}
@GetMapping("testB")
public String testB(){
return "--------------------- testB -------------------";
}
}
SpringBoot项目启动入口
@SpringBootApplication
@EnableDiscoveryClient // 将微服务注册到注册中心
public class SentinelService
{
public static void main( String[] args )
{
SpringApplication.run(SentinelService.class, args);
}
}
编写完成后,必须得先访问一下接口,再去sentinel控制台中才能看到监控的微服务(因为sentinel采用的是懒加载)。