一、环境准备
1、环境搭建,查看Spring Cloud专栏
二、介绍
1、在定义了资源点之后,我们可以通过Dashboard控制台页面来设置限流和降级策略来对资源点进行保护。同时还能通过@SentinelResource注解来指定出现异常时的处理策略。
2、@SentinelResource注解的blockHandler字段,指定发生Sentinel异常时的处理方法(即触发Sentinel五种类型规则时的处理逻辑)。fallback字段指定发生业务异常时的处理方法。当然,为了省事,可以不指定blockHandler,所有处理逻辑都放在fallback中。
3、参数介绍:图示
三、代码配置
1、新增SentinelBlockHandler类和SentinelFallback类
package com.cyun.sys.controller.sentinel;
import com.alibaba.csp.sentinel.slots.block.BlockException;
/**
* 触发Sentinel五种类型规则时的处理逻辑
*
* @author He PanFu
* @date 2022-03-16 11:24:17
*/
public class SentinelBlockHandler {
public static String testBlockHandler(BlockException e) {
return "sentinel限流";
}
}
package com.cyun.sys.controller.sentinel;
/**
* 指定发生业务异常时的处理方法
*
* @author He PanFu
* @date 2022-03-16 11:24:32
*/
public class SentinelFallback {
public static String testFallback() {
return "方法错误";
}
}
2、测试类使用
package com.cyun.sys.controller.sentinel;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.security.RolesAllowed;
/**
* 测试 controller
*
* @author He PanFu
* @date 2022-03-12 17:53:02
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class TestSentinelController {
int i = 0;
/**
* 熔断测试
*
* @return 结果
*/
@GetMapping("/sentinel1")
@SentinelResource(value = "test_sentinel1", blockHandlerClass = SentinelBlockHandler.class, blockHandler = "testBlockHandler",
fallbackClass = SentinelFallback.class, fallback = "testFallback")
public String sentinel1() {
if (i++ % 3 == 0) {
throw new RuntimeException();
}
return "新增接口";
}
/**
* 熔断测试
*
* @return 结果
*/
@GetMapping("/sentinel2")
@SentinelResource(value = "test_sentinel2", blockHandler = "testBlockHandler", fallback = "testFallback")
public String sentinel2() {
if (i++ % 3 == 0) {
throw new RuntimeException();
}
return "新增接口";
}
/**
* 熔断测试
*
* @return 结果
*/
@GetMapping("/sentinel3")
@SentinelResource(value = "test_sentinel3", blockHandler = "testBlockHandler", fallback = "testFallback",
exceptionsToIgnore = {RuntimeException.class})
public String sentinel3() {
if (i++ % 3 == 0) {
throw new RuntimeException();
}
return "新增接口";
}
/**
* 熔断测试
*
* @return 结果
*/
@GetMapping("/sentinel4")
@SentinelResource(value = "test_sentinel4", fallback = "testFallback", defaultFallback = "testDefaultFallback")
public String sentinel4() {
if (i++ % 3 == 0) {
throw new RuntimeException();
}
return "新增接口";
}
/**
* 触发Sentinel五种类型规则时的处理逻辑
*
* @param e 异常
* @return 结果
*/
public String testBlockHandler(BlockException e) {
return "sentinel限流";
}
/**
* 指定发生业务异常时的处理方法
*
* @return 结果
*/
public String testFallback(Throwable throwable) {
return "方法错误";
}
/**
* 指定发生业务异常时的默认处理方法
*
* @return 结果
*/
public String testDefaultFallback() {
return "默认:方法错误";
}
}
3、Sentinel的Dashboard控制台配置
4、结果展示
-
触发Sentinel五种类型规则时
-
发生业务异常时的处理方法