Sentinel 控制台 使用
- 一、引入依赖
- 二、配置sentinel监控服务
- 三 、下载jar
- 四、 流控/限流 统一响应返回异常@Configuration
一、引入依赖
官网
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
二、配置sentinel监控服务
application.properties 配置:
spring.cloud.sentinel.transport.dashboard=localhost:8333 //-sentinel控制台地址 只写端口是不能的
spring.cloud.sentinel.transport.port=8719 //通信端口 默认8719
application.yml 配置
spring:application:name: yimall-usercloud:sentinel:transport:dashboard: localhost:8333port: 8719
三 、下载jar
官网下载
启动 jar
java -Dserver.port=8333 -Dcsp.sentinel.dashboard.server=localhost:8333 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar
四、 流控/限流 统一响应返回异常@Configuration
public class SentinelConfig implements BlockExceptionHandler {
@Overridepublic void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
System.out.println("-----------------------------------------");httpServletResponse.setContentType("application/json;charset=utf-8");Ret data = Ret.success();if (e instanceof FlowException) {
data = new Ret(-1, "流控规则被触发......");} else if (e instanceof DegradeException) {
data = new Ret(-2, "降级规则被触发......");} else if (e instanceof AuthorityException) {
data = new Ret(-3, "授权规则被触发......");} else if (e instanceof ParamFlowException) {
data = new Ret(-4, "热点规则被触发......");} else if (e instanceof SystemBlockException) {
data = new Ret(-5, "系统规则被触发......");}httpServletResponse.getWriter().write(JSON.toJSONString(data));}
}