spring cloud alibaba ----- Sentinel单独配置

不使用alibaba集成的sentinel组件,单独使用核心组件库。

         1、添加依赖:

<?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>lemon-alibaba</artifactId>
        <groupId>com.lemon.alibaba</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lemon-sentinel</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        sentinel 依赖,已使用alibaba组件,使用sentinel自己的核心库-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.0</version>
        </dependency>
<!--        sentinel 相关注解-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.8.0</version>
        </dependency>

    </dependencies>

</project>

        2、添加bean: 启动类中添加

        

package com.lemon.sentinel;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SentinelApplication {
    public static void main(String[] args) {
        SpringApplication.run(SentinelApplication.class,args);
    }

    @Bean
    public SentinelResourceAspect sentinelResourceAspect(){
        return new SentinelResourceAspect();
    }
}

             3、指定规则:在bean加载的时候启动执行,示例中使用的流控规则,可以添加以下爱多个方法。

   


    @PostConstruct // 此注解会在应用启动并且加载该类bean的时候自动执行,是一个init-method的方法
    private static void initFlowRules(){
        // 流控规则列表
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("/index"); //流控可限制的资源
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS); //设置QPS
        rule.setCount(1);
        rules.add(rule);

        FlowRuleManager.loadRules(rules); //加载配置好的规则
    }

             4、在需要的地方添加@SentinelResource注解:

                        value 值资源的名称,一般和请求地址保持一致

                        blockHandler sentinel规则的实现,需要与修饰的方法写在同一个文件中

                        如果需要单独下block规则:

                        

                blockHandlerClass = 规则类.class

                                blockHandler sentinel规则的实现,需要写成为静态的方法,即使用static修饰

                        fallback : 异常时候调用的方法,其参数、返回值和修饰的方法保持一致

        

package com.lemon.sentinel.conntroller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
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.GetMapping;
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("/sentinel")
public class IndexController {

    public static final String RESOURCE_NAME = "/index";

    @GetMapping("/index")
    @SentinelResource(value = RESOURCE_NAME,blockHandler = "blockHandlerForGetUser")
    public String index(){
        return "sentinel";
    }

    /**
     * 该方法的要求:
     *  1、public 修饰
     *  2、返回值和修饰的返回时需要保持一致
     *  3、参数也要保持一致
        4、额外参数BlockException  必须有
     * @return
     */
    public String blockHandlerForGetUser(BlockException e){
        e.printStackTrace();
        return "sentinel-error,流空方法处理";
    }

    /**
      
    *额外参数BlockException  必须有
    */
    public  String handleException(Throwable e){
        e.printStackTrace();
        return "exxception,异常处理方法";
    }




    @PostConstruct // 此注解会在应用启动并且加载该类bean的时候自动执行,是一个init-method的方法
    private static void initFlowRules(){
        // 流控规则列表
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource(RESOURCE_NAME); //流控可限制的资源
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS); //设置QPS
        rule.setCount(1);
        rules.add(rule);

        FlowRuleManager.loadRules(rules); //加载配置好的规则
    }




}

        以上每一个资源的名称都需要保持一致,才可以成功实现该机制。

        当规则和异常同时发生的时候:流控规则而优先级大于异常的优先级

5、控制台界面:

        1、下载sentinel-dashboard.jar 包

        2、java -jar sentinel-dasshboard.jar 程序

        3、默认localhost:8080,可通过 -Dserver.port= 端口 开修改启动的端口

        4、登录: 用户名: sentinel  密码:sentinel

        5、项目中加入依赖:

                

<!--        整合sentinel控制台使用-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.8.0</version>
        </dependency>

        6、项目中配置:启动项目的时候配置:JVM参数:

        -Dcsp.sentinel.dashboard.server=127.0.0.1:8080 指定sentinel控制台的地址和端口

        7、访问项目中的地址,然后刷新sentinel就可以看到已经注册的各种规则的列表

        8、sentinel控制台设定的参数,会在项目重启的时候丢失,因为其是保存在内存中的。

        持久化的方案与alibaba、nacos集成到数据库中

猜你喜欢

转载自blog.csdn.net/qq_31319235/article/details/121398768