依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-demo</artifactId>
<groupId>demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-sentinel</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--健康监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- mybatis 和SpringBoot 整合-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
配置
server:
port: 8002
spring:
application:
name: demo-nacos
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.2.20:30569/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
cloud:
nacos:
server-addr: 192.168.2.20:30010
username: nacos
password: nacos
sentinel:
transport:
dashboard: 192.168.2.20:31596 # 控制台的安装位置
port: 8719 # 与sentinel单独连接的端口
client-ip: 192.168.2.20 # 本机的ip,如果sentinel装在虚拟机,必须配这个
# 暴露应用信息
management:
endpoints:
web:
exposure:
include: '*'
接口示例
package com.test.sentinel.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.nacos.common.model.RestResult;
import com.test.sentinel.common.handler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author jia
* @since 2022-04-04 22:06
*/
@RestController
@RequestMapping("/sentinel")
public class TestController {
@GetMapping("/test1")
@SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException")
public RestResult test1() {
return new RestResult(200, "success");
}
@GetMapping("/test2")
@SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException2")
public RestResult test2() {
return new RestResult(200, "success");
}
}
流控处理类
package com.test.sentinel.common.handler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.nacos.common.model.RestResult;
/**
* @author jia
* @since 2022-04-04 22:03
*/
public class CustomerBlockHandler {
public static RestResult handlerException(BlockException ex) {
return new RestResult(40001, "自定义异常1");
}
public static RestResult handlerException2(BlockException ex) {
return new RestResult(40002, "自定义异常2");
}
}