Sentinel 的工作模式
1、原始模式(非生产环境推荐模式):不持久化,重启微服务,配置的限流规则就会丢失
2、Pull模式(非生产环境推荐模式):(拉模式),将规则缓存到本地文件
3、push模式(生产环境推荐模式):将规则在云端配置中心,然后将云端配置推送至sentinel dashboard,在dashboard配置的规则也可以知道同步到云端配置中心
下面重点介绍第三种模式(push模式)
1.pom.xml添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<!--引入父依赖 -->
<dependencyManagement>
<dependencies>
<!--spring-cloud-dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--alibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--springcloud 子项目nacos配置中心依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--sentinel依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--sentinel 持久化nacos依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2.在application.yml文件中添加配置内容,本人使用的是yml文件格式,也可以使用properties格式
spring:
cloud:
sentinel:
transport:
dashboard: ip:端口 #链接sentinel dashboard
datasource: #dashbord的数据由下来库中获取
flow: #此名称可以随意取,该值得含义是key,不过最好见名知意,如限流用flow、降级用degrade等
nacos:
serverAddr: 127.0.0.1:80
dataId: ${spring.application.name}.json
groupId: DEFAULT_GROUP
ruleType: flow
dataType: json
3.在nacos中添加规则
4.创建测试类
@RestControllerpublicclass Test {
@GetMapping("/test")
public String test() {
return"test sentinel";
}
}
访问几次接口之后,就可以在Sentinel Dashboard 中看到在nacos中配置的规则信息了,并且项目服务重启依然存在。
以上的代码是经过本人运行过的,希望对看到的您有所帮助!