Sentinel部署和使用教程
1、部署:通过Docker镜像 或者 安装包解压到Linux系统的方式
下载docker镜像可参考:https://www.cnblogs.com/xd99/p/18386275
docker run -d --name sentinel-dashboard \
--restart unless-stopped \
-p 8080:8080 \
registry.cn-hangzhou.aliyuncs.com/zxd-docker-images/sentinel-dashboard:1.8.6
2、接入项目并持久化
2.1 引入主依赖和持久化依赖
<!--主依赖-->
<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>
<version>1.8.6</version>
</dependency>
2.2 修改nacos中的yml文件
注:需要手动修改其中的ip地址和账号密码
.yml
spring:
cloud:
#sentinel控制台
sentinel:
# sentinel开关
enabled: true
# 是否饥开启饿加载(默认为false) 默认情况下Sentinel会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包
eager: true
transport:
dashboard: 《你的Sentinel部署ip地址》:8082
client-username: 《Sentinel账号》
client-password: 《Sentinel密码》
datasource:
#限流持久配置
flow:
nacos:
server-addr: 《你的Nacos部署ip地址》:8848
username: 《Nacos账号》
password: 《Nacos密码》
dataId: sentinel-flow
groupId: DEFAULT_GROUP
rule-type: flow
data-type: json
# 降级熔断规则配置
degrade:
nacos:
server-addr: 《你的Nacos部署ip地址》:8848
username: 《Nacos账号》
password: 《Nacos密码》
dataId: sentinel-degrade
groupId: DEFAULT_GROUP
data-type: json
rule-type: degrade
# 热点参数限流规则配置
param-flow:
nacos:
server-addr: 《你的Nacos部署ip地址》:8848
username: 《Nacos账号》
password: 《Nacos密码》
dataId: sentinel-param-flow
groupId: DEFAULT_GROUP
data-type: json
rule-type: param-flow
# 系统规则配置
system:
nacos:
server-addr: 《你的Nacos部署ip地址》:8848
username: 《Nacos账号》
password: 《Nacos密码》
dataId: sentinel-system
groupId: DEFAULT_GROUP
data-type: json
rule-type: system
2.3 配置几个基础的json文件
2.3.1 限流规则
sentinel-flow
[
{
"resource": "hello",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
2.3.2 熔断规则
sentinel-degrade
[
{
"count": 1,
"enable": false,
"grade": 0,
"limitApp": "default",
"minRequestAmount": 1,
"resource": "hello",
"slowRatioThreshold": 0.5,
"statIntervalMs": 1000,
"strategy": 0,
"timeWindow": 10
}
]
2.3.3 热点限流规则
sentinel-param-flow
[
{
"resource": "service-hotspot",
"paramIdx": 0,
"count": 100,
"grade": 1,
"durationInSec": 60,
"controlBehavior": 0,
"paramFlowItemList": [
{
"object": "specific_value",
"count": 50
}
]
}
]
2.3.4 系统规则
sentinel-system
[
{
"grade": 3,
"count": 0.8
}
]
持久化参考链接:https://blog.csdn.net/aa35434/article/details/140739516
重启项目进入Sentinel就可以看到上面json定义的规则了
3、自定义异常
SentinelExceptionHandler
package org.auth.exception;
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 org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class SentinelExceptionHandler {
/**
* 限流全局异常
*/
@ExceptionHandler(FlowException.class)
public Map handlerFlowException(){
return new HashMap(){{
put("code", HttpStatus.TOO_MANY_REQUESTS.value());
put("msg", "被限流,请稍后重试");
}};
}
/**
* 熔断全局异常
*/
@ExceptionHandler(DegradeException.class)
public Map handlerDegradeException(){
return new HashMap(){{
put("code", HttpStatus.TOO_MANY_REQUESTS.value());
put("msg", "被熔断,请稍后重试");
}};
}
/**
* 热点限流异常
*/
@ExceptionHandler(ParamFlowException.class)
public Map handlerparamFlowException(){
return new HashMap(){{
put("code", HttpStatus.TOO_MANY_REQUESTS.value());
put("msg", "热点限流,请稍后重试");
}};
}
/**
* Sentinel 权限拦截全局异常
*/
@ExceptionHandler(AuthorityException.class)
@ResponseBody
public Map handlerAuthorityException(){
return new HashMap(){{
put("code", HttpStatus.UNAUTHORIZED.value());
put("msg", "暂无权限");
}};
}
}
参考链接:https://www.cnblogs.com/vipstone/p/17848233.html