This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Spring Cloud integrates with Sentinel
Spring Cloud Alibaba integrates Servlets, RestTemplate, FeignClient, and SpringWebFlux for Sentinel by default. Sentinel completes the shortcomings of Hystrix in servlets and RestTemplates, and is compatible with the use of Hystrix in FeignClient to limit and downgrade traffic. Sentinel supports flexible configuration and adjustment of flow control rules.
Sentinel connects to Spring Cloud
-
Create a SpringBoot based project and integrate SpringCloud dependencies
-
Adding Sentinel dependencies
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> < version > 2.2.5. RELEASE < / version > < / dependency >Copy the code
-
Create a REST interface and configure traffic limiting to protect the resource using @sentinelResource
@RestController
@RequestMapping("/sentinel")
public class SentinelController {
@SentinelResource(value = "getName",blockHandler = "blockHandlerMethodByLimit")
@GetMapping(value = "/{name}")
public String getName(@PathVariable("name") String name){
return name;
}
public String blockHandlerMethodByLimit(BlockException e){
return "Restricted flow"; }}Copy the code
You can configure traffic limiting resources in the following ways
- Sentinel Starter provides traffic limiting sites for all HTTP services by default, and only needs to add dependencies without modifying the code
- To limit or degrade a particular method, use the @SentinelResource annotation to define a stream-limiting resource
- The resource is configured through sphu.entry ()
Manual configuration of flow control rules is implemented with the help of Sentinel’s InitFunc SPI extension interface. InitFunc interface needs to be implemented and relevant rule logic needs to be written in init method
public class FlowRlueInitFunc implements InitFunc {
@Override
public void init(a) throws Exception {
List<FlowRule> list = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setCount(1);
flowRule.setResource("getName");
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setLimitApp("default"); list.add(flowRule); FlowRuleManager.loadRules(list); }}Copy the code
SPI is extension point mechanism, needs to be Sentinel load, but also in the resource directory to create a meta-inf/services/com. Alibaba. CSP. Sentinel. Init. InitFunc file, the file content is the full path of the custom extension point
com.ozx.springcloud.sentinel.util.FlowRuleInitFunc
With the above configuration, Sentinel automatically loads the flow control rules for the getName resource on the first access to any resource.
After start the service, visit http://localhost:8080/sentinel/Gxin method, when the access frequency exceeds the threshold, will trigger the current limit.
The above configuration process is based on manual configuration to load the Sentinel flow control rules. The next article will describe another way to configure the relevant flow control rules through the Sentinel Dashboard.