【springcloud 微服务】Spring Cloud Alibaba整合Sentinel详解
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/testA")
@SentinelResource(value = "testA", blockHandler = "handleExceptionA")
public String testA() {
return "Hello, Sentinel A";
}
public String handleExceptionA(BlockException ex) {
return "Error: " + ex.getClass().getSimpleName();
}
@GetMapping("/testB")
@SentinelResource(value = "testB", blockHandler = "handleExceptionB")
public String testB() {
return "Hello, Sentinel B";
}
public String handleExceptionB(BlockException ex) {
return "Error: " + ex.getClass().getSimpleName();
}
}
这个代码示例展示了如何在Spring Cloud Alibaba项目中使用Sentinel进行资源的保护以及定义异常处理方法。通过@SentinelResource
注解指定资源名和异常处理方法,当资源访问受限时,会调用对应的异常处理方法。
评论已关闭