Spring Cloud Alibaba Sentinel,随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
丰富的应用场景: Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。
完备的实时监控: Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
广泛的开源生态: Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
完善的 SPI 扩展点: Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。
Sentinel 分为两个部分:
Sentinel 可以简单的分为 Sentinel 核心库和 Dashboard。核心库不依赖 Dashboard,但是结合 Dashboard 可以取得最好的效果。
1、文档地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
2、下载版本地址:https://github.com/alibaba/Sentinel/releases
当前最新的版本是1.8.1,我们选择1.8.0进行下载:
[root@bogon ~]# mkdir /usr/local/soft/sentinel
[root@bogon ~]# cd /usr/local/soft/sentinel
[root@bogon sentinel]# wget https://github.com/alibaba/Sentinel/releases/download/v1.8.0/sentinel-dashboard-1.8.0.jar
[root@bogon sentinel]# nohup java -Dserver.port=8971 -Dcsp.sentinel.dashboard.server=localhost:8971 -Dproject.name=sentinel -jar sentinel-dashboard-1.8.0.jar &
备注:nohup 和后边的 & 表明是后台运行 ,不至于退出命令就结束。
-Dserver.port:指定 Sentinel 控制台端口
-Dcsp.sentinel.dashboard.server:指定 Sentinel 控制台IP+端口
端口默认是8080,防止和其他的服务端口冲突,此处修改8971.
[root@bogon sentinel]# firewall-cmd --zone=public --add-port=8971/tcp --permanent
防火墙重起:
[root@bogon sentinel]# firewall-cmd --reload
浏览器输入:http://ip:8971 账号:sentinel 密码:sentinel
此时页面为空,这是因为还没有监控任何服务。由于Sentinel是懒加载,如果服务没有被访问,也看不到该服务信息。
<!-- sentinel限流、降级以及限流后的熔错依赖包start -->
<!-- 从 1.6.0 版本开始,Sentinel 提供了 Spring Cloud Gateway 的适配模块,可以提供两种资源维度的限流:
route 维度:即在 Spring 配置文件中配置的路由条目,资源名为对应的 routeId
自定义 API 维度:用户可以利用 Sentinel 提供的 API 来自定义一些 API 分组
-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- sentinel配置的数据源选用nacos -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- sentinel限流、降级以及限流后的熔错依赖包end -->
备注:上边引用了nacos作为Sentinel的数据源,使用Sentinel融合nacos,可直接在nacos中配置限流相关配置,不用在使用Sentinel控制台去操作了。
spring:
application:
name: gateway
cloud:
sentinel: transport: dashboard: localhost:8971 # sentinel控制台访问路径 8080为默认的端口,启动时可以进行设置 port: 8971 # 客户端需向控制台提供端口 eager: true # 心跳启动 datasource: # sentinel使用的数据源为nacos ds: nacos: server-addr: ip:端口 # nacos的地址 dataId: ${spring.application.name}-sentinel.json # nacos中存储规则的dataId,和nacos平台配置的时候对应 groupId: DEFAULT_GROUP # nacos中存储规则的groupId,和nacos平台配置的时候对应 rule-type: flow # 该参数是spring cloud alibaba升级到0.2.2之后增加的配置,用来定义存储的规则类型。所有的规则类型可查看枚举类:org.springframework.cloud.alibaba.sentinel.datasource.RuleType,每种规则的定义格式可以通过各枚举值中定义的规则对象来查看,比如限流规则可查看:com.alibaba.csp.sentinel.slots.block.flow.FlowRule
备注:此处只配置了spring.cloud下的sentinel,其他的如:nacos注册发现的配置、gateway的路由配置等请自行配置。
/** * sentinel 阿里的开源框架 * 网关的sentinel限流、降级以及限流后的容错配置类, * 项目中融入sentinel,可在sentinel控制平台进行限流、降级、簇点链路(所有接口的请求统计)的实时观察,以及热点链接的实时查看以及配置限制 * * @author weijb * @date 2020/11/24 11:55 * @param * @return */ @Slf4j @Configuration public class GatewaySentinelConfiguration { private final List<ViewResolver> viewResolvers; private final ServerCodecConfigurer serverCodecConfigurer; public GatewaySentinelConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) { this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); this.serverCodecConfigurer = serverCodecConfigurer; } /** * 配置 限流后异常处理 使用 SentinelGatewayBlockExceptionHandler * @return */ @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { // Register the block exception handler for Spring Cloud Gateway. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); } /** * 配置SentinelGatewayFilter * @return */ @Bean @Order(-1) public GlobalFilter sentinelGatewayFilter() { return new SentinelGatewayFilter(); } /** * 自定义异常返回 * 限流异常重新定义BlockExceptionHandler的返回 * 对应接口为 BlockRequestHandler。默认实现为 DefaultBlockRequestHandler, * 当被限流时会返回类似于下面的错误信息:Blocked by Sentinel: FlowException * @author weijb * @date 2020/11/24 13:24 * @param * @return void */ @PostConstruct public void initBlockRequestHandler(){ BlockRequestHandler blockRequestHandler = (serverWebExchange, throwable) -> ServerResponse.status(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(Result.custom(ResultCode.SYSTEM_RESOURCE_EXHAUSTION))); GatewayCallbackManager.setBlockHandler(blockRequestHandler); } }
[ { "resource": "/admin-api/admin/userStaff/list", "limitApp": "default", "grade": 1, "count": 5, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]
一条限流规则主要由下面几个因素组成,我们可以组合这些元素来实现不同的限流效果:
resource:资源名,即限流规则的作用对象
limitApp:流控针对的调用来源,若为 default 则不区分调用来源
grade:限流阈值类型(QPS 或并发线程数);0代表根据并发数量来限流,1代表根据QPS(每秒请求数量)来进行流量控制
count:限流阈值
strategy:调用关系限流策略 流控模式,0-直接,1-关联, 2-链路
controlBehavior:流量控制效果(0-快速失败,1-warm up 2-排队等待)
clusterMode:是否为集群模式
具体的配置如下图所示:
备注:具体的限流规则,可查看sentinel融合gateway的官方文档:https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6
到此,Sentinel融合gateway,并在nacos中限流的设置到此结束!
Centos7安装Sentinel详情以及和网关gateway融合并使用nacos作为数据源进行配置
原文:https://www.cnblogs.com/yanlaile/p/14600926.html