sentinel可以进行流量控制,熔断限流等保护我们的系统,那么这里进行简单的使用下,看看怎么用,主要参考官网
https://sentinelguard.io/zh-cn/docs/quick-start.html 和 https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8
因为我这边是学习springCloudAlibaba,所以就直接在前面的基础上来搞了,其实任何项目都是可以的,这里用最简单的springboot项目为例子
1、目的
新建一个请求,通过sentinel来限制请求的QPS也就是每秒查询最多1次
2、搭建个springboot项目
https://www.suibibk.com/topic/1133524977824301056
3、引入sentinel的依赖
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.8.6</version></dependency>
4、测试类
package com.suibibk.springCloud.order.controller;import com.alibaba.csp.sentinel.Entry;import com.alibaba.csp.sentinel.SphU;import com.alibaba.csp.sentinel.slots.block.BlockException;import com.alibaba.csp.sentinel.slots.block.RuleConstant;import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;import java.util.ArrayList;import java.util.List;@RestController@RequestMapping("/order")public class OrderController {public static final String HELLO_RESOURCE_NAME = "hello";@RequestMapping("/hello")public String hello(){// 1.5.0 版本开始可以直接利用 try-with-resources 特性try (Entry entry = SphU.entry(HELLO_RESOURCE_NAME)) {// 被保护的逻辑return "hello world";} catch (BlockException ex) {// 处理被流控的逻辑return "被限流了";}}@PostConstructprivate void initFlowRules(){List<FlowRule> rules = new ArrayList<>();FlowRule rule = new FlowRule();rule.setResource(HELLO_RESOURCE_NAME);rule.setGrade(RuleConstant.FLOW_GRADE_QPS);// Set limit QPS to 20. 这里表示超过1秒的频率就会限流rule.setCount(1);rules.add(rule);FlowRuleManager.loadRules(rules);}}
若是IDEA import Entry 提示 “Cannot resolve symbol” 解决办法 “File” -> “Invalidate Caches / Restart”
上面主要的就是对资源进行控制比如这里的资源是“hello”,先是在bean初始化的时候进行流量规则设置,resource为“hello”.
@PostConstructprivate void initFlowRules(){List<FlowRule> rules = new ArrayList<>();FlowRule rule = new FlowRule();rule.setResource(HELLO_RESOURCE_NAME);rule.setGrade(RuleConstant.FLOW_GRADE_QPS);// Set limit QPS to 20. 这里表示超过1秒的频率就会限流rule.setCount(1);rules.add(rule);FlowRuleManager.loadRules(rules);}
然后在我们的业务逻辑里就可以对这个资源进行控制了
@RequestMapping("/hello")public String hello(){// 1.5.0 版本开始可以直接利用 try-with-resources 特性try (Entry entry = SphU.entry(HELLO_RESOURCE_NAME)) {// 被保护的逻辑return "hello world";} catch (BlockException ex) {// 处理被流控的逻辑return "被限流了";}}
//被保护的逻辑就是我们的正常业务逻辑,若超过了流量控制规则,那么就会到catch逻辑,我们访问http://localhost:8080/order/hello 点快两下,发现就返回了“被限流了”.
上面太麻烦了,用注解比较好,下面用下注解,文档参考
https://sentinelguard.io/zh-cn/docs/annotation-support.html
5、用注解
pom.xml引入
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId><version>1.8.0</version></dependency>
再添加个规则
FlowRule rule2 = new FlowRule();rule2.setResource(USER_RESOURCE_NAME);rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);// Set limit QPS to 20. 这里表示超过1秒的频率就会限流rule2.setCount(1);rules.add(rule2);
添加个测试方法
/*** @param id* @return*/@RequestMapping("/user")@SentinelResource(value = USER_RESOURCE_NAME, blockHandler = "exceptionHandler")public String user(String id) {return "返回用户成功"+id;}// Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.public String exceptionHandler(String id, BlockException ex) {// Do some log here.ex.printStackTrace();return "被限流了"+id;}
启动类需要加上
@Beanpublic SentinelResourceAspect sentinelResourceAspect(){return new SentinelResourceAspect();}
启动测试,发现也是ok的, @SentinelResource还可以指定异常所在类,以及fallback异常等,这些直接参考官方文档就可以了,很详细!
ok!
