springCloudAlibaba集成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 SentinelController {
@GetMapping("/hello")
@SentinelResource(value = "hello", blockHandler = "handleException")
public String helloService() {
return "Hello, Sentinel!";
}
public String handleException(BlockException ex) {
return "Service is blocked, details: " + ex.getClass().getSimpleName();
}
}
这段代码演示了如何在Spring Cloud Aliaba集成的项目中使用Sentinel进行流量控制。@SentinelResource
注解用于定义资源,并指定了当资源访问受限时的异常处理方法handleException
。当helloService
方法因为资源访问超出设定的限制而被限流时,将调用handleException
方法返回自定义的错误信息。
评论已关闭