- 1. 引言
- 2. 介绍
- 3. 使用示例
- 3.1 项目集成
- 3.1.1 添加依赖
- 3.1.2 配置限流规则
- 3.2 启动 Sentinel 控制台
- 3.3 应用接入配置
- 3.3.1 引入jar
- 3.3.2 接入限流埋点
- 3.3.3 对接Sentinel 控制台
- 3.3.4 配置降级、限流规则
- 3.3.5 自定义限流处理逻辑
在上一博客《微服务轮子项目(15) -审计日志》,主要讲解了审计日志的集成使用,以及如何配置到ELK。
本文要讲解的是阿里的限流熔断框架Sentinel
。
Sentinel
是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel
把流量作为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。
Sentinel开源项目地址:https://github.com/alibaba/Sentinel
Wiki地址:https://github.com/alibaba/Sentinel/wiki
3. 使用示例 3.1 项目集成 3.1.1 添加依赖首先,修改pom.xml
文件,引入 Sentinel starter
org.springframework.cloud
spring-cloud-starter-alibaba-sentinel
3.1.2 配置限流规则
Sentinel 提供了两种配置限流规则的方式:代码配置
和 控制台配置
。
通过代码来实现限流规则的配置。一个简单的限流规则配置示例代码如下,更多限流规则配置详情请参考 Sentinel 文档。
List rules = new ArrayList();
FlowRule rule = new FlowRule();
rule.setResource(str);
// set limit qps to 10
rule.setCount(10);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setLimitApp("default");
rules.add(rule);
FlowRuleManager.loadRules(rules);
3.2 启动 Sentinel 控制台
1.首先需要获取 Sentinel 控制台,支持直接下载和源码构建两种方式。
- 直接下载:下载 Sentinel 控制台(下载截止目前为止最新版本的控制台 jar 包)
- 源码构建:进入 Sentinel Github 项目页面,将代码 git clone 到本地自行编译打包,参考此文档。
2.启动控制台,执行 Java 命令 java -jar -Dserver.port=6999 sentinel-dashboard.jar
完成Sentinel
控制台的启动。
控制台默认的监听端口为 8080,本项目修改为6999。
3.3 应用接入配置 3.3.1 引入jar修改pom.xml
文件,引入 Sentinel starter
:
org.springframework.cloud
spring-cloud-starter-alibaba-sentinel
3.3.2 接入限流埋点
- HTTP 埋点:
Sentinel starter
默认为所有的 HTTP 服务提供了限流埋点,如果只想对 HTTP 服务进行限流,那么只需要引入依赖,无需修改代码。 - 自定义埋点:如果需要对某个特定的方法进行限流或降级,可以通过
@SentinelResource
注解来完成限流的埋点,示例代码如下:
@SentinelResource("resource")
public String hello() {
return "Hello";
}
当然也可以通过原始的SphU.entry(xxx)
方法进行埋点,可以参见 Sentinel 文档。
在应用的/src/main/resources/application.properties
中添加基本配置信息:
spring.cloud.sentinel.transport.dashboard=localhost:6999
3.3.4 配置降级、限流规则
Sentinel 能通过控制台动态配置规则,不过这样配置不会持久化不建议这样做,建议搭配nacos实现动态配置并且配置持久化
Sentinel
内部提供了动态规则的扩展实现 ReadableDataSource。
Sentinel starter
整合了目前存在的几类 ReadableDataSource
。只需要在配置文件中进行相关配置,即可在 Spring
容器中自动注册 DataSource
。
比如要定义两个ReadableDataSource
,分别是 FileRefreshableDataSource
和 NacosDataSource
,配置如下:
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=degrade
spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds2.nacos.dataId=sentinel
spring.cloud.sentinel.datasource.ds2.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
ds1 和 ds2 表示ReadableDataSource的名称,可随意编写。ds1 和 ds2 后面的 file 和 nacos 表示ReadableDataSource的类型。
目前支持file, nacos, zk, apollo 这4种类型。 其中nacos,zk,apollo这3种类型的使用需要加上对应的依赖sentinel-datasource-nacos, sentinel-datasource-zookeeper, sentinel-datasource-apollo。
当ReadableDataSource加载规则数据成功的时候,控制台会打印出相应的日志信息:
[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 3 DegradeRule
[Sentinel Starter] DataSource ds2-sentinel-nacos-datasource load 2 FlowRule
3.3.5 自定义限流处理逻辑
默认限流异常处理:
- URL 限流触发后默认处理逻辑是,直接返回 “Blocked by Sentinel (flow limiting)”。 如果需要自定义处理逻辑,实现的方式如下:
public class CustomUrlBlockHandler implements UrlBlockHandler {
@Override
public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
// todo add your logic
}
}
WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler());
使用 @SentinelResource
注解下的限流异常处理:
- 如果需要自定义处理逻辑,填写
@SentinelResource
注解的blockHandler
属性(针对所有类型的BlockException
,需自行判断)或fallback
属性(针对熔断降级异常),注意对应方法的签名和位置有限制,详情见 Sentinel 注解支持文档。示例实现如下:
public class TestService {
// blockHandler 是位于 ExceptionUtil 类下的 handleException 静态方法,需符合对应的类型限制.
@SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
public void test() {
System.out.println("Test");
}
// blockHandler 是位于当前类下的 exceptionHandler 方法,需符合对应的类型限制.
@SentinelResource(value = "hello", blockHandler = "exceptionHandler")
public String hello(long s) {
return String.format("Hello at %d", s);
}
public String exceptionHandler(long s, BlockException ex) {
// Do some log here.
ex.printStackTrace();
return "Oops, error occurred at " + s;
}
}
public final class ExceptionUtil {
public static void handleException(BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
}
}