持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情
什么是Sentinel
Sentinel(分布式系统的流量防卫兵) 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量控制、流量路由、熔断降级、系统自适应保护等多个维度来帮助用户保障微服务的稳定性。
Sentinel 具有以下特征:
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。
- 完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
Sentinel 的主要特性:
Sentinel 的开源生态:
Sentinel 分为两个部分:
- 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
- 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。
Sentinel 社区官方网站:sentinelguard.io
Sentinel Wiki文档:github.com/alibaba/Sen…
为什么使用Sentinel
Hystrix与Sentinel的对比
Hystrix 的资源模型设计上采用了命令模式,将对外部资源的调用和 fallback 逻辑封装成一个命令对象(HystrixCommand / HystrixObservableCommand)。
其隔离策略采用线程池隔离(Bulkhead Pattern)和信号量隔离。其不足之处如下:
- 线程池隔离策略,过多的线程池会非常影响性能,增加了线程切换的成本,此外,使用线程池上下文的场景也会失效。
- 信号量隔离,通过并发数来控制服务请求,但是,无法设置超时时间,容易造成服务的雪崩。
Sentinel对于资源的熔断和降级操作是通过定义拦截器,采用责任链模式实现。Sentinel 的核心骨架,将不同的 Slot 按照顺序串在一起(责任链模式),从而将不同的功能(限流、降级、系统保护)组合在一起。slot chain 其实可以分为两部分:统计数据构建部分(statistic)和判断部分(rule checking)。
Sentinel官网关于Hystrix的详细对比可以参考,Sentinel 与 Hystrix 的对比。
Sentinel的优点
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制(Service Mesh)、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
- 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
另外从开源角度来看:新版springCloud移除了旧版处理熔断业务的Hystrix ,因此功能更为强大的sentinel就成了其替代品。
下面是功能对比图 :
| 功能 | Hystrix | Sentinel |
|---|---|---|
| 隔离策略 | 线程池隔离/信号量隔离 | 信号量隔离 |
| 熔断降级策略 | 基于失败比率 | 基于响应时间或失败比率 |
| 实时指标实现 | 滑动窗口(基于 RxJava) | 滑动窗口 |
| 规则配置 | 支持多种数据源 | 支持多种数据源 |
| 扩展性 | 插件的形式 | 多个扩展点 |
| 基于注解的支持 | 支持 | 支持 |
| 限流 | 不支持 | 基于 QPS,支持基于调用关系的限流 |
| 流量整形 | 不支持 | 支持慢启动、匀速器模式 |
| 系统负载保护 | 不支持 | 支持 |
| 控制台 | 不完善 | 开箱即用,可配置规则、查看秒级监控、机器发现等 |
| 常见框架的适配 | Servlet、Spring Cloud Netflix | Servlet、Spring Cloud、Dubbo、gRPC |
Sentinel官方提供的组件功能对比图
| 功能 | Sentinel | Hystrix | resilience4j |
|---|---|---|---|
| 隔离策略 | 信号量隔离(并发控制) | 线程池隔离/信号量隔离 | 信号量隔离 |
| 熔断降级策略 | 基于慢调用比例、异常比例、异常数 | 基于异常比例 | 基于异常比例、响应时间 |
| 实时统计实现 | 滑动窗口(LeapArray) | 滑动窗口(基于 RxJava) | Ring Bit Buffer |
| 动态规则配置 | 支持近十种动态数据源 | 支持多种数据源 | 有限支持 |
| 扩展性 | 多个扩展点 | 插件的形式 | 接口的形式 |
| 基于注解的支持 | 支持 | 支持 | 支持 |
| 单机限流 | 基于 QPS,支持基于调用关系的限流 | 有限的支持 | Rate Limiter |
| 集群流控 | 支持 | 不支持 | 不支持 |
| 流量整形 | 支持预热模式与匀速排队控制效果 | 不支持 | 简单的 Rate Limiter 模式 |
| 系统自适应保护 | 支持 | 不支持 | 不支持 |
| 热点识别/防护 | 支持 | 不支持 | 不支持 |
| 多语言支持 | Java/Go/C++ | Java | Java |
| Service Mesh 支持 | 支持 Envoy/Istio | 不支持 | 不支持 |
| 控制台 | 提供开箱即用的控制台,可配置规则、实时监控、机器发现等 | 简单的监控查看 | 不提供控制台,可对接其它监控系统 |
多说一句,对于阿里开源的框架,我一直都是持有保守的态度去看。感觉开源的目的完全是为了给阿里云做广告,而且部分开源代码的核心逻辑没有一句注释;甚至个人感觉一些核心逻辑都做了代码的混淆,让人看了一头雾水。但是不合否认的是Sentinel框架还是比较优秀的,相对于Hystrix 功能更加完善;虽然在部分功能的实现代码中与Hystrix有很大区别,不知道是刻意跟Hystrix区分还是有什么别的原因。
下载Sentinel
下载完成后,会得到一个名为 sentinel-dashboard-1.8.3.jar 的jar包,只需要以命令启动这个jar包就可以直接运行Sentinel了。
启动Sentinel
在地址栏中输入cmd,打开命令窗口,执行启动命令:
输入命令 ,启动sentinel(默认为8080端口,被占用时可以通过port启动命令参数更改端口)
java -jar sentinel-dashboard-1.8.3.jar --server.port=8849
其中 --server.port=8849 用于指定 Sentinel 控制台端口为 8849。
还可以使用 -Dserver.port 参数指定端口:
# 错误的例子(启动后端口还是默认的8080,参数未生效)
java -jar sentinel-dashboard-1.8.3.jar -Dserver.port=8849
# 正确的例子
jar -jar -Dserver.port=8849 sentinel-dashboard-1.8.3.jar
注意:但是记得参数必须在jar包前面指定,否则自定义端口参数不生效(参考上面的例子)。
在GitHub的issues中有人反馈过这个bug,详情见:在使用命令行参数(-Dserver.port = 9000)启动被nacos管理的服务客户端时,发现命令行参数没有生效 #3510
启动Sentinel进程优化
可以将上面的启动命令放到 startup.cmd 文件中,这样就不用每次手动输入启动参数了。
另外,由于启动Sentinel每次都需要开启一个cmd控制台界面,我们还可以设置在后台静默启动:
-
新建startup-silence.txt文件,输入如下命令:
@echo off if "%1" == "h" goto begin mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit :begin cd "D:\DevCenter\Spring Cloud Alibaba\Sentinel" java -jar sentinel-dashboard-1.8.3.jar --server.port=8849其中,
sentinel-dashboard-1.8.3.jar为需要后台运行的程序(直接运行是一个窗口程序),D:\DevCenter\Spring Cloud Alibaba\Sentinel为sentinel-dashboard-1.8.3.jar所在路径。注意:不要添加多余的指令运行
sentinel-dashboard-1.8.3.jar,例如start cmd.exe /k "123.exe",进入对应的路径后直接运行程序即可。 -
修改startup-silence.txt为startup-silence.cmd或startup-silence.bat即可后台静默运行。
这种方式相当于创建一个windows脚本来调用windows内置接口(window.close)来关闭cmd窗口,然后再到指定路径下,最后启动可执行文件(因为使用cmd命令启动可执行文件时,启动顺序为:绘制cmd窗口 => 启动指定路径的可执行文件)。
-
如果需要结束这个后台进程,可以使用netstat找到端口对应的进程pid,然后使用taskkill命令结束进程:
C:\Users\deepinsea>netstat -ano|findstr 8849 TCP 0.0.0.0:8849 0.0.0.0:0 LISTENING 1916 TCP [::]:8849 [::]:0 LISTENING 1916 TCP [::1]:8849 [::1]:62615 ESTABLISHED 1916 TCP [::1]:8849 [::1]:62616 ESTABLISHED 1916 TCP [::1]:62615 [::1]:8849 ESTABLISHED 22852 TCP [::1]:62616 [::1]:8849 ESTABLISHED 22852 C:\Users\deepinsea>taskkill /f /t /im 1916 成功: 已终止 PID 1916 (属于 PID 34288 子进程)的进程。 # 已成功kill掉进程,因此目前没有占用的进程号了 C:\Users\deepinsea>netstat -ano|find "8849"同样,这里也可以写成结束进程脚本shutdown.cmd:
@echo off rem Copyright 2022-2030 Java白羊 Developer Holding Ltd. rem Licensed under the Apache License, Version 2.0 (the "License"); rem you may not use this file except in compliance with the License. rem You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. setlocal for /f "tokens=1" %%i in ('jps -m ^| find "8849"') do ( taskkill /F /PID %%i ) echo Done!
上面将启动命令和结束命令写成脚本的方式虽然简化了手动敲命令的麻烦,但是无法做到开机自启,并且还是需要手动点击脚本启动Sentinel。我们还可以将Sentinel进程注册为windows服务,这样就相当于启动了一个守护进程,可以解决上面的问题。
下载 Windows Service Wrapper工具
这是利用xml配置文件将cmd一键注册为windows系统服务的,像这种工具还有 nssm 。
编写nacos-service.xml配置文件
和上面的Nacos服务注册配置一样,同样需要配置Sentinel服务的配置文件,然后注册这个服务。
-
将下面的服务启动、服务配置、服务注册和服务卸载文件都统一放置在Sentinel目录下。
-
将WinSW-X64.exe(或WinSW.NET4.exe)重命名为sentinel-service.exe,复制后放在Sentinel目录下;
注意:sentinel-service.exe文件需要放置在与启动文件的同级目录,否则服务注册了不能启动成功!
-
创建配置文件sentinel-service.xml,并添加服务配置(注意: 服务名称不要为中文):
<?xml version="1.0" encoding="UTF-8" ?> <service> <!-- 唯一服务ID--> <id>sentinel</id> <!-- 显示服务的名称 --> <name>Sentinel Service</name> <!-- 服务描述 --> <description>Sentinel服务</description> <!-- 日志路径 --> <logpath>D:\DevCenter\Spring Cloud Alibaba\Sentinel\Sentinel 1.8.3\logs</logpath> <!-- 日志模式 --> <logmode>roll</logmode> <!-- 可执行文件的命令 --> <executable>D:\DevCenter\Spring Cloud Alibaba\Sentinel\Sentinel 1.8.3\startup.cmd</executable> <!-- 停止可执行文件的命令 --> <stopexecutable>D:\DevCenter\Spring Cloud Alibaba\Sentinel\Sentinel 1.8.3\shutdown.cmd</stopexecutable> </service>
下面的部分可以手动点击完成,命令行只是从原理介绍
注册和卸载Nacos服务
D:\DevCenter\Spring Cloud Alibaba\Sentinel>sentinel-service.exe install
2022-07-03 00:44:11,786 INFO - Installing service 'Sentinel Service (sentinel)'...
2022-07-03 00:44:11,831 INFO - Service 'Sentinel Service (sentinel)' was installed successfully.
D:\DevCenter\Spring Cloud Alibaba\Sentinel>sentinel-service.exe uninstall
2022-07-03 00:47:32,715 INFO - Uninstalling service 'Sentinel Service (sentinel)'...
2022-07-03 00:47:32,723 INFO - Service 'Sentinel Service (sentinel)' was uninstalled successfully.
启动和关闭服务(需要管理员模式运行cmd)
# 启动Sentinel服务
net start Sentinel
# 停止Sentinel服务
net stop Sentinel
注意:上面sentinel-service.xml中配置的id标签中的才是服务名,而name标签只是显示服务名。
查看Windows服务
按win + R打开运行,输入以下命令打开windows服务列表进行查看:
services.msc
到此,服务注册完成,并且成功启动!
下面是Sentinel启动时控制台输出:
Microsoft Windows [版本 10.0.22000.708]
(c) Microsoft Corporation。保留所有权利。
D:\DevCenter\Spring Cloud Alibaba\Sentinel>java -jar sentinel-dashboard-1.8.1.jar --server.port=8849
Error: Unable to access jarfile sentinel-dashboard-1.8.1.jar
D:\DevCenter\Spring Cloud Alibaba\Sentinel>java -jar sentinel-dashboard-1.8.3.jar --server.port=8849
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: C:\Users\deepinsea\logs\csp\
INFO: Sentinel log name use pid is: false
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )___ | '_ | '_| | '_ / _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2022-07-02 20:14:14.786 INFO 24844 --- [ main] c.a.c.s.dashboard.DashboardApplication : Starting DashboardApplication on aries with PID 24844 (D:\DevCenter\Spring Cloud Alibaba\Sentinel\sentinel-dashboard-1.8.3.jar started by deepinsea in D:\DevCenter\Spring Cloud Alibaba\Sentinel)
2022-07-02 20:14:14.795 INFO 24844 --- [ main] c.a.c.s.dashboard.DashboardApplication : No active profile set, falling back to default profiles: default
2022-07-02 20:14:14.855 INFO 24844 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2280cdac: startup date [Sat Jul 02 20:14:14 CST 2022]; root of context hierarchy
2022-07-02 20:14:17.015 INFO 24844 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8849 (http)
2022-07-02 20:14:17.059 INFO 24844 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-02 20:14:17.059 INFO 24844 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
可以看到Sentinel启动成功!
如遇到下图错误,请保证jdk安装正确,jdk环境变量配置无误,继续报错时进入到 C:\Program Files (x86)\Common Files\Oracle\Java\javapath 目录下删除此文件下的java.exe,javaw.exe,javaws.exe三个文件。
浏览器输入:localhost:8849,默认账号密码都是sentinel:
注意:只有1.6.0及以上版本,才有这个简单的登录页面。默认用户名和密码都是sentinel。对于用户登录的相关配置可以在启动命令中增加下面的参数来进行配置:
- -Dsentinel.dashboard.auth.username=sentinel : 用于指定控制台的登录用户名为 sentinel;
- -Dsentinel.dashboard.auth.password=123456 : 用于指定控制台的登录密码为 123456;如果省略这两个参数,默认用户和密码均为 sentinel
- -Dserver.servlet.session.timeout=7200 : 用于指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;
输入账户密码登录后,进入Sentinel首页,登录成功:
部署Sentinel
#下载sentinel-dashboard依赖包
https://github.com/alibaba/Sentinel/releases
#编写启动sh脚本
java -Dserver.port=8849 -Dcsp.sentinel.dashboard.server=127.0.0.1:8849 -Dproject.name=service-sentinel -jar sentinel-dashboard-1.8.3.jar
访问页面:http://127.0.0.1:8089 ,初始账户密码都为sentinel。
下面进入Sentinel服务代码层面的配置和集成:
集成Sentinel服务熔断器
1. 创建服务熔断子模块
创建服务熔断子模块 service-sentinel:
2. 添加Gateway和Sentinel依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba-starter</artifactId>
<groupId>com.deepinsea</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-sentinel</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- nacos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- gateway 服务网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 解决网关服务名负载调用503错误 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!-- sentinel 提供服务保障,并实现gateway的自动配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- sentinel-gateway 适配依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
</dependencies>
</project>
自1.6.0版本开始,Sentinel 提供了 sentinel-spring-cloud-gateway-adapter 子项目对 Gateway 进行适配,能针对路由(route)和自定义API分组两个维度进行限流。所以我们只要引入它,基本就完成了 Gateway 和 Sentinel 的整合。
注意:spring-cloud-alibaba-sentinel-gateway 中已经包含了 sentinel-spring-cloud-gateway-adapter 依赖,无需再次引人gateway适配器依赖了。
另外:一定要添加spring-cloud-loadbalancer依赖。否则网关自动路由发现和基于服务名的路由规则都会失效(当然直接url中声明真实IP,还是可以进行IP调用的),所有经过网关代理的请求都会出现 GateWay type=Service Unavailable, status=503 的异常:
C:\Users\deepinsea>curl http://localhost:9080/service-provider-nacos/provider-nacos/hello
{"timestamp":"2022-07-03T02:25:40.185+00:00","path":"/service-provider-nacos/provider-nacos/hello","status":503,"error":"Service Unavailable","requestId":"363ddda2-1"}
添加 spring-cloud-loadbalancer 依赖后:
C:\Users\deepinsea>curl http://localhost:9080/service-provider-nacos/provider-nacos/hello
hi, this is service-provider-api!
Sentinel是基于Gateway的,没有Gateway的话Sentinel是不生效的,并且Sentinel也不能监控到其他网关的流量。因此Gateway与Sentinel是一对一的,集群流量监控模式是多个Gateway-Sentinel的一对一搭配。
3. 添加熔断器主启动类
编写主启动类src/main/java/com/deepinsea/ServiceSentinelApplication.java:
package com.deepinsea;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Created by deepinsea on 2022/7/3.
* 服务熔断主启动类
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceSentinelApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSentinelApplication.class, args);
}
}
4. 创建yml配置文件
server:
# 服务运行端口
port: 9080
spring:
application:
# 服务名称
name: service-sentinel
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: localhost:8848
# Nacos认证信息
username: nacos
password: nacos
# 注册到 nacos 的指定 namespace,默认为 public
namespace: public
gateway:
enabled: true
discovery:
locator:
# 动态路由开关
enabled: true
lower-case-service-id: true
# 路由数组
# routes:
# # 我们⾃定义的路由 ID,保持唯⼀
# - id: service-provider-nacos
# # ⽬标服务地址(部署多实例)
## uri: http://localhost:9010 # 不引入loadbalancer依旧可以调用
# uri: lb://service-provider-nacos
# # 断⾔:路由条件
# predicates:
# - Path=/service-provider-nacos/provider-nacos/**
# # 过滤器
# filters:
# - StripPrefix=1
sentinel:
# sentinel开关
enabled: true
# 是否饥开启饿加载(默认为false)
eager: false # 默认情况下Sentinel会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包
transport:
dashboard: localhost:8849
# sentinel客户端-数据端口(默认为8719端口,但因为控制台默认是8719端口,为了防止冲突会使用8720端口)
# 这个端口配置会在应用对应的机器上启动一个Http Server,该Server会与Sentinel控制台做通信
port: 8721
# 指定心跳周期,默认null
# heartbeat-interval-ms: 10000
# sentinel客户端-外部端口
# 如果有多套网络,又无法正确获取本机IP,则需要使用下面的参数设置当前机器可被外部访问的IP地址,供admin控制台使用
# client-ip: 0.0.0.0:8849
注意:测试时注意端口不要冲突了,Setinel服务端和控制台占用了两个端口(8849,8719),Sentinel客户端和数据端口(与Sentinel Dashboard通信) 也会占用两个端口(9080,8720)。服务端控制台默认是8719端口,因为控制台已经使用了8719端口,因此Sentinel客户端的数据端口为了防止端口冲突就会默认使用8720端口。
上面我们设置了8721端口作为Sentinel客户端的数据端口,我们下面通过调用网关接口来启用Sentinel仪表盘,查看机器列表中本地机器的Sentinel客户端端口信息进行验证:
5. 创建服务调用Controller
这里我在sentinel服务中写个测试接口, 因为服务有经过网关的请求才能在sentinel控制台中看到服务
package com.deepinsea.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by deepinsea on 2022/7/4.
*/
@RestController
@RequestMapping("/sentinel")
public class SentinelController {
@GetMapping("/hello")
public String hello(){
return "hi, this is service-sentinel!";
}
@PostMapping("/test")
public String test(){
return "hi, this is service-sentinel[POST]!";
}
}
到此,我们可以启动项目了,然后通过网关访问服务接口 http://localhost:9080/service-sentinel/sentinel/hello 。Sentinel收到通过网关的请求后,就会将服务信息捕获并记录到仪表盘(Dashboard)中。
注意:一定要调用基于网关代理后的接口,否则Sentinel仪表盘将不会启用。因为Sentinel是基于Gateway网关的限流器(包括过滤器等功能都是依赖于网关进行实现的),因此本质上是服务端限流的方式。因此,只有通过网关的流量才能被监控到。
6. 测试网关流量监控
使用curl命令进行调用测试:
C:\Users\deepinsea>curl http://localhost:9080/service-sentinel/sentinel/hello
hi, this is service-sentinel!
可以看到,监控到经过Gateway网关代理的请求后,成功看到Sentinel记录的日志(说明Sentinel的请求过滤器开始生效了):
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: C:\Users\deepinsea\logs\csp\
INFO: Sentinel log name use pid is: false
2022-07-04 00:25:54.700 WARN 44216 --- [tor-http-nio-12] c.l.c.ServiceInstanceListSupplierBuilder : LoadBalancerCacheManager not available, returning delegate without caching.
通过网关调用后,过一会就可以在Dahboard上看到我们的调用流量监控了:
另外还有被Sentinel监控的网关机器信息:
到此,可以验证Sentinel已经成功启用了。另外,我们自定义的数据端口也生效了,如果不自定义就是8720端口(默认其实是8719端口,但和Dashboard端口冲突后会使用8720端口)。
注意:Sentinel仪表盘监控的机器失联的情况:①网关宕机了(可以注释掉网关配置来模拟);②重启网关后请求没有经过网关调用( 不访问或直接访问后端接口)。第一种情况是Sentinel启用但网关没有启用,第二种情况是网关和Sentinel都正常启用但流量没有经过网关,这两种情况可以总结为Sentinel启用后请求没有经过网关这一种情况。
另外,应用的sentinel客户端会缓存sentinel控制台的熔断规则。如果sentinel控制台宕机了,应用还会继续使用缓存的熔断规则进行熔断,可以重启应用让熔断规则失效。
Sentinel的简单集成就到这里,下面进行Sentinel用于服务保障的限流、降级以及熔断功能测试:
欢迎点赞,谢谢大佬了ヾ(◍°∇°◍)ノ゙