Sentinel自身就支持了多种不同的数据源来持久化规则配置,目前包括以下几种方式:
这里我们整合的配置中心Nacos
存储限流规则。
下面我们将同时使用到Nacos
和Sentinel Dashboard
,所以可以先把Nacos
和Sentinel Dashboard
启动起来。
默认配置下启动后,它们的访问地址(后续会用到)为:
<dependencies>
<!--sentinel nacos-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.2</version>
</dependency>
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</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>
</dependencies>
这里必须为bootstrap格式,不然获取不到数据
server:
port: 8005
spring:
application:
name: sentinel-nacos
cloud:
nacos:
discovery:
server-addr: 192.168.31.100:8848 #这里是我linux地址
sentinel:
transport:
#配置sentinel地址,端口
dashboard: 192.168.31.100:8859 #这里是我linux地址
#客户端IP(sentinel dashboard进行实时监控的主机ip地址)
# 默认端口8719端口假如被占用会自动从8719开始依次+1扫描,直到找到未被占用的端口
port: 8719
client-ip: 172.100.20.220 #这里是我windows地址
datasource:
flow:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
groupId: DEFAULT_GROUP
dataId: ${spring.application.name}
rule-type: flow
data-type: json
authority:
nacos:
server-addr: ${spring.cloud.nacos.config.server-addr}
dataId: ${spring.application.name}-authority-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: authority
degrade:
nacos:
server-addr: ${spring.cloud.nacos.config.server-addr}
dataId: ${spring.application.name}-degrade-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: degrade
param-flow:
nacos:
server-addr: ${spring.cloud.nacos.config.server-addr}
dataId: ${spring.application.name}-param-flow-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: param-flow
system:
nacos:
server-addr: ${spring.cloud.nacos.config.server-addr}
dataId: ${spring.application.name}-system-rules
groupId: DEFAULT_GROUP
data-type: json
rule-type: system
#暴露自己的端点
management:
endpoints:
web:
exposure:
include: '*'
这里的dataId,还有Group要和上面的bootstrap.yml的内容一样
json串
[{
"resource": "hello",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}]
参数解析
resource:资源名称;
IimitApp:来源应用;
grade:國值类型,0表示线程数,1表示QPS;
count:单机阈值
strategy:流控模式,0表示直接,1表示关联,2表示链路
controlbehavior:流控效果,0表示快速失败,1表示 Warm Up,2表示排队等待;
cluster Mode:是否集群。
@RestController
@Slf4j
public class SentinelController {
@GetMapping("/hello")
@SentinelResource(value = "hello",blockHandler = "helloBlockHandler")
public String hello(){
log.info("hello");
return "hello";
}
public String helloBlockHandler(BlockException e){
return "CustomerController invoke blockHandler";
}
}
http://127.0.0.1:8005/hello
频繁访问后:
Sentinel控制台修改规则:仅存在于服务的内存中,不会修改Nacos中配置值,重启后恢复原来的值 Nacos控制台修改规则:Nacos持久化规则,服务的内存也同步更新
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43296313/article/details/121018780
内容来源于网络,如有侵权,请联系作者删除!