Sentinel概述
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
- 丰富的应用场景: Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。
- 完备的实时监控: Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态: Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
- 完善的 SPI 扩展点: Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。
安装
1.0 控制台部署
1.0.1 下载
地址:https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard
1.0.2 启动
java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar
2.0 微服务配置
2.0.1 引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.0.2 添加yml配置
spring:
cloud:
sentinel:
transport:
port: 8858
dashboard: localhost:8858
2.0.3 升级fastjson(V1.2.58+)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
3.0 访问控制台
用户名、密码:sentinel(默认)
4.0 配置流控规则
4.1 流控配置
超出QPS配置相应如下,异常响应内容也可自定义(见下备注)
Blocked by Sentinel(flow limiting)
4.2异常配置
4.2.1 自定义异常内容
根据不同业务返回不同的异常响应内容
添加@SentinelResource注解
@SentinelResource(value = "publicKey",blockHandler = "getRsaPublicKeyBlockHandler")
添加异常响应block方法
/**
* 限流异常处理
*
*/
public Message getRsaPublicKeyBlockHandler(BlockException e) {
return Message.fail("访问人数过多,请稍后重试!");
}
超出QPS配置相应如下,异常自定义
{
"code": 1,
"msg": "操作失败",
"obj": "访问人数过多,请稍后重试!"
}
4.2.2 统一异常处理
import cn.zhenghe.framework.core.utils.message.Message;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class MyBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
String msg = "访问异常";
if (e instanceof FlowException) {
msg = "接口限流";
}
if (e instanceof DegradeException) {
msg = "服务降级";
}
if (e instanceof ParamFlowException) {
msg = "热点参数限流";
}
if (e instanceof SystemBlockException) {
msg = "触发系统保护规则";
}
if (e instanceof AuthorityException) {
msg = "授权规则不通过";
}
//返回Json数据
httpServletResponse.setStatus(500);
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
PrintWriter writer = null;
try {
writer = httpServletResponse.getWriter();
writer.write(JSON.toJSONString(Message.fail(msg)));
writer.flush();
} catch (IOException ioException) {
} finally {
if (writer != null) {
writer.close();
}
}
}
}
5.0 配置降级规则
6.0 持久化配置
6.1 nacos持久化
6.1.1添加依赖
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.0</version>
</dependency>
6.1.2 完善yaml文件
spring:
cloud:
sentinel:
transport:
#配置 Sentinel dashboard 地址
dashboard: localhost:8858
datasource:
flow-rule: #添加nacos数据源的配置,将sentinel配置持久化进nacos里面
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}
dataId: ${spring.application.name}-flow-rule
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
6.1.3 添加配置文件
zhenghe-uaa-flow-rule
规则备注:${spring.application.name}-flow-rule
[
{
"resource": "/encrypt/publicKey",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
重点修改resource、count即可
说明:
resource:资源名称;
limitApp:来源应用;
grade:阈值类型,0表示线程数,1表示QPS;
count:单机阈值;
strategy:流控模式,0表示直接,1表示关联,2表示链路;
controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待;
clusterMode:是否集群。