安装Sentinel控制台
- 下载sentinel-dashboard jar包:https://github.com/alibaba/Sentinel/releases
- 运行此jar:java -jar sentinel-dashboard-1.7.2.jar
- 访问地址:http://localhost:8080/
- 默认登录账号密码都是:sentinel
启动注册中心Nacos
http://localhost:1111/nacos/#/login
Nacos注册中心搭建详见:https://blog.csdn.net/weixin_40760239/article/details/106928295
新建Sentinel工程
- pom
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- yml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinal-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: 127.0.0.1:1111
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的支持
- 业务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author hzy
* @Date 2020/7/22 15:45
*/
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401 {
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class,args);
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author hzy
* @Date 2020/7/23 16:43
*/
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "----testA";
}
@GetMapping("/testB")
public String testB() {
return "----testB";
}
}
使用
登录Sentinel控制台发现无监控,因为Sentinel采用的是懒加载。
访问接口后:http://localhost:8401/testA、http://localhost:8401/testB
查看Sentinel控制台
Spring Cloud Alibaba Sentinel 初始化监控搭建完成
源码
https://gitee.com/Smilehzy/SpringCloud/tree/master/cloudalibaba-sentinel-service8401