前言
上一篇小白学习sentinel dashboard持久化到nacos已经部署了持久化到nacos的sentinel-dashboard
。
本篇任务:
- 测试
sentinel-dashboard
是否能将内容存放到nacos
- 测试接口是否可以按规则限流
springboot集成sentinel
- 新建
gateway-sentinel
模块,添加如下依赖
pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
- 修改
application.yml
,添加nacos
和sentinel
配置,内容如下:
yml
spring:
application:
name: gateway-sentinel
cloud:
nacos:
discovery:
server-addr: 192.168.0.102:8848
username: nacos
password: nacos
namespace:
# namespace: db224ee6-a96e-4bc9-ad5c-a673b0630386
sentinel:
# 开启对sentinel看板的饥饿式加载。sentinel默认是懒加载机制,只有访问过一次的资源才会被监控,通过关闭懒加载,在项目启动时就连接sentinel控制台
eager: true
log:
dir: logs/sentinel #日志输出地址
transport:
#跟控制台交流的端口,docker部署的话,需要将容器的8719端口映射出来
# docker run --name sentinel-server -p 8080:8080 -p 8719:8719 --restart=always --privileged=true -d sentinel-server
#如果client-ip和port不配置,则会使用docker内网ip和默认的8719端口进行注册,如果客户端与dashboard不在同一网段则无法访问
client-ip: 192.168.0.104 # 指定机器ip,否则存在虚拟机的时候,sentinel访问的可能是虚拟机的ip
port: 8719
# sentinel看板的地址
dashboard: 192.168.0.102:6005
enabled: true
web-context-unify: false
datasource:
flow:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
group-id: SENTINEL_GROUP
data-id: ${spring.application.name}-flow-rules
# 规则类型:flow、degrade、param-flow、system、authority
rule-type: flow
# 熔断降级
degrade:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
group-id: SENTINEL_GROUP
data-id: ${spring.application.name}-degrade-rules
rule-type: degrade
# 热点规则
param-flow:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
group-id: SENTINEL_GROUP
data-id: ${spring.application.name}-param-flow-rules
rule-type: param-flow
# 系统规则
system:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
group-id: SENTINEL_GROUP
data-id: ${spring.application.name}-system-rules
rule-type: system
# 授权规则
authority:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
namespace: ${spring.cloud.nacos.discovery.namespace}
group-id: SENTINEL_GROUP
data-id: ${spring.application.name}-authority-rules
rule-type: authority
management:
endpoints:
web:
exposure:
## yml文件中存在特殊字符,必须用单引号包含,否则启动报错
include: '*'
注意:
sentinel
的nacos
的配置,要和持久化sentinel-dashboard
相关配置保持一致。
- 例一:
sentinel
的nacos
的group-id
配置,要和持久化sentinel-dashboard
的SentinelNacosProperties
的groupId
保持一致,如下图: 所以上面的group-id
也为SENTINEL_GROUP
- 创建
SentinelController
,并添加测试接口/sentinel/message1
java
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/sentinel")
public class SentinelController {
@GetMapping("/message1")
public String message1() {
return "message1";
}
}
-
浏览器访问http://localhost:8080/sentinel/message1,打开
sentinel-dashboard
,进入簇点链路,可看到接口已被添加。 -
新建流控规则 对接口
/sentinel/message1
添加流控规则,设置单机阈值为2,即每秒至多发两次请求 -
测试流控规则 浏览器不断访问接口,当超过阈值时,会限流,如下图:
-
访问nacos,如果持久化成功,可在配置列表看到如下内容
count
就是单机阈值
修改count
值为3
,然后发布,打开sentinel-dashboard
,可看到单机阈值变成了3
。
至此,sentinel-dashboard的规则持久化已经实现。