前言
之前老顾介绍了gateway集合sentinel1.8的整体概念介绍,今天老顾就来介绍一下,gateway如何与sentinel相结合,sentinel1.8降级规则的变化,以及dashboard源码的bug。
POM依赖
我们先创建一个gateway项目,要与Sentinel结合需要依赖一下jar包。
说明一下老顾把以前文章中的案例依赖的jar包进行了升级,针对SpringCloud Alibaba的版本需要升级到2.2.3.RELEASE版本;spring boot也升级到了2.3.3.RELEASE;spring cloud升级到Hoxton.SR8
<dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>dependency> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-alibaba-sentinel-gatewayartifactId> dependency> <dependency> <groupId>com.alibaba.cspgroupId> <artifactId>sentinel-spring-cloud-gateway-adapterartifactId>dependency>
不要搞错了哦,网上有些资料有坑
配置
网关项目中增加一个配置类,我们需要添加一个SentinelGatewayFilter和SentinelGatewayBlockExceptionHandler实例
下载并安装sentinel-dashboard
之前老顾的文章已经介绍过了
1、下载地址:https://github.com/alibaba/Sentinel/releases根据自己需要下载对应版本,这里以sentinel-dashboard-1.8.0.jar为例
2、上传控制台jar包至linux服务器,启动sentinel控制台命(端口被占用请修改端口):
nohup java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar &
3、访问后台:http://localhost:8081,用户名、密码默认都为:sentinel,也可在启动命令中自行配置
网关启动参数
为网关增加JVM启动参数
-Dcsp.sentinel.dashboard.server=localhost:8081
-Dproject.name=qy-tribe-gateway
-Dcsp.sentinel.api.port=8719
-Dcsp.sentinel.app.type=1
参数说明: -Dcsp.sentinel.dashboard.server:指定控制台地址和端口。
-Dproject.name:在sentinel控制台中展示的项目名称。
-Dcsp.sentinel.api.port:指定客户端监控 API 的端口(