Spring Cloud项目整合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("/testSentinel")
@SentinelResource(value = "test", blockHandler = "handleException")
public String testSentinel() {
return "Sentinel is working!";
}
public String handleException(BlockException ex) {
return "Sorry, the system is busy now, please try again later.";
}
}
这段代码演示了如何在Spring Cloud项目中使用Sentinel进行流量控制。@SentinelResource
注解用于定义资源,并指定了当资源访问受限时的回退方法handleException
。在实际使用中,你需要配置Sentinel的规则来适应你的服务负载和并发要求。
评论已关闭