sentinel规则持久化-规则同步nacos-最标准配置

这篇具有很好参考价值的文章主要介绍了sentinel规则持久化-规则同步nacos-最标准配置。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

官方参考文档:

动态规则扩展 · alibaba/Sentinel Wiki · GitHub

需要修改的代码如下:

sentinel规则持久化-规则同步nacos-最标准配置,微服务,java,sentinel,java

为了便于后续版本集成nacos,简单讲一下集成思路

1.更改pom

修改sentinel-datasource-nacos的范围

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

改为

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

2.拷贝示例

将test目录下的com.alibaba.csp.sentinel.dashboard.rule.nacos包下的内容拷贝到src的 com.alibaba.csp.sentinel.dashboard.rule的目录

test目录只包含限流,其他规则参照创建即可。

创建时注意修改常量,并且在NacosConfig实现各种converter

注意:授权规则和热点规则需要特殊处理,否则nacos配置不生效。

因为授权规则Entity比流控规则Entity多包了一层。

public class FlowRuleEntity implements RuleEntity 
public class AuthorityRuleEntity extends AbstractRuleEntity<AuthorityRule>

以授权规则为例

AuthorityRuleNacosProvider.java

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Component("authorityRuleNacosProvider")
public class AuthorityRuleNacosProvider implements DynamicRuleProvider<List<AuthorityRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<String, List<AuthorityRuleEntity>> converter;

    @Override
    public List<AuthorityRuleEntity> getRules(String appName) throws Exception {
        String rules = configService.getConfig(appName + NacosConfigUtil.AUTHORITY_DATA_ID_POSTFIX,
                NacosConfigUtil.GROUP_ID, 3000);
        if (StringUtil.isEmpty(rules)) {
            return new ArrayList<>();
        }
        return converter.convert(this.parseRules(rules));
    }

    private String parseRules(String rules) {
        JSONArray newRuleJsons = new JSONArray();
        JSONArray ruleJsons = JSONArray.parseArray(rules);
        for (int i = 0; i < ruleJsons.size(); i++) {
            JSONObject ruleJson = ruleJsons.getJSONObject(i);
            AuthorityRuleEntity ruleEntity = JSON.parseObject(ruleJson.toJSONString(), AuthorityRuleEntity.class);
            JSONObject newRuleJson = JSON.parseObject(JSON.toJSONString(ruleEntity));
            AuthorityRule rule = JSON.parseObject(ruleJson.toJSONString(), AuthorityRule.class);
            newRuleJson.put("rule", rule);
            newRuleJsons.add(newRuleJson);
        }
        return newRuleJsons.toJSONString();
    }
}

AuthorityRuleNacosPublisher.java

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Component("authorityRuleNacosPublisher")
public class AuthorityRuleNacosPublisher implements DynamicRulePublisher<List<AuthorityRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<List<AuthorityRuleEntity>, String> converter;

    @Override
    public void publish(String app, List<AuthorityRuleEntity> rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        configService.publishConfig(app + NacosConfigUtil.AUTHORITY_DATA_ID_POSTFIX,
            NacosConfigUtil.GROUP_ID,  this.parseRules(converter.convert(rules)));
    }

    private String parseRules(String rules) {
        JSONArray oldRuleJsons = JSONArray.parseArray(rules);
        for (int i = 0; i < oldRuleJsons.size(); i++) {
            JSONObject oldRuleJson = oldRuleJsons.getJSONObject(i);
            JSONObject ruleJson = oldRuleJson.getJSONObject("rule");
            oldRuleJson.putAll(ruleJson);
            oldRuleJson.remove("rule");
        }
        return oldRuleJsons.toJSONString();
    }
}

热点规则同理

3.修改controller

v2目录的FlowControllerV2

 @Autowired
    @Qualifier("flowRuleDefaultProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleDefaultPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

改为

 @Autowired
    @Qualifier("flowRuleNacosProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleNacosPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

controller目录的其他controller包括

AuthorityRuleController DegradeController FlowControllerV1 ParamFlowRuleController SystemController

做如下更改(以DegradeController为例)

@Autowired
    private SentinelApiClient sentinelApiClient;

改成

@Autowired
    @Qualifier("degradeRuleNacosProvider")
    private DynamicRuleProvider<List<DegradeRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("degradeRuleNacosPublisher")
    private DynamicRulePublisher<List<DegradeRuleEntity>> rulePublisher;

将原有publishRules方法删除,统一改成

 private void publishRules(/*@NonNull*/ String app) throws Exception {
        List<DegradeRuleEntity> rules = repository.findAllByApp(app);
        rulePublisher.publish(app, rules);
    }

之后解决报错的地方即可。

获取所有rules的地方

 List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port);

改成

List<DegradeRuleEntity> rules = ruleProvider.getRules(app);

原有调用publishRules方法的地方,删除掉

在上一个try catch方法里加上

publishRules(entity.getApp());

这里的entity.getApp()也有可能是oldEntity.getApp()/app等变量。根据删除的publishRules代码片段推测即可。

4.修改前端文件

文件路径:src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html

 <a ui-sref="dashboard.flowV1({app: entry.app})">

改成

 <a ui-sref="dashboard.flow({app: entry.app})">

5.最后打包即可

执行命令打包成jar

mvn clean package

运行方法与官方jar一致,不做赘述

6.微服务程序集成

pom.xml添加

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

配置文件application.yml添加

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

7.测试验证

在sentinel控制台界面添加几个流控规则后尝试关闭微服务和sentinel,然后重新打开sentinel和微服务,看流控规则是否还在。

8.最后把配置好的代码发给大家,大家可以自行下载,里边有运行访问流程

https://download.csdn.net/download/qq_34091529/88482757文章来源地址https://www.toymoban.com/news/detail-726874.html

到了这里,关于sentinel规则持久化-规则同步nacos-最标准配置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Sentinel授权规则和规则持久化

    大家好我是苏麟 , 今天说说Sentinel规则持久化. 授权规则 授权规则可以对请求方来源做判断和控制。 授权规则 基本规则 授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式。 白名单:来源(origin)在白名单内的调用者允许访问 黑名单:来源(origin)在黑名单内

    2024年02月08日
    浏览(37)
  • Java----Sentinel持久化规则启动

    java -jar -Dnacos.add=8848 你的sentinel源码修改包.jar 前期准备: 1.引入依赖 在order-service中引入sentinel监听nacos的依赖: 2.配置nacos地址 在order-service中的application.yml文件配置nacos地址及监听的配置信息: 3.启动 启动方式跟官方一样: java -jar -Dserver.port=8180 sentinel-dashboard.jar 如果要修改

    2024年02月11日
    浏览(26)
  • sentinel整合nacos配置中心持久化

    在网上找了很多的资料,发现sentinel整合nacos持久化的博文和视频大多数都只有改造限流部分的教程,并且都需要修改前端,略显麻烦,至于剩下的熔断、热点流控、授权的更是没有相关的改造教程,最后在知乎的看到一篇文章后让我大受启发 这位前辈讲到sentinel原来是把配置

    2024年02月04日
    浏览(40)
  • Sentinel控制台配置 持久化到nacos

      sentinel控制台,使用方便,功能强大。使用官方的jar包,配置不会持久化,sentinel重启后会导致,之前的规则全部丢失,下面一起改造源码实现规则数据的持久化 (github访问太慢,直接上镜像版) Sentinel: Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越来

    2024年02月09日
    浏览(28)
  • (十五)docker安装sentinel,客户端配置规则本地持久化

    操作系统: Linux  CentOS 7.3 64位 docker版本: 19.03.8 sentinel版本: 1.8.0 1、拉取镜像   2、运行容器   3.访问sentinel http://192.168.121.132:8858/ 账号密码默认都是sentinel ​ sentinel配置的规则默认是存在内存里的,不够稳定,所以我们需要持久化到本地文件中。 1.新建持久化处理类 在我

    2024年02月08日
    浏览(37)
  • Spring Cloud Alibaba全家桶(八)——Sentinel规则持久化

    本文小新为大家带来 Sentinel规则持久化 相关知识,具体内容包括, Sentinel规则推送三种模式 介绍,包括: 原始模式 , 拉模式 , 推模式 ,并对 基于Nacos配置中心控制台实现推送 进行详尽介绍~ 不积跬步,无以至千里;不积小流,无以成江海。每天进步一点点,在成为强者

    2024年01月25日
    浏览(28)
  • Sentinel-Dashboard-1.8持久化Nacos

    Sentinel-Dashboard-1.8持久化Nacos 一、客户端改造 1.引入pom.xml文件依赖 2.配置application.yml文件。 客户端改造完成 二、Sentinel-Dashboard源码改造 1.修改pom.xml中nacos的范围中【test】注释。 2.把test目录下com/alibaba/csp/sentinel/dashboard/rule下的nacos文件夹复制到main相同目录下 3.修改application.

    2024年02月04日
    浏览(24)
  • Spring Cloud Sentinel整合Nacos实现配置持久化

    sentinel配置相关配置后无法持久化,服务重启之后就没了,所以整合nacos,在nacos服务持久化,sentinel实时与nacos通信获取相关配置。 使用上一章节Feign消费者服务实现整合。 版本信息: nacos:1.4.1 Sentinel 控制台 1.7.2 spring-boot:2.3.3.RELEASE spring.cloud.version:Hoxton.SR8 spring.cloud.alibaba.v

    2024年02月08日
    浏览(44)
  • SpringCloudAlibaba微服务实战系列(五)Sentinel1.8.5+Nacos持久化

    前面介绍Sentinel的流控、熔断降级等功能,同时Sentinel应用也在面临着一个问题:我们在Sentinel后台管理界面中配置了一堆流控、降级规则,但是Sentinel一重启,这些规则全部消失了。那么我们就要考虑Sentinel的持久化问题。 Sentinel为我们提供了几种持久化的解决方案: 存储到

    2024年02月10日
    浏览(40)
  • Sentinel nacos spring cloud 持久化配置---分布式/微服务流量控制

    下载地址:https://github.com/alibaba/Sentinel/releases 本次版本:1.8.6 上一篇文章已介绍 我们先说目标,为各位看官节省不匹配的时间 0、使用sentinel流控中心 1、使用nacos做配置中心 5、使用spring-cloud-starter-alibaba-sentinel做持久化配置 https://github.com/OrderDong/microservice-boot 分支:microserv

    2024年02月16日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包