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

 一、简介

操作系统:Linux  CentOS 7.3 64位

docker版本:19.03.8

sentinel版本:1.8.0

二、实践

1、拉取镜像

docker pull bladex/sentinel-dashboard:1.8.0

 

2、运行容器

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

 

3.访问sentinel

http://192.168.121.132:8858/

账号密码默认都是sentinel

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

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<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(flowRulePath, flowRuleListParser);
        FlowRuleManager.register2Property(flowRuleRDS.getProperty());
        WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
        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<>(authorityRulePath, 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()) {
            file.createNewFile();
        }
    }

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

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

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

com.alibaba.csp.sentinel.init.InitFunc

没有后缀格式。

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

com.example.config.SentinelRulesPersistence

3.配置规则查看效果

新增一条流控规则。

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Windows上安装Docker可以按照以下步骤进行操作: 1. 首先,确保你的Windows版本是Windows 10或者Windows Server 2016以上,并且开启了Hyper-V虚拟化功能。 2. 前往Docker官网(https://www.docker.com/)下载Docker Desktop for Windows安装程序。 3. 运行安装程序,按照提示进行安装安装完成后,Docker会自动启动。 4. 在系统托盘中找到Docker图标,右键点击选择"Settings",进入设置界面。 5. 在设置界面中,可以配置Docker的一些选项,例如镜像加速器、资源限制等。根据需要进行配置。 6. 完成配置后,点击"Apply & Restart"按钮,Docker会重新启动应用配置。 至此,你已经成功在Windows上安装Docker。 接下来,如果你想在Docker安装数据库,可以按照以下步骤进行操作: 1. 打开Docker Desktop应用,确保Docker已经启动。 2. 在命令行或者终端中输入以下命令来搜索并下载数据库的Docker镜像: ``` docker search 数据库名称 ``` 3. 选择一个合适的镜像,并使用以下命令来下载该镜像: ``` docker pull 镜像名称 ``` 4. 下载完成后,使用以下命令来创建并运行一个数据库容器: ``` docker run --name 容器名称 -e 环境变量 镜像名称 ``` 其中,容器名称是你给容器起的一个名字,环境变量是数据库的配置信息,镜像名称是你下载的数据库镜像的名称。 5. 容器创建成功后,你可以使用以下命令来管理容器: - 启动容器:`docker start 容器名称` - 停止容器:`docker stop 容器名称` - 进入容器:`docker exec -it 容器名称 bash` 通过以上步骤,你可以在Docker中成功安装和运行数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值