美文网首页
sentinel学习

sentinel学习

作者: 传葱 | 来源:发表于2019-10-11 21:23 被阅读0次

1.sentinel是什么?

sentinel是一个中间件,通过注解和filter等侵入较轻的方式指定接口,定义降级,熔断,流控规则。

2.接入:

1.pom.xml
<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-core</artifactId>
  <version>1.4.0</version>
</dependency>
 
<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-transport-simple-http</artifactId>
  <version>1.4.0</version>
</dependency>
 
<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-annotation-aspectj</artifactId>
  <version>1.4.0</version>
</dependency>
<!-- spring cloud -->
 
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-alibaba-sentinel</artifactId>
  <version>0.2.1.RELEASE</version>
</dependency>
 
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  <version>2.0.2.RELEASE</version>
</dependency>
<!-- dubbo filter -->
<dependency>
  <groupId>com.kuaikan.comic</groupId>
  <artifactId>comic-common</artifactId>
  <version>1.3.0</version>
</dependency>
 
2.application.yml
spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080
 
3.XXXServiceApplication.java
@ComponentScan(
    value = {
      "com.kuaikan.game.gamecard",
      "com.kuaikan.common.perfcounter",
      "com.kuaikan.common.service",
      "com.kuaikan.comic.common.sentinel",
      "com.alibaba.csp.sentinel.annotation.aspectj"
    })
 
@Bean("sentinelRuleConfig")
public SentinelRuleConfig initSentinelRuleConfig() {
  return new SentinelRuleConfig();
}
 
@Bean("sentinelResourceAspect")
public SentinelResourceAspect initSentinelResourceAspect() {
  return new SentinelResourceAspect();
}

3.@SentinelResource 使用

##Apollo 配置
sentinel_flow_rules //流控
    {
        "resource": "fuse",
        "limitApp": "default",
        "grade": 1,
        "count": 10,
        "strategy": 0,
        "controlBehavior": 0,
        "warmUpPeriodSec": 0,
        "maxQueueingTimeMs": 0
    }
 
sentinel_degrade_rules //降级规则
    {
        "resource": "hello1",
        "limitApp": "default",
        "count": 0.001,
        "timeWindow": 10,
        "grade": 1
    }
 
 
@SentinelResource(
    value = "fuse", // 限流配置名称 resource
    blockHandler = "handleHelloException", // 发生限流时处理方法
    blockHandlerClass = SentinelExceptionUtil.class)
@GetMapping("/hello")
public String hello() {
  return "didispace.com";
}
 
@SentinelResource(
    value = "hello1", // 限流配置名称 resource
    blockHandler = "handleHello1Exception", // 发生熔断时处理方法
    blockHandlerClass = SentinelExceptionUtil.class,
    entryType = EntryType.IN)
@GetMapping("/hello1")
public String hello1() {
  throw new RuntimeException("hello1");
}
 
//到达资源定义规则的阙值,触发
public class SentinelExceptionUtil {
  //函数必须是static  参数和被注释函数相同, 最后一个函数是BlockException
  public static String handleHelloException(BlockException e) {
    return "helloWorld";
  }
 
  // handleHello1Exception
  public static String handleHello1Exception(BlockException e) {
    return "hello1World";
  }
}

4.dubbo调用

##注入filter  sentinelDubboConsumerFilter
// 推荐服务
@Reference(registry = "rec-idc", timeout = 600, filter = "sentinelDubboConsumerFilter")
private GuessRecService guessRecService;
 
//调用
@Autowired private GuessRecService guessRecService;
 
@GetMapping("/rec")
public Object demo2() {
  GuessRequest guessRequest = new GuessRequest();
  guessRequest.setNum(123);
  return guessRecService.getGuessRec("arg1", "arg2", guessRequest);
}
 
//到达规则阙值触发调用, 参数和原参数一致
public class GuessRecServiceHandler {
  public static Object getGuessRec(String var1, String var2, GuessRequest var3) {
    return Lists.newArrayList(var3);
  }
}
 
//apollo 配置
use_sentinel_fail_back: true   //开启熔断流控处理
sentinel_fail_back_url: com.kuaikan.game.gamecard.handler   //处理流控降价函数的包名, 也就是上面GuessRecServiceHandler所在的package

5.sentinel核心原理
https://github.com/alibaba/Sentinel/wiki/Sentinel%E5%B7%A5%E4%BD%9C%E4%B8%BB%E6%B5%81%E7%A8%8B
6.基本使用

@GetMapping("/demo1")
public String demo1() {
  initDegradeRule();
 
  Entry entry = null;
  try {
    //开始一个entry, 一个资源可以有多个entry, 一条链路上的entry组成一棵树
    entry = SphU.entry("demo1", EntryType.IN);
    Thread.sleep(10);
    return "demo1";
  } catch (Throwable t) {
    //到达规则阙值时候的处理
    if (!BlockException.isBlockException(t)) {
      Tracer.trace(t);
    }
    log.error("demo1");
  } finally {
    //关闭entry
    if (entry != null) {
      entry.exit();
    }
  }
  return "lalal";
}
 
//定义降级规则
private void initDegradeRule() {
  List<DegradeRule> rules = new ArrayList<>();
  DegradeRule rule = new DegradeRule();
  rule.setResource("demo1");
  // set threshold RT, 10 ms
  rule.setCount(2);
  rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
  rule.setTimeWindow(10);
  rules.add(rule);
  DegradeRuleManager.loadRules(rules);
}

7.Apollo配置


image.png

8.项目启动时候做了两件事:

  • 拉取Apollo对应key的数据,加载到进程
  • 配置监听规则改变的监听器,一旦配置发生变化,执行上一步(拉取到本地,加载到进程)
  1. 启动dashboard ,可视化配置界面, apollo配置的规则会同步到dashboard,但是dashboard配置的规则只会在项目内存中保存,重启消失,不会持久化到Apollo

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

image.png

相关文章

网友评论

      本文标题:sentinel学习

      本文链接:https://www.haomeiwen.com/subject/ypnxmctx.html