Spring Cloud 阿里哨兵
Spring Cloud 阿里哨兵
哨兵简介
随着微服务的普及,服务调用的稳定性变得越来越重要。Sentinel以“流量”为切入点,在流量控制、断路、负载保护等多个领域开展业务,保障业务可靠性。
哨兵具有以下特点:
- 丰富场景:Sentinel 支持阿里巴巴双十一重点场景,如秒杀(即控制突发流量,使其在系统容量可接受范围内)、消息负载转移、不可靠的下游应用的断路。
- 全面的实时监控: Sentinel 提供实时监控能力。您可以看到您的服务器的监控数据,精确到秒级,甚至可以看到少于 500 个节点的集群的整体运行状态。
- 广泛的开源生态系统: Sentinel 提供开箱即用的模块,可以轻松与其他开源框架/库集成,例如 Spring Cloud、Dubbo 和 gRPC。使用 Sentinel,只需要引入相关依赖并做一些简单的配置即可。
- Sound SPI Extensions: Sentinel 提供简单易用的声音 SPI 扩展接口。您可以使用 SPI 扩展快速自定义逻辑,例如,您可以定义自己的规则管理,或适应特定的数据源。
如何使用哨兵
如果您想在项目中使用 Sentinel,请使用组 ID 为com.alibaba.cloud
和工件 ID 为的启动器spring-cloud-starter-alibaba-sentinel
。
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
以下是如何使用 Sentinel 的简单示例:
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);}}@RestController
public class TestController {@GetMapping(value = "/hello")@SentinelResource("hello")public String hello() {return "Hello Sentinel";}}
@SentinelResource 注释用于识别资源是速率限制还是降级。在上面的示例中,注解的 ‘hello’ 属性指的是资源名称。
@SentinelResource还提供了属性,例如blockHandler
,blockHandlerClass
,和fallback
,以确定速率限制或降解操作。有关更多详细信息,请参阅 Sentinel 注释支持。
上面的例子都是在WebServlet环境中使用的。Sentinel目前支持WebFlux,需要配合spring-boot-starter-webflux
依赖在sentinel starter中触发WebFlux相关的自动化配置。
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);}}@RestController
public class TestController {@GetMapping("/mono")@SentinelResource("hello")public Mono<String> mono() {return Mono.just("simple string").transform(new SentinelReactorTransformer<>("otherResourceName"));}}
哨兵仪表板
Sentinel仪表板是一个轻量级控制台,提供机器发现、单机资源监控、集群资源数据概览、规则管理等功能。要使用这些功能,您只需完成几个步骤。
注意:集群的统计概览仅支持节点数少于 500 的集群,延迟大约为 1 到 2 秒。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NGKANDS2-1644989237558)(https://github.com/alibaba/Sentinel/wiki/image/dashboard.png)]
图 3. Sentinel 仪表板
要使用 Sentinel 仪表板,只需完成以下 3 个步骤。
获取仪表板
您可以从Release Page下载最新的仪表板 JAR 文件。
您还可以获得最新的源代码来构建自己的 Sentinel 仪表板:
- 下载 仪表板项目。
- 运行以下命令将代码打包成 FatJar:
mvn clean package
启动仪表板
Sentinel 仪表板是一个标准的 SpringBoot 应用程序,您可以在 Spring Boot 模式下运行 JAR 文件。
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
如果与 8080 端口有冲突,可以使用-Dserver.port=new port
定义一个新的端口。
配置仪表板
应用程序.yml
spring:cloud:sentinel:transport:port: 8719dashboard: localhost:8080
中指定的端口号spring.cloud.sentinel.transport.port
将在应用程序的相应服务器上启动一个 HTTP Server,该服务器将与 Sentinel 仪表板交互。例如,如果在 Sentinel 仪表板中添加了限速规则,则规则数据将被 HTTP 服务器推送和接收,然后将规则注册到 Sentinel。
有关 Sentinel 仪表板的更多信息,请参阅Sentinel 仪表板。
OpenFeign 支持
Sentinel 与OpenFeign组件兼容。要使用它,除了引入sentinel-starter
依赖外,还要完成以下2个步骤:
- 在属性文件中启用对 feign 的 Sentinel 支持。
feign.sentinel.enabled=true
- 添加
openfeign starter
依赖以触发和启用sentinel starter
:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
这是一个简单的用法FeignClient
:
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {@GetMapping(value = "/echo/{str}")String echo(@PathVariable("str") String str);
}class FeignConfiguration {@Beanpublic EchoServiceFallback echoServiceFallback() {return new EchoServiceFallback();}
}class EchoServiceFallback implements EchoService {@Overridepublic String echo(@PathVariable("str") String str) {return "echo fallback";}
}
接口中echo
方法对应的资源名称EchoService
为GET:http://service-provider/echo/{str}
.
RestTemplate 支持
Spring Cloud Alibaba Sentinel 支持使用 Sentinel 保护RestTemplate
服务调用。为此,您需要在构造bean@SentinelRestTemplate
时添加注解。RestTemplate
@Bean
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
public RestTemplate restTemplate() {return new RestTemplate();
}
@SentinelRestTemplate
注解的属性支持流控( blockHandler
, blockHandlerClass
)和断路(fallback
, fallbackClass
)。
blockHandler
orfallback
是 or 的blockHandlerClass
静态方法fallbackClass
。
方法 in 的参数和返回值与@SentinelRestTemplate
相同org.springframework.http.client.ClientHttpRequestInterceptor#interceptor
,但多了一个参数BlockException
,用于 Sentinel 捕获异常。
上面的方法签名应该handleException
是ExceptionUtil
这样的:
public class ExceptionUtil {public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {...}
}
@SentinelRestTemplate
注释的属性是可选的。
RestTemplate request block by sentinel
当您使用RestTemplate
被 Sentinel 阻止时,它将返回。你可以用你自己的逻辑覆盖它。我们提供SentinelClientHttpResponse
处理响应。
Sentinel RestTemplate 为资源速率限制提供了两种粒度:
httpmethod:schema://host:port/path
: 协议、主机、端口和路径httpmethod:schema://host:port
: 协议、主机和端口
动态数据源支持
SentinelProperties
提供datasource
属性来配置数据源。
例如配置4个数据源:
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow#spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
#spring.cloud.sentinel.datasource.ds1.file.data-type=custom
#spring.cloud.sentinel.datasource.ds1.file.converter-class=JsonFlowRuleListConverter
#spring.cloud.sentinel.datasource.ds1.file.rule-type=flowspring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinel
spring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
spring.cloud.sentinel.datasource.ds2.nacos.rule-type=degradespring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW
spring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181
spring.cloud.sentinel.datasource.ds3.zk.rule-type=authorityspring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application
spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinel
spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = test
spring.cloud.sentinel.datasource.ds4.apollo.rule-type=param-flow
该方法遵循 Spring Cloud Stream Binder 的配置。TreeMap
用于内部存储,比较器为String.CASE_INSENSITIVE_ORDER
.
d1, ds2, ds3, ds4 是 的名称ReadableDataSource
,可以任意编码。, file
, zk
,指的是具体的数据源nacos
。apollo
后面的配置分别是这些数据源的具体配置。
每个数据源都有 3 个常见的配置项data-type
:converter-class
和rule-type
。
data-type
指Converter
. Spring Cloud Alibaba Sentinel 默认提供了两个嵌入值:json
和xml
(不指定默认为 json)。如果不想使用内嵌json
或者xml
Converter
,也可以填写,custom
表示自己定义Converter
,然后配置converter-class
。您需要为此配置指定类的完整路径。
rule-type
指数据源中的规则类型( flow
,degrade
,authority
,system
, param-flow
, gw-flow
, gw-api-group
)。
默认情况下不支持 XML 格式。要使其生效,您需要添加 jackson-dataformat-xml
依赖项。
要了解有关 Sentinel 中动态数据源如何工作的更多信息,请参阅动态规则扩展。
支持zuul
参考API 网关流控
如果要在 Zuul 中使用 Sentinel Starter,需要添加spring-cloud-alibaba-sentinel-gateway
依赖,并且需要添加spring-cloud-starter-netflix-zuul
依赖才能让网关模块中的 Zuul AutoConfiguration 类生效:
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
支持 Spring Cloud 网关
参考API 网关流控
如果要在 Spring Cloud Gateway 中使用 Sentinel Starter,需要添加spring-cloud-alibaba-sentinel-gateway
依赖,添加spring-cloud-starter-gateway
依赖才能让模块中的 Spring Cloud Gateway AutoConfiguration 类生效:
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
断路器:带有 Sentinel 和配置 Sentinel 断路器的 Spring Cloud 断路器
默认配置
要为所有断路器提供默认配置,请创建一个Customizer
传递 SentinelCircuitBreakerFactory
或ReactiveSentinelCircuitBreakerFactory
. 该configureDefault
方法可用于提供默认配置。
@Bean
public Customizer<SentinelCircuitBreakerFactory> defaultCustomizer() {return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id).build());
}
您可以选择通过 提供默认熔断规则SentinelConfigBuilder#rules(rules)
。您还可以选择稍后使用DegradeRuleManager.loadRules(rules)
Sentinel 的 API 或通过 Sentinel 仪表板在其他地方加载断路规则 。
反应式示例
@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> defaultCustomizer() {return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id).build());
}
特定的断路器配置
与提供默认配置类似,您可以创建一个Customizer
传递给 SentinelCircuitBreakerFactory
.
@Bean
public Customizer<SentinelCircuitBreakerFactory> slowCustomizer() {String slowId = "slow";List<DegradeRule> rules = Collections.singletonList(new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100).setTimeWindow(10));return factory -> factory.configure(builder -> builder.rules(rules), slowId);
}
反应式示例
@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> customizer() {List<DegradeRule> rules = Collections.singletonList(new DegradeRule().setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100).setTimeWindow(10));return factory -> factory.configure(builder -> builder.rules(rules), "foo", "bar");
}
哨兵端点
Sentinel 在内部提供一个 Endpoint,其对应的端点 id 为sentinel
.
端点暴露的 json 包含多个属性:
- appName:应用程序名称
- logDir:日志的目录
- logUsePid:没有 pid 的日志名称
- blockPage:标记块后重定向页面
- metricsFileSize:指标文件的大小
- metricsFileCharset:指标文件字符集
- totalMetricsFileCount:metrics文件的总文件数
- consoleServer:哨兵仪表板地址
- 客户端IP:客户端IP
- heartbeatIntervalMs:带有仪表板的客户端心跳间隔
- clientPort:客户端需要暴露端口才能与仪表板交互
- 冷因子:冷因子
- filter:CommonFilter 相关属性,例如 order、urlPatterns 和 enable
- 数据源:客户端的数据源配置信息
- rules:客户端内部生效的规则包含flowRules、degradeRules、systemRules、authorityRule、paramFlowRule
以下显示了服务实例如何访问 Endpoint:
{"blockPage": null,"appName": "sentinel-example","consoleServer": "localhost:8080","coldFactor": "3","rules": {"flowRules": [{"resource": "GET:http://www.taobao.com","limitApp": "default","grade": 1,"count": 0.0,"strategy": 0,"refResource": null,"controlBehavior": 0,"warmUpPeriodSec": 10,"maxQueueingTimeMs": 500,"clusterMode": false,"clusterConfig": null}, {"resource": "/test","limitApp": "default","grade": 1,"count": 0.0,"strategy": 0,"refResource": null,"controlBehavior": 0,"warmUpPeriodSec": 10,"maxQueueingTimeMs": 500,"clusterMode": false,"clusterConfig": null}, {"resource": "/hello","limitApp": "default","grade": 1,"count": 1.0,"strategy": 0,"refResource": null,"controlBehavior": 0,"warmUpPeriodSec": 10,"maxQueueingTimeMs": 500,"clusterMode": false,"clusterConfig": null}]},"metricsFileCharset": "UTF-8","filter": {"order": -2147483648,"urlPatterns": ["/*"],"enabled": true},"totalMetricsFileCount": 6,"datasource": {"ds1": {"file": {"dataType": "json","ruleType": "FLOW","converterClass": null,"file": "...","charset": "utf-8","recommendRefreshMs": 3000,"bufSize": 1048576},"nacos": null,"zk": null,"apollo": null,"redis": null}},"clientIp": "30.5.121.91","clientPort": "8719","logUsePid": false,"metricsFileSize": 52428800,"logDir": "...","heartbeatIntervalMs": 10000
}
配置
下表显示,当 中存在对应的 bean 类型时ApplicationContext
,会采取一些动作:
现有 Bean 类型 | 行动 | 功能 |
---|---|---|
UrlCleaner
|
WebCallbackManager.setUrlCleaner(urlCleaner)
|
资源清理(resource(例如,将/foo/:id的所有URL归类到/foo/*资源)) |
UrlBlockHandler
|
WebCallbackManager.setUrlBlockHandler(urlBlockHandler)
|
自定义限速逻辑 |
RequestOriginParser
|
WebCallbackManager.setRequestOriginParser(requestOriginParser)
|
设置原点 |
Spring Cloud Alibaba Sentinel 的所有配置如下表所示:
配置 | 描述 | 默认值 |
---|---|---|
spring.application.name 或者 project.name
|
哨兵项目名称 | |
spring.cloud.sentinel.enabled
|
Sentinel 自动配置是否生效 | 真的 |
spring.cloud.sentinel.eager
|
是否提前触发 Sentinel 初始化 | 错误的 |
spring.cloud.sentinel.transport.port
|
应用程序与 Sentinel 仪表板交互的端口。使用此端口的 HTTP 服务器将在应用程序中启动 | 8719 |
spring.cloud.sentinel.transport.dashboard
|
Sentinel 仪表板地址 | |
spring.cloud.sentinel.transport.heartbeatIntervalMs
|
应用程序和 Sentinel 仪表板之间的检测信号间隔 | |
spring.cloud.sentinel.transport.client-ip
|
此配置的客户端 IP 将注册到 Sentinel Server 端。 | |
spring.cloud.sentinel.filter.order
|
Servlet过滤器的加载顺序。过滤器将在 Starter 中构建 | 整数.MIN_VALUE |
spring.cloud.sentinel.filter.url-patterns
|
数据类型为数组。指 Servlet Filter ULR 模式的集合 | /* |
spring.cloud.sentinel.filter.enabled
|
启用实例 CommonFilter | 真的 |
spring.cloud.sentinel.metric.charset
|
度量文件字符集 | UTF-8 |
spring.cloud.sentinel.metric.fileSingleSize
|
Sentinel metric 单个文件大小 | |
spring.cloud.sentinel.metric.fileTotalCount
|
哨兵指标总文件数 | |
spring.cloud.sentinel.log.dir
|
Sentinel 日志文件目录 | |
spring.cloud.sentinel.log.switch-pid
|
如果 Sentinel 日志文件名需要 PID | 错误的 |
spring.cloud.sentinel.servlet.blockPage
|
自定义重定向 URL。当速率受限时,请求将被重定向到预定义的 URL | |
spring.cloud.sentinel.flow.coldFactor
|
冷因子 | 3 |
spring.cloud.sentinel.zuul.order.pre
|
SentinelZuulPreFilter 的顺序 | 10000 |
spring.cloud.sentinel.zuul.order.post
|
SentinelZuulPostFilter 的顺序 | 1000 |
spring.cloud.sentinel.zuul.order.error
|
SentinelZuulErrorFilter 的顺序 | -1 |
spring.cloud.sentinel.scg.fallback.mode
|
Spring Cloud Gateway 断路后的响应方式(选择redirect 或response )
|
|
spring.cloud.sentinel.scg.fallback.redirect
|
Spring Cloud Gateway 响应方式为 ‘redirect’ 方式对应的重定向 URL | |
spring.cloud.sentinel.scg.fallback.response-body
|
Spring Cloud Gateway 响应模式为响应模式对应的响应内容 | |
spring.cloud.sentinel.scg.fallback.response-status
|
Spring Cloud Gateway 响应模式为’response’模式对应的响应码 | 429 |
spring.cloud.sentinel.scg.fallback.content-type
|
Spring Cloud Gateway 响应模式是与“响应”模式对应的内容类型。 | 应用程序/json |
Spring Cloud 阿里哨兵相关推荐
- Spring Cloud Alibaba 参考文档-2021.0.4.0
Spring Cloud Alibaba 简介 依赖管理 Spring Cloud Alibaba Nacos Discovery 3.1 服务注册/发现:Nacos Discovery 3.2 如何 ...
- Java 微服务框架选型(Dubbo 和 Spring Cloud?),大厂 HR 如何面试
写在最前面,我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家.扫码加微信好友进[程序员面试学习交流群],免费领取.也欢迎各位一起在群里探讨技术. 微服 ...
- Java 微服务框架选型(Dubbo 和 Spring Cloud?)
|来源:博客园 |链接:https://www.cnblogs.com/xishuai/archive/2018/04/13/dubbo-and-spring-cloud.html 微服务(Micro ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...
- Google K8S与阿里Spring Cloud Alibaba相爱相杀,下一个神级架构来了!
都2021年了 还没用过微服务 吗?如日中天的K8S还只是听说过?云原生架构知道是怎么回事吗? 2020年的双十一,天猫订单处理峰值达到54.3万笔/秒,2020年的双十二,数据量高达1200万+.2 ...
- 阿里新框架发布!干掉Spring Cloud,换下Dubbo,微服务王者来了!
目前,但凡谈及微服务技术选型,就必然会遇到一个两难的抉择,到底该采用Dubbo,还是该选择Spring Cloud呢? 当初阿里于2017年宣布重新开源Dubbo,近年来Dubbo发展速度和势头可谓是 ...
- 使用阿里云ACM简化你的Spring Cloud微服务环境配置管理
摘要: 本文我们就如何使用阿里云ACM这样的配置管理产品在Spring Cloud中替代Spring Cloud Config帮助简化环境配置管理做一个简单的示例,帮助你理解基于ACM来简化微服务环境 ...
- 使用阿里云ACM简化你的Spring Cloud微服务环境配置管理 1
摘要: 本文我们就如何使用阿里云ACM这样的配置管理产品在Spring Cloud中替代Spring Cloud Config帮助简化环境配置管理做一个简单的示例,帮助你理解基于ACM来简化微服务环境 ...
- spring cloud全家桶_阿里架构师玩转spring全家桶(实战篇),附赠3本spring电子书...
Spring框架自诞生以来一直备受开发者青睐,今天在这里分享的是Spring全家桶实战篇电子书籍.书籍内容中包括了Spring.SpringBoot.SpringCloud.SpringMVC四个实战 ...
最新文章
- Google公开其JavaScript工具:Closure Compiler
- 常用数据库连接和diriver以及默认端口
- python整数逆序输出_利用Python实现倒序任意整数
- erlang安装_消息中间件(八)- RabbitMQ - 安装
- 搭建小程序表情包教程
- webhooks php,GitHub和WebHooks自动部署PHP项目
- Android实现保存图片和视频到系统相册
- Ps雅点设计合成大师
- 【专题5: 硬件设计】 之 【70.开关电源 之 电感计算例子】
- 修改密码 的测试用例(web)
- gba模拟器ios_不越狱iOS设备安装GBA模拟器 GBA4iOS 方法
- Datadog:APM和分布式跟踪的新Java支持
- 读提交和可重复读区别
- UDP协议、广播、组播和多路复用(网络编程二)
- JAVA看云判断天气_怎样看云判断天气变化?
- css魔法 真名,CSS魔法(二)
- Diffraction、Scatterring、Diffusion、Reflection 衍射、散射、干涉、折射 傻傻分不清
- link和@import之间的区别
- vue基于el-timeline组件实现动态表格时间线
- Java开发常见面试题详解(LockSupport,AQS,Spring循环依赖,Redis)_3
热门文章
- uni-app - 最详细 H5 网页接入微信登录功能,提供公众号配置与详细注释示例代码(移动端网页实现点击登录按钮后 调用微信公众号授权登录功能 详细讲解接入流程与详细示例代码)官方最新超级详细教程
- plt.savefig()保存图片打开却神魔都没有
- 2023-05-20:go语言的slice和rust语言的Vec的扩容流程是什么?
- python手写答题卡识别_基于 Python OpenCV 的简易答题卡识别
- 2021年国内应用商店App减少21%,合规分发已成必然
- 7-6 日K蜡烛图 (15 分)
- 三角函数积化和差、和差化积公式(含口诀)
- 为什么65535是极值
- 未转变者服务器买车指令,未转变者服务器指令大全
- 洛谷P1797 克鲁斯的加减法(纯暴力)