Version: Next

Sentinel 初始化监控

  1. 启动 Nacos 8848
  2. 新建一个微服务 cloudalibaba-sentinel-service8401
  3. 启动 Sentinel8080
  4. 启动 8401
  5. 通过 Sentinel 控制台监控 8401 微服务

cloudalibaba-sentinel-service8401

POM

<!-- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- SpringCloud ailibaba sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

YAML

  • Sentinel 8719 端口用来与 Dashboard 的 8080 通信,将监控数据发送到 Dashboard 供开发者查看
application.yaml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentin dashboard地址
dashboard: localhost:8080
# 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口
port: 8719
# datasource:
# ds1:
# nacos:
# server-addr: localhost:8848
# dataId: cloudalibaba-sentinel-service
# groupId: DEFAULT_GROUP
# data-type: json
# rule-type: flow
management:
endpoints:
web:
exposure:
include: '*'
#feign:
# sentinel:
# enabled: true #激活Sentinel 对Feign的支持

主启动

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceMain8401.class, args);
}
}

业务类

Controller

FlowLimitController
@RestController
@Slf4j
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "----testA";
}
@GetMapping("/testB")
public String testB() {
return "----testB";
}
}

测试

步骤

  • 启动 Nacos 8848
  • 启动 Sentinel 8080 (下载好的 Jar 包)
  • 启动微服务 8401
  • 访问 localhost:8080 查看 Sentinel 控制台
问题

会发现 Sentinel 里啥也没有

  • 这是因为 Sentinel 采用懒加载机制
  • 执行一次微服务访问即可
    • 访问 http://localhost:8401/testA
    • 访问 http://localhost:8401/testB