alibaba sentinel的基本入门
sentinel的官方文档:https://sentinelguard.io/zh-cn/docs/introduction.html
sentinel介绍:
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,
主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
Sentinel 的使用可以分为两个部分:
核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配)。
控制台(Dashboard):Dashboard 主要负责管理推送规则、监控、管理机器信息等。
入门案例1: Java代码保护
1、pom.xml引入
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.3 </version>
</dependency>
```
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.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
public class SentinelTest01 {
public void initFlowRules(){
List
rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
//0并发线程数 1:QPS
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(3);//1秒内仅允许3个请求
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
@Test
public void test1(){
initFlowRules() ;
Entry entry = null;
try {
// 资源名可使用任意有业务语义的字符串
entry = SphU.entry("HelloWorld");
// 被保护的业务逻辑
// do something...
System.out.println("正常执行业务逻辑");
} catch (BlockException e1) {
// 资源访问阻止,被限流或被降级
// 进行相应的处理操作
System.out.println("发生了BlockException。");
} finally {
if (entry != null) {
System.out.println("entry.exit。。");
entry.exit();
}
}
}
}
```
入门案例2:
springboot中使用sentinel对接口限流。
(
springboot版本: 2.7.2
springcloud版本:2021.0.3
springcloud-alibaba版本:2021.0.1.0
(1)定义需要被保护的资源@SentinelResource
(2)定义限流、熔断规则
(3)定义sentinel的异常处理器
)
1、pom.xml引入
<!-- sentinel 客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2021.0.4.0</version>
</dependency>
2、controller示例类
```
@RestController
@RequestMapping( "/sentinelDemo")
public class SentinelDemoController {
//1、定义被保护的资源 @SentinelResource
@SentinelResource(value = "hello")
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public Resp hello() throws BlockException{ //没有定义 blockHandler ,此处需要声明式抛出BlockException
System.out.println("执行业务逻辑:hello");
//int i=2/0;
return Resp.success();
}
// 使用blockHandler进行熔断、降级处理
// @SentinelResource 注解 blockHandler 、fallback、blockHandlerClass 、fallbackClass
// @SentinelResource(value = "hello",blockHandler = "helloBlockHandler")
// @RequestMapping(value = "/hello",method = RequestMethod.GET)
// public Resp hello() {
// System.out.println("执行业务逻辑:hello");
// //int i=2/0;
// return Resp.success();
// }
// public Resp helloBlockHandler(BlockException e){
// System.out.println("请求限流了");
// return Resp.failure("hello请求限流了。。");
// }
//2、定义规则
@PostConstruct
public void init(){
List rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("hello");//资源名称
//0并发线程数 1:QPS
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(2); //1秒只能进入2个请求
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
```
3、BlockExcption异常处理
```
/**
* sentinel的blockException
* @param e
* @param request
* @return
*/
@ExceptionHandler(value={BlockException.class})
public Resp handleBlockException(BlockException e,HttpServletRequest request){
log.error(e.getMessage(),e);
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String requestPath = uri.replaceFirst(contextPath, "").replaceAll("/+", "/");
if(e instanceof FlowException){
log.info("接口被限流了,path="+requestPath);
return Resp.failure("1001","接口被限流了,path="+requestPath);
}else if(e instanceof DegradeException){
log.info("接口被降级了,path="+requestPath);
return Resp.failure("1002","接口被降级了,path="+requestPath);
}else if(e instanceof AuthorityException){
log.info("sentinel授权异常,path="+requestPath);
return Resp.failure("1003","sentinel授权异常,path="+requestPath);
}else if(e instanceof SystemBlockException){
log.info("sentinel系统异常,path="+requestPath);
return Resp.failure("1004","sentinel系统异常,path="+requestPath);
}
return Resp.failure(e.getMessage());
}
```
浏览器快速多次请求接口 ,发现接口限流了。
