(十五)docker安装sentinel,客户端配置规则本地持久化

 一、简介

操作系统:Linux  CentOS 7.3 64位

docker版本:19.03.8

sentinel版本:1.8.0

二、实践

1、拉取镜像

docker pull bladex/sentinel-dashboard:1.8.0

(十五)docker安装sentinel,客户端配置规则本地持久化_第1张图片

 (十五)docker安装sentinel,客户端配置规则本地持久化_第2张图片

2、运行容器

docker run --name sentinel \
-p 8858:8858 \
--privileged=true \
--restart=always \
-d bladex/sentinel-dashboard:1.8.0

(十五)docker安装sentinel,客户端配置规则本地持久化_第3张图片

 (十五)docker安装sentinel,客户端配置规则本地持久化_第4张图片

3.访问sentinel

http://192.168.121.132:8858/

账号密码默认都是sentinel

(十五)docker安装sentinel,客户端配置规则本地持久化_第5张图片

三、客户端配置规则本地持久化

sentinel配置的规则默认是存在内存里的,不够稳定,所以我们需要持久化到本地文件中。

1.新建持久化处理类

在我们连接sentinel的springboot项目客户端中新增持久化处理类。

package com.example.config;

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;

//sentinel规则持久化
public class SentinelRulesPersistence implements InitFunc {

    private String appcationName = "order";

    @Override
    public void init() throws Exception {
        String ruleDir = System.getProperty("user.home") + "/sentinel-rules/" + appcationName;
        System.out.println(ruleDir);
        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.register2Property(flowRuleRDS.getProperty());
        WritableDataSource> flowRuleWDS = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
        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);
    }
}

2.新增配置文件指向我们的持久化处理类

在resources文件夹创建目录META-INF/services,然后添加文件

com.alibaba.csp.sentinel.init.InitFunc

没有后缀格式。

(十五)docker安装sentinel,客户端配置规则本地持久化_第6张图片

 文件内容为持久化处理类的全路径,例如:

com.example.config.SentinelRulesPersistence

3.配置规则查看效果

新增一条流控规则。

(十五)docker安装sentinel,客户端配置规则本地持久化_第7张图片

然后打开客户端的user.home目录(不同电脑路径不一样)。

(十五)docker安装sentinel,客户端配置规则本地持久化_第8张图片

你可能感兴趣的:(docker,docker,sentinel,sentinel规则持久化)