pom依赖
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
yml配置
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
#暴露端点
management:
endpoints:
web:
exposure:
include: '*'
MainApp8401
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401
{
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class, args);
}
}
myhandlerclass
public class BlockExceptionHandler {
public static String exceptionHandler(BlockException blockException){
return "{className: BlockExceptionHandler, methodName: selfDefineExceptionHandler,exception: "+blockException.getMessage()+"} ";
}
}
测试controller内附关键注解
@RestController
public class DeSelfController {
@GetMapping(value = "/selfcon")
@SentinelResource(value = "selfcon",
blockHandlerClass = BlockExceptionHandler.class,
blockHandler = "exceptionHandler"
)
public String selfDefineExceptionHandler(){
return "{className: DeSelfController, methodName: selfDefineExceptionHandler}";
}
}
添加限流规则
标签:自定义,class,alibaba,限流,sentinel,错误处理,public,cloud From: https://www.cnblogs.com/huoziqi/p/17542190.html