• sentinel下载、安装、使用、循环引用异常


    官网:https://sentinelguard.io/zh-cn/index.html
    注解支持:https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
    sentinel控制台文档:https://sentinelguard.io/zh-cn/docs/dashboard.html
    参考:https://www.cnblogs.com/ralgo/p/14152390.html
    1、下载sentinel-dashboard.jar,下载地址:https://github.com/alibaba/Sentinel/releases
    并启动,cmd执行命令,最后面是jar包路径:java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar E:\download\sentinel-dashboard-1.8.3.jar
    浏览器访问 http://localhost:8080,登录账号密码都是sentinel
    2、项目的pom

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>cloud22</artifactId>
            <groupId>com.jay.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-provider-payment8001</artifactId>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.6.4</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>2021.0.1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2021.0.1.0</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>2021.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bootstrap</artifactId>
                <version>3.1.1</version>
            </dependency>
            <!--        不需要引用这些,仅仅引用 com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel/2.1.0.RELEASE 就可以,版本号高了会导致jar包循环引用的问题-->
            <!--        <dependency>-->
            <!--            <groupId>com.alibaba.csp</groupId>-->
            <!--            <artifactId>sentinel-core</artifactId>-->
            <!--            <version>1.8.3</version>-->
            <!--        </dependency>-->
            <!--        <dependency>-->
            <!--            <groupId>com.alibaba.csp</groupId>-->
            <!--            <artifactId>sentinel-annotation-aspectj</artifactId>-->
            <!--            <version>1.8.3</version>-->
            <!--        </dependency>-->
            <!--        <dependency>-->
            <!--            <groupId>com.alibaba.csp</groupId>-->
            <!--            <artifactId>sentinel-transport-simple-http</artifactId>-->
            <!--            <version>1.8.3</version>-->
            <!--        </dependency>-->
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
                <version>2.1.0.RELEASE</version>
            </dependency>
    
            <!--        <dependency>-->
            <!--            <groupId>com.alibaba.csp</groupId>-->
            <!--            <artifactId>sentinel-datasource-nacos</artifactId>-->
            <!--            <version>1.8.3</version>-->
            <!--        </dependency>-->
    
    
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
            </dependency>
            <!--mysql-connector-java-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--jdbc-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
            </dependency>
            <dependency>
                <groupId>com.jay.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </project>

     yaml修改了,增加了sentinel那一段的配置

    server:
      port: 8001
    spring:
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #Nacos作为服务中心地址
          config:
            server-addr: localhost:8848
            file-extension: yaml
            group: SH_GROUP
            namespace: test
        sentinel:
          transport:
            dashboard: localhost:9090 #配置sentinel dashboard地址,监控项目端口8001
            port: 8719 #默认8719端口,如果被占用,则从8719递增检查未被占用的端口
      application:
        name: cloud-payment-service #cloud-payment-service-dev.yaml
      profiles:
        active: test
    
    #${prefix}-${spring.profiles.active}.${file-extension}

    服务端代码示例:当热点规则配置 每秒1次,超过就会走dealGetPaymentById方法,deal方法的返回值和引用方法一样,参数多了一个 BlockException

    /**
         * SentinelResource 在 sentinel 网页中配置热点(HotKey)的时候使用 value,保持唯一,
         * blockHandler 是在 getPaymentById 方法异常后的处理方法
         */
        @SentinelResource(value = "PaymentController.getPaymentById", blockHandler = "dealGetPaymentById")
        @GetMapping(value = "/payment/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable(value = "id", required = true) Long id) {
            Payment payment = paymentService.getPaymentById(id);
            if (payment != null) {
                return new CommonResult(200, "查询成功,serverPort:  " + serverPort, payment);
            } else {
                return new CommonResult(444, "没有对应记录,查询ID: " + id, null);
            }
        }
    
        /**
         * sentinel 限流处理
         */
        public CommonResult<Payment> dealGetPaymentById(Long id, BlockException ex) {
            return new CommonResult(555, "Sentinel HotKey限流: " + id, null);
        }

     @SentinelResource中还可以配置fallback,服务如果出现异常,走fallback指定的方法处理异常Throwable。自定义异常处理。
    如果同时配置了fallback和blockHandler,当超过sentinel的流控规则时,流控规则blockHandler优先,否则走fallback。
    配置exceptionsToIgnore指定fallback不处理哪些异常。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>

    ymal:feign.sentinel.enabled = true
    启动类:@EnableFeignClients,消费端interface加@FeignClient(value = "服务id",fallback=xxx.class),例如:payment-service,
    xxx实现interface,其中的实现就是运行出错时的处理类。
    参考:https://www.bilibili.com/video/BV18E411x7eT?p=136
    保持接口和服务端签名一致,加注解,例如@GetMapping(value = "服务的调用路径 payment/get/{id}")。
    sentinel持久化:https://www.bilibili.com/video/BV18E411x7eT?p=138

    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
        <version>1.8.0</version>
    </dependency>

    ymal:

    前缀:spring.cloud.sentinel
    
    dataSource
     ds1:
      nacos:
       server-addr: localhost:8848
       dataId: cloudalibaba-sentinel-service
       groupId: DEFAULT_GROUP
       data-type: json
       rule-typle: flow
  • 相关阅读:
    bzoj2190[SDOI2008]仪仗队(欧拉函数)
    洛谷P3601签到题(欧拉函数)
    bzoj2818 Gcd(欧拉函数)
    poj2104 K-th Number(主席树静态区间第k大)
    只要有它,你就永远不会被打垮!
    网站美化常见CSS
    虚拟机安装CentOS6.4
    提高工作效率是有秘诀的
    不要消费信任
    项目经理必备7要素
  • 原文地址:https://www.cnblogs.com/xsj1989/p/16072646.html
Copyright © 2020-2023  润新知