美文网首页
sentinel和本地配置规则文件

sentinel和本地配置规则文件

作者: 张云飞Vir | 来源:发表于2022-10-15 21:31 被阅读0次

    写在前面

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

    概念

    资源,可以是任何东西,服务,服务里的方法,甚至是一段代码。使用 Sentinel 来进行资源保护,主要分为几个步骤:

    • 定义资源
    • 定义规则
    • 检验规则是否生效

    先把可能需要保护的资源定义好,针对资源配置各种流量控制规则,就可以使用了。
    可考虑 整合动态配置系统,如 ZooKeeper、Nacos 等,动态地实时刷新配置规则

    也可使用 控制台 以web 方式来来动态修改规则,即配合 Sentinel Dashboard 使用。

    sentinel 提供了对主流框架的适配。

    开始使用

    在 spring-cloud 项目中使用非常方便,加入依赖

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    

    使用 @SentinelResource 注解 声明一个 资源即可。

       @SentinelResource(value = "sayHello")
       public String sayHello(String name) {
           return "Hello, " + name;
       }
    

    @SentinelResource 还提供了其它额外的属性:

    • 如 blockHandler,blockHandlerClass, fallback 用于表示限流或降级的操作.
    • 若不配置 blockHandler、fallback 等函数,则被流控降级时方法会直接抛出对应的 BlockException;

    网址路径即是资源名
    除了手动的用 SentinelResource 声明一个资源名的方式,sentinel 提供了自动配置的方式,对于一个 controller 的方法会自动生成。
    比如 我写一个 /order/some2 接口,那么资源名就是 /order/some2 ,我可以对这个资源直接配置规则。

    以本地文件方式加载 规则

    sentine 支持多种方式配置 规则。比如:

    • 控制台方式:启动一个控制台,web 管理的方式操作配置,非常方便,缺点是重启就没了
    • 本地文件方式:写个本地配置文件,防止到项目文件夹下
    • nacos配置中心方式:配置文件防止到nacos中

    这里说下本地配置文件的方式。

    (1)修改 yml 配置文件
    在yml 配置文件里声明了 本地 规则文件 的地址,修改 yml 配置文件即可。比如下面的示例配置了 flowrule.json 和 degraderule.json 两个配置文件。

    spring:
      cloud:
        sentinel:
          # 控制台地址,连不上也不会报错。
          transport:
            port: 8719
            dashboard: 192.168.1.111:8180
          datasource:
            ds1:
              file:
                file: classpath:flowrule.json
                data-type: json
                rule-type: flow
            ds2:
              file:
                file: classpath:degraderule.json
                data-type: json
                rule-type: degrade
          eager: true #立即加载
    
    

    (2)编写规则文件
    编写规则文件 放置到 resources 文件夹下。
    比如我的 flowrule.json

    [
     {
       "resource": "orderFindById",
       "controlBehavior": 0,
       "count": 1,
       "grade": 1,
       "limitApp": "default",
       "strategy": 0
     },
     {
       "resource": "/order/some2",
       "controlBehavior": 0,
       "count": 5,
       "grade": 1,
       "limitApp": "default",
       "strategy": 0
     }
    ]
    
    

    我的 degraderule.json 规则文件

     [
      {
        "resource": "testDeGreade",
        "limitApp": "default",
        "grade": 0,
        "count": 70,
        "slowRatioThreshold": 0.5,
        "minRequestAmount": 5,
        "statIntervalMs": 1000,
        "timeWindow": 10
      },
      {
        "resource": "/order/testDegreade2",
        "limitApp": "default",
        "grade": 0,
        "count": 70,
        "slowRatioThreshold": 0.5,
        "minRequestAmount": 5,
        "statIntervalMs": 1000,
        "timeWindow": 10
      }
    ]
    
    

    至此就配置完成了,配合 声明 资源的方式就可使用了。

    Feign 支持

    Sentinel 提供了对 Feign 的支持

    Sentinel 适配了 Feign 组件。使用时分下面两个步骤:

    具体来说:
    (1) 加入 openfeign 依赖, 使自动化配置类生效:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    

    记得也依赖 sentinel

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    

    (2) 配置文件打开 Sentinel 对 Feign 的支持:

    # 开启 Feign 对 Sentinel 的支持
    feign:
      sentinel:
        enabled: true
    

    资源名
    Feign 对应的接口中的资源名策略定义:httpmethod:protocol://requesturl

    Sentinel 控制台

    我们可以使用 Sentinel 控制台 ,启动后是个web,在网页上方便修改配置的规则。

    Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理、监控(单机和集群),规则管理和推送的功能。

    Sentinel 控制台包含如下功能:
    查看机器列表以及健康情况:收集 Sentinel 客户端发送的心跳包,用于判断机器是否在线。
    监控 (单机和集群聚合):通过 Sentinel 客户端暴露的监控 API,定期拉取并且聚合应用监控信息,最终可以实现秒级的实时监控。
    规则管理和推送:统一管理推送规则。
    鉴权:生产环境中鉴权非常重要。这里每个开发者需要根据自己的实际情况进行定制。

    这里下载:https://github.com/alibaba/Sentinel/releases

    启动它即可

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

    参考

    https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
    https://blog.csdn.net/qq_41169544/article/details/122629640

    相关文章

      网友评论

          本文标题:sentinel和本地配置规则文件

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