Java中基于推、拉模式的sentinel规则持久化详解

 更新时间:2023年09月12日 10:08:26   作者:雪峰.贵  
这篇文章主要介绍了Java中基于推、拉模式的sentinel规则持久化详解,推模式是sentinelDashboard 把规则推给Nacos,Nacos监听规则的变化推给微服务,拉模式是sentinelDashboard 把规则直接给微服务, Nacos定时的同步微服务端的规则,需要的朋友可以参考下

推 模式

sentinelDashboard 把规则 推给Nacos,Nacos监听规则的变化推给微服务

下载sentinel懒人包

执行 java -jar sentinel-dashboard.jar

加依赖

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

加配置

spring:
  cloud:
    sentinel:
      datasource:
        # 名称随意
        flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-flow-rules
            groupId: SENTINEL_GROUP
            # 规则类型,取值见:
            # org.springframework.cloud.alibaba.sentinel.datasource.RuleType
            rule-type: flow
        degrade:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade
        system:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-system-rules
            groupId: SENTINEL_GROUP
            rule-type: system
        authority:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-authority-rules
            groupId: SENTINEL_GROUP
            rule-type: authority
        param-flow:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}-param-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: param-flow

拉模式

sentinelDashboard 把规则直接给微服务, Nacos定时的同步微服务端的规则。

加依赖

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

创建FileDataSourceInit

import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.*;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
 * 拉模式规则持久化
 *
 * @author itmuch.com
 */
@Slf4j
public class FileDataSourceInit implements InitFunc {
    @Override
    public void init() throws Exception {
        // TIPS: 如果你对这个路径不喜欢,可修改为你喜欢的路径
        String ruleDir = "E:\\ideaworkspace\\content-center\\src\\main\\resources\\sentinel\\rules";
        String flowRulePath = ruleDir + "\\flow-rule.json";
        String degradeRulePath = ruleDir + "\\degrade-rule.json";
        String systemRulePath = ruleDir + "\\system-rule.json";
        String authorityRulePath = ruleDir + "\\authority-rule.json";
        String paramFlowRulePath = ruleDir + "\\param-flow-rule.json";
        this.mkdirIfNotExits(ruleDir);
        this.createFileIfNotExits(flowRulePath);
        this.createFileIfNotExits(degradeRulePath);
        this.createFileIfNotExits(systemRulePath);
        this.createFileIfNotExits(authorityRulePath);
        this.createFileIfNotExits(paramFlowRulePath);
        // 流控规则
        ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
                flowRulePath,
                flowRuleListParser
        );
        // 将可读数据源注册至FlowRuleManager
        // 这样当规则文件发生变化时,就会更新规则到内存
        FlowRuleManager.register2Property(flowRuleRDS.getProperty());
        WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
                flowRulePath,
                this::encodeJson
        );
        // 将可写数据源注册至transport模块的WritableDataSourceRegistry中
        // 这样收到控制台推送的规则时,Sentinel会先更新到内存,然后将规则写入到文件中
        WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
        // 降级规则
        ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
                degradeRulePath,
                degradeRuleListParser
        );
        DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
        WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
                degradeRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
        // 系统规则
        ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
                systemRulePath,
                systemRuleListParser
        );
        SystemRuleManager.register2Property(systemRuleRDS.getProperty());
        WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
                systemRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
        // 授权规则
        ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
                flowRulePath,
                authorityRuleListParser
        );
        AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
        WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
                authorityRulePath,
                this::encodeJson
        );
        WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
        // 热点参数规则
        ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
                paramFlowRulePath,
                paramFlowRuleListParser
        );
        ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
        WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
                paramFlowRulePath,
                this::encodeJson
        );
        ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
    }
    private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<FlowRule>>() {
            }
    );
    private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<DegradeRule>>() {
            }
    );
    private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<SystemRule>>() {
            }
    );
    private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<AuthorityRule>>() {
            }
    );
    private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
            source,
            new TypeReference<List<ParamFlowRule>>() {
            }
    );
    private void mkdirIfNotExits(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    private void createFileIfNotExits(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            if (!file.createNewFile()) {
                log.warn("文件创建失败!filePath = {}", filePath);
            }
        }
    }
    private <T> String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}

 创建目录与文件

在这里插入图片描述

文件里写入FileDataSourceInit 的全路径,即 包名+类名

到此这篇关于Java中基于推、拉模式的sentinel规则持久化详解的文章就介绍到这了,更多相关推、拉模式的sentinel规则持久化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java8简单了解Lambda表达式与函数式接口

    Java8简单了解Lambda表达式与函数式接口

    这篇文章主要介绍了Java8简单了解Lambda表达式与函数式接口,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Android入门简单实例

    Android入门简单实例

    这篇文章主要介绍了Android入门简单实例,对于初学Android的朋友有一定的借鉴价值,需要的朋友可以参考下
    2014-08-08
  • Java设计模式之观察者模式详解

    Java设计模式之观察者模式详解

    这篇文章主要介绍了Java设计模式之观察者模式详解,对象之间的多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化,需要的朋友可以参考下
    2023-12-12
  • spring aop两种配置方式

    spring aop两种配置方式

    这篇文章主要为大家详细介绍了spring aop两种配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以参考下
    2015-09-09
  • Java实现SHA-1算法实例

    Java实现SHA-1算法实例

    这篇文章主要介绍了Java实现SHA-1算法,实例分析了java实现SHA-1算法的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • RabbitMQ中的channel信道、exchange交换机和queue队列详解

    RabbitMQ中的channel信道、exchange交换机和queue队列详解

    这篇文章主要介绍了RabbitMQ中的channel信道、exchange交换机和queue队列详解,connection是指物理的连接,一个client与一个server之间有一个连接,一个连接上可以建立多个channel,可以理解为逻辑上的连接,需要的朋友可以参考下
    2023-08-08
  • Maven中的库repository详解

    Maven中的库repository详解

    Maven中要配置库,可以有多种方式,最直接的是在项目中的pom.xml文件中,通过<repositories>配置库,这样配置的库仅适用于当前项目,这篇文章主要介绍了Maven中的库(repository),需要的朋友可以参考下
    2024-01-01
  • Java开发工具-scala处理json格式利器-json4s详解

    Java开发工具-scala处理json格式利器-json4s详解

    这篇文章主要介绍了开发工具-scala处理json格式利器-json4s,文章中处理方法讲解的很清楚,有需要的同学可以研究下
    2021-02-02
  • JAVA中 Spring定时器的两种实现方式

    JAVA中 Spring定时器的两种实现方式

    本文向您介绍Spring定时器的两种实现方式,包括Java Timer定时和Quartz定时器,两种Spring定时器的实现方式各有优点,可结合具体项目考虑是否采用。
    2015-09-09
  • JAVA中方法的声明及使用方式(继承、多态、封装)

    JAVA中方法的声明及使用方式(继承、多态、封装)

    这篇文章主要介绍了JAVA中方法的声明及使用方式(继承、多态、封装),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02

最新评论