ITPub博客

首页 > IT职业 > IT图书 > Sentinel(3)

Sentinel(3)

IT图书 作者:wyfem 时间:2021-09-09 08:52:24 0 删除 编辑

规则持久化

拉模式

参考大目老师的手记(手把手)拉模式

图片描述

sentinel控制台将规则推送给微服务,微服务会将规则生成JSON文件保存到磁盘的某的地方,然后sentinel会用定时任务定时读取这个文件,将它更新到规则缓存里面去,及时重启服务,sentinel都会去本地文件读取规则,实现规则的持久化。

导入依赖


<!--        sentinel拉文件模式-->
        
            com.alibaba.csp
            sentinel-datasource-extension
        

拉进功能代码

package com.itmuch.usercenter.sentineltest;

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 java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 拉模式规则持久化
 *
 * @author itmuch.com
 */
public class FileDataSourceInit implements InitFunc {
    @Override
    public void init() throws Exception {
        // TIPS: 如果你对这个路径不喜欢,可修改为你喜欢的路径
        String ruleDir = System.getProperty("user.home") + "/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> flowRuleRDS = new FileRefreshableDataSource(
            flowRulePath,
            flowRuleListParser
        );
        // 将可读数据源注册至FlowRuleManager
        // 这样当规则文件发生变化时,就会更新规则到内存
        FlowRuleManager.register2Property(flowRuleRDS.getProperty());
        WritableDataSource> flowRuleWDS = new FileWritableDataSource(
            flowRulePath,
            this::encodeJson
        );
        // 将可写数据源注册至transport模块的WritableDataSourceRegistry中
        // 这样收到控制台推送的规则时,Sentinel会先更新到内存,然后将规则写入到文件中
        WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);

        // 降级规则
        ReadableDataSource> degradeRuleRDS = new FileRefreshableDataSource(
            degradeRulePath,
            degradeRuleListParser
        );
        DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
        WritableDataSource> degradeRuleWDS = new FileWritableDataSource(
            degradeRulePath,
            this::encodeJson
        );
        WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);

        // 系统规则
        ReadableDataSource> systemRuleRDS = new FileRefreshableDataSource(
            systemRulePath,
            systemRuleListParser
        );
        SystemRuleManager.register2Property(systemRuleRDS.getProperty());
        WritableDataSource> systemRuleWDS = new FileWritableDataSource(
            systemRulePath,
            this::encodeJson
        );
        WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);

        // 授权规则
        ReadableDataSource> authorityRuleRDS = new FileRefreshableDataSource(
            authorityRulePath,
            authorityRuleListParser
        );
        AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
        WritableDataSource> authorityRuleWDS = new FileWritableDataSource(
            authorityRulePath,
            this::encodeJson
        );
        WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);

        // 热点参数规则
        ReadableDataSource> paramFlowRuleRDS = new FileRefreshableDataSource(
            paramFlowRulePath,
            paramFlowRuleListParser
        );
        ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
        WritableDataSource> paramFlowRuleWDS = new FileWritableDataSource(
            paramFlowRulePath,
            this::encodeJson
        );
        ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
    }

    private Converter> flowRuleListParser = source -> JSON.parseObject(
        source,
        new TypeReference>() {
        }
    );
    private Converter> degradeRuleListParser = source -> JSON.parseObject(
        source,
        new TypeReference>() {
        }
    );
    private Converter> systemRuleListParser = source -> JSON.parseObject(
        source,
        new TypeReference>() {
        }
    );

    private Converter> authorityRuleListParser = source -> JSON.parseObject(
        source,
        new TypeReference>() {
        }
    );

    private Converter> paramFlowRuleListParser = source -> JSON.parseObject(
        source,
        new TypeReference>() {
        }
    );

    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()) {
            file.createNewFile();
        }
    }

    private  String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}

在resources中创建META-INF.services(关闭拉模式可以将services这个文件改成别的名字就行了),

创建文件com.alibaba.csp.sentinel.init.InitFunc并把FileDataSourceInit.java的原路径copy进去

图片描述




推模式

参考大目老师的手记(手把手)推模式

图片描述

sentinel控制台推送规则的时候不再调用调用微服务的通信API,而是将规则推送到nacos上(这里nacos作为配置中心),微服务通过连接nacos,就会获取nacos上的规则,同时微服务也会监听nacos上的数据变更,nacos数据发生变化就会更新sentinel规则


因为推模式配置很繁琐,可以使用大目老师的懒人包

https://github.com/eacdy/Sentinel-Dashboard-Nacos/releases


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2894/viewspace-2797027/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-12-22

  • 博文量
    73
  • 访问量
    33676