美文网首页
01-spring-cloud-sentinel-01-基本使用

01-spring-cloud-sentinel-01-基本使用

作者: cjxz | 来源:发表于2024-03-12 16:52 被阅读0次

sentinel基本使用

1.1 编程式

下面是启动main方法,然后会创建限流规则initFlowRules()然后进入while循环,代码块Thread.sleep(10)System.out.println("hello world")就是业务方法。然后entry = SphU.entry("HelloWorld")是使用了一个HelloWorld的资源。这个资源的限流模式再initFlowRules()里面已经定义了。

public class MySentinelTest {
    public static void main(String[] args) {
        initFlowRules();
        while (true) {
            Entry entry = null;
            try {
                entry = SphU.entry("HelloWorld");
                /*您的业务逻辑 - 开始*/
                Thread.sleep(10);
                System.out.println("hello world");
                /*您的业务逻辑 - 结束*/
            } catch (BlockException | InterruptedException e) {
                /*流控逻辑处理 - 开始*/
                System.out.println("block!");
                /*流控逻辑处理 - 结束*/
            } finally {
                if (entry != null) {
                    entry.exit();
                }
            }
        }
    }

    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

上面代码执行可以新建一个maven项目,然后导入sentinel依赖。由于现在spring主要推荐3.x所以无法再spring init上面创建jdk为1.8的项目。大家可以去alibaba上面创建空的项目。地址如下:https://start.aliyun.com/

1.2 注解式

第一步:创建一个controller层和service层。其中service层使用注解控制资源

controller层代码

@RestController
public class MySentinelController {
    @Autowired
    private MySentinelService mySentinelService;

    @GetMapping("/helloworld")
    public String helloworld(){
        String message = mySentinelService.getInfo();
        return message;
    }
}

service层代码

controller层会调用service的getInfo方法。再getInfo方法上面新增资源注解@SentinelResource(value = "helloworld",blockHandler = "blockMessage其中value是资源的名字,blockHandler是如果触发限流,那么执行什么方法。这个例子里面如果触发限流会调用下面的blockMessage方法。主要这里的入参需要时BlockException

@Service
public class MySentinelService {
    @SentinelResource(value = "helloworld",blockHandler = "blockMessage")
    public String getInfo() {
        return "hello world sentinel";
    }

    public String blockMessage(BlockException e){
        System.out.println(e);
        return "阻塞了!";
    }
}

第二步:创建限流规则hellworld

springboot启动类里面新增初始化限流规则的方法

@SpringBootApplication
public class LearningSentinelApplication {

    public static void main(String[] args) {
        SpringApplication.run(LearningSentinelApplication.class, args);
        initFlowRules();
    }

    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("helloworld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(1);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

:::success

上面是一个简单的sentinel入门程序,能够让我们看到sentinel的作用,也了解一些再sentinel里面的概念,比如:资源。下面是根据上面代码整合sentinel提供的dashboard

:::

dashboard环境搭建

2.1 下载dashboard

可以去github下载

2.2 启动dashboard

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

2.3 访问dashboard

地址:http://localhost:8080/#/login

默认账号密码:sentinel/sentinel

访问结果:


image.png

2.4 项目接入dashboard

修改项目配置文件

# Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
# 取消Sentinel控制台懒加载
# 默认情况下 Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包
# 配置 sentinel.eager=true 时,取消Sentinel控制台懒加载功能
spring.cloud.sentinel.eager=true
# 如果有多套网络,又无法正确获取本机IP,则需要使用下面的参数设置当前机器可被外部访问的IP地址,供admin控制台使用
# spring.cloud.sentinel.transport.client-ip=
server.port=8081
spring.application.name=learning_sentinel

spring boot启动类去掉初始化限流规则

在spring启动类中去掉限流规则。可以在dashboard上面创建限流规则,然后再业务代码中使用。后面也可以整合nacos通过nacos下发限流规则

    public static void main(String[] args) {
        SpringApplication.run(LearningSentinelApplication.class, args);
//        initFlowRules();
    }

2.5 启动项目,在dashboard上面配置helloworld限流规则

image.png

整合nacos实现下发限流规则

3.1 下载nacos客户端

可以去github上面下载

3.2 启动nacos

启动nacos

.\startup.cmd -m standalone

访问地址

http://192.168.31.14:8848/nacos/index.html

3.3 修改项目配置

项目添加nacos依赖

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

修改项目配置文件

下面是配置项目里面的application.properties新增nacos地址,其中dataId一定要和配置的nacos保持一致,groupId也是一样

spring.cloud.sentinel.datasource.ds.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.ds.nacos.dataId=learning-sentinel
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP

spring.cloud.sentinel.datasource.ds.nacos.ruleType=flow

3.4 在nacos上面新增流控配置

upload_eebb3a2dca99454f385e7152f058243d.png

其中json表示配置的资源和资源的流控规则

[
    {
        "resource": "helloworld",
        "limitApp": "default",
        "grade": 1,
        "count": 2,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

相关文章

  • Flutter--Text/Container/Image

    Text基本使用 Container基本使用 Image基本使用

  • 基本使用

    1、 打开需要上传的文件夹执行: git init 格式化窗口 2、执行 git add . 上传文件 3、执行 ...

  • 基本使用

    href="javascript:;" 其中javascript: 是一个伪协议。它可以让我们通过一个链接来调用...

  • 基本使用

    数据库: 什么是数据库?简单来说就是存数据的。 都有什么是数据库? oracle(强大,跟金融政府打交道的,安全,...

  • 基本使用

    本文参考:https://morvanzhou.github.io/tutorials/machine-learn...

  • 6-xpath和css select基本使用

    Xpath基本使用 css select基本使用

  • MySQL语法入门(一)

    MySQL语法入门(一) 基本运算符使用 基本数学函数使用 基本字符串函数使用 基本日期时间函数使用

  • python time与datetime模块基本使用

    time模块基本使用 datetime模块基本使用

  • SQL语句基本使用

    SQL语句基本使用——增删改查 SQL语句基本使用——WHERE子句 SQL语句基本使用——AND和OR的使用 S...

  • iOS-UICollectionView基本使用

    iOS-UICollectionView基本使用 iOS-UICollectionView基本使用

网友评论

      本文标题:01-spring-cloud-sentinel-01-基本使用

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