Sentinel整合GataWay
一,Sentinel 控制台
1.下载sentinel jar包,下载地址:https://github.com/alibaba/Sentinel/releases/tag/1.8.8

2.启动控制台,执行命令【java -jar jar包名称】

3.登录Sentinel 控制台,默认地址http://127.0.0.1:8080/,默认登录账号密码都是 sentinel

二、整合GataWay项目
1.导入sentinel相关pom文件
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
2.配置application.yml
server:
port: 8019
spring:
redis:
host: 127.0.0.1
port: 6379
password: 123456
timeout: 6000ms # 连接超时时长(毫秒)
application:
name: gatewaytest
###consul注册中心地址
cloud:
sentinel:
eager: true
transport:
dashboard: localhost:8080 # Sentinel Dashboard地址
nacos:
discovery:
service-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true # gateway可以通过开启以下配置来打开根据服务的serviceId来匹配路由,默认是大写
routes: # 网关路由配置
- id: order-payment1 # 路由id,自定义,只要唯一即可
#uri: http://127.0.0.1:8016 # 路由的目标地址 http就是固定地址
uri: lb://order-payment # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/addOrder2 # 这个是按照路径匹配
3.配置项目启动参数并启动项目
-Dcsp.sentinel.dashboard.server=localhost:8080
-Dproject.name=sentinel-dashboard2
-Dsentinel.dashboard.auth.username=sentinel
-Dsentinel.dashboard.auth.password=sentinel

启动项目后刷新sentinel控制台,或者退出重新登录,就可以看到服务已经注册到sentinel控制台上了

请求接口http://127.0.0.1:8019/addOrder2,然后查看控制台-请求链路,然后根据请求的信息设置流控参数

4.使用jmter进行流控测试
设置执行线程数为4

执行jmter,查看运行结果

执行结果为3个成功,1个失败请求,返回状态码为429,被限流
1399

被折叠的 条评论
为什么被折叠?



