Sentinel
Alibaba Sentinel 官方文档地址: https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
Sentinel使用:
1. 在pom.xml文件中加入依赖,根据所依赖的Spring Cloud及Spring Cloud Alibaba版本决定sentinel使用哪个版本,以下版本对应Spring Cloud Alibaba2.1.0.RELESE版本,对应Spring Cloud Greenwich.SR2版本
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
加入以上依赖后,可通过http://localhost:port/actuator/sentinel 查看是否配置成功,若请求地址有返回信息,则说明Sentinel配置成功(若想使用actuator,须加入actuator依赖)
actuator依赖:
a
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在appcation.yml 配置
management:
endpoints:
web:
exposure:
include: '*'
请求http://localhost:port/actuator/sentinel 返回信息:
2.在配置文件中(application.yml/bootstrap.yml)中添加配置 ,以下配置为sentiel 控制台配置地址
spring:
application:
name: XXXX(服务名称)
cloud:
sentinel:
transport:
# 指定sentinel 控制台地址
dashboard: 127.0.0.1:8080
3.使用Sentinel 控制台
前往官方GitHub上下载Sentinel 控制台jar包
地址: https://github.com/alibaba/Sentinel/releases
根据自己项目中所使用的Sentinel 版本下载对应的jar包版本,在IDE中可进行查看所使用的Sentinel版本。
将下载下来的Sentinel控制台jar包通过java -jar 方式执行
启动成功后,访问 http://localhost:8080,即可打开Sentinel 控制台,账号密码默认为:sentinel/sentinel
再启动配置好Sentinel 的工程(服务),任意访问一个接口,再刷新控制台,即可查看到对应工程(服务)相应的接口,并可对接口进行限流、降级操作。,任意访问一个接口,再刷新控制台,即可查看到对应工程(服务)相应的接口,并可对接口进行限流、降级操作。
具体使用规则,可查看官方文档说明,以下总结几个使用方式
Sentinel 使用方式,具体详情可查看官方文档。
这里推荐一个大佬的手记文档:
SentinelResource注解 属性总结 :https://www.imooc.com/article/289384
Alibaba Sentinel 规则参数总结:https://www.imooc.com/article/289345
Alibaba Sentinel 配置项总结: https://www.imooc.com/article/289562
使用方式 | 使用方式 | 使用方法 |
---|---|---|
编码方式 | API | try…catch…finally |
注解方式 | SentinelResource | blockHandler/fallback |
RestTemplate | SentinelRestTemplate | blockHandler/fallback |
Feign | feign.sentinel.enabled=true | fallback/fallbackFactory |
生产环境使用Sentinel 规则持久化有以下三种方法,根据实际项目选择合适的即可。
方式一:
Alibaba Sentinel 拉模式
具体操作可参考这篇文章: https://www.imooc.com/article/289402
方式二:
Alibaba Sentinel 推模式
具体操作可参考这篇文章: https://www.imooc.com/article/289464
方式三:
Alibaba Sentinel 推出的Sentinel 在线托管Sentinel 控制台AHAS
开通地址: https://ahas.console.aliyun.com/
使用说明: https://help.aliyun.com/document_detail/110599.html?spm=a2c4g.11186623.6.597.74dc4ac5lz0dWn