Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。
服务端控制面板
首先,下载Sentinel控制台 Releases · alibaba/Sentinel · GitHub
后台启动
nohup java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.6.jar
http://localhost:8858 #账号密码都是sentinel
客户端
SpringCloud Hoxton.SR12
SpringBoot 2.2.4.RELEASE
在项目pom文件中添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.0</version>
</dependency>
<!-- sentinel客户端与dashboard通信依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.0</version>
</dependency>
在application.yml中添加sentinel配置
spring:
application:
name: sentinel-client1
cloud:
sentinel:
transport:
post: 8719 #跟控制台交流的端口,随意指定一个未使用的端口即可
dashboard: 192.168.86.127:8858
eager: true
sentinel是懒加载,如果服务不被调用,sentinel控制台无法感知到服务
我们写了一个测试接口
@RestController("/i18n")
public class TestController {
@GetMapping(value = "/hello")
public String hello() {
System.out.println("Hello Sentinel");
return "Hello Sentinel";
}
}
调用后接口后,可以在控制面板看到我们定义的资源
设置QPS为1
访问一次,可以正常调用
1秒内快速点击,则提示被限流: