sentinel 1.8.2 持久化Nacos动态规则热点规则和授权规则不生效的问题

问题:规则持久化到nacos之后,我在本地测试时候只有热点和授权规则不生效,我慢慢的在客户端sentinel-csp-nacos源码下然后推送之后BUG源码 一步一步查看,发现 ParamFlowRuleManager 类中map中一直没有规则,慢慢发现里面是实体类ParamFlowRule数据就是初始化类数据,这时候我继续看修改和查看 RulePropertyListener监听器 监听到了 但是在ParamFlowRuleManager.configUpdateconfigLoad 方法中buildParamRuleMap时候一直返回数据为空。这时候我觉得是不是版本有问题,当时的时候控制台1.8.0版本,

客户端是1.7.1版本,这时候我更换了本地客户端所有的cloudAlibaba boot sentinel 版本。发现还是失效,这时候我就拿出来nacos里面数据结构和客户端数据结构对比。数据结构不一致。查看sentinel-dashboard在自定义ParamFlowRuleNacosPublisher时候 推送的数据是ParamFlowRuleEntity。 客户端接收的ParamFlowRule类。这时候就需要改一下。

接下来~~~~~~~~~~~~~~~~~~~~~~~~开始

解决方案:

重写ParamFlowRuleEntity与AuthorityRuleEntity

1.热点参数规则

第一步重写ParamFlowRuleEntity
import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;import java.util.*;/*** @author 阿志有点坑* @description* @date 2021/2/4 10:04*/
public class ParamFlowRuleCorrectEntity implements RuleEntity {private Long id;private String app;private String ip;private Integer port;private String limitApp;private String resource;private Date gmtCreate;private int grade = 1;private Integer paramIdx;private double count;private int controlBehavior = 0;private int maxQueueingTimeMs = 0;private int burstCount = 0;private long durationInSec = 1L;private List<ParamFlowItem> paramFlowItemList = new ArrayList();private Map<Object, Integer> hotItems = new HashMap();private boolean clusterMode = false;private ParamFlowClusterConfig clusterConfig;public int getGrade() {return grade;}public void setGrade(int grade) {this.grade = grade;}public Integer getParamIdx() {return paramIdx;}public void setParamIdx(Integer paramIdx) {this.paramIdx = paramIdx;}public double getCount() {return count;}public void setCount(double count) {this.count = count;}public int getControlBehavior() {return controlBehavior;}public void setControlBehavior(int controlBehavior) {this.controlBehavior = controlBehavior;}public int getMaxQueueingTimeMs() {return maxQueueingTimeMs;}public void setMaxQueueingTimeMs(int maxQueueingTimeMs) {this.maxQueueingTimeMs = maxQueueingTimeMs;}public int getBurstCount() {return burstCount;}public void setBurstCount(int burstCount) {this.burstCount = burstCount;}public long getDurationInSec() {return durationInSec;}public void setDurationInSec(long durationInSec) {this.durationInSec = durationInSec;}public List<ParamFlowItem> getParamFlowItemList() {return paramFlowItemList;}public void setParamFlowItemList(List<ParamFlowItem> paramFlowItemList) {this.paramFlowItemList = paramFlowItemList;}public Map<Object, Integer> getHotItems() {return hotItems;}public void setHotItems(Map<Object, Integer> hotItems) {this.hotItems = hotItems;}public boolean isClusterMode() {return clusterMode;}public void setClusterMode(boolean clusterMode) {this.clusterMode = clusterMode;}public ParamFlowClusterConfig getClusterConfig() {return clusterConfig;}public void setClusterConfig(ParamFlowClusterConfig clusterConfig) {this.clusterConfig = clusterConfig;}@Overridepublic Date getGmtCreate() {return gmtCreate;}public void setGmtCreate(Date gmtCreate) {this.gmtCreate = gmtCreate;}@Overridepublic Long getId() {return id;}@Overridepublic void setId(Long id) {this.id = id;}@Overridepublic String getApp() {return app;}public void setApp(String app) {this.app = app;}@Overridepublic String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}@Overridepublic Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}public String getLimitApp() {return limitApp;}public void setLimitApp(String limitApp) {this.limitApp = limitApp;}public String getResource() {return resource;}public void setResource(String resource) {this.resource = resource;}@Overridepublic Rule toRule(){ParamFlowRule rule=new ParamFlowRule();return rule;}
}
第二步:在NacosConfig添加对应的类型转换配置
@Bean
public Converter<List<ParamFlowRuleCorrectEntity>, String> paramFlowRuleCorrectEntityEncoder() {return JSON::toJSONString;
}@Bean
public Converter<String, List<ParamFlowRuleCorrectEntity>> paramFlowRuleCorrectEntityDecoder() {return s -> JSON.parseArray(s, ParamFlowRuleCorrectEntity.class);
}
第三步:修改对应的ParamFlowRuleNacosProvider

package com.alibaba.csp.sentinel.dashboard.rule.nacos.param;import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleCorrectEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
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.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;/*** @author 阿志有点坑* @since 1.4.0*/
@Component("paramFlowRuleNacosProvider")
public class ParamFlowRuleNacosProvider implements DynamicRuleProvider<List<ParamFlowRuleEntity>> {@Autowiredprivate ConfigService configService;@Autowiredprivate Converter<String, List<ParamFlowRuleCorrectEntity>> converter;@Overridepublic List<ParamFlowRuleEntity> getRules(String appName) throws Exception {String rules = configService.getConfig(appName + NacosConfigUtil.PARAM_FLOW_DATA_ID_POSTFIX,NacosConfigUtil.GROUP_ID, 3000);if (StringUtil.isEmpty(rules)) {return new ArrayList<>();}List<ParamFlowRuleCorrectEntity> entityList = converter.convert(rules);entityList.forEach(e -> e.setApp(appName));return entityList.stream().map(rule -> {ParamFlowRule paramFlowRule = new ParamFlowRule();BeanUtils.copyProperties(rule, paramFlowRule);ParamFlowRuleEntity entity = ParamFlowRuleEntity.fromParamFlowRule(rule.getApp(), rule.getIp(), rule.getPort(), paramFlowRule);entity.setId(rule.getId());entity.setGmtCreate(rule.getGmtCreate());return entity;}).collect(Collectors.toList());}
}
第四步:修改对应的NacosPublisher
/** 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.param;import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleCorrectEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
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.nacos.api.config.ConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.stream.Collectors;/*** @author Eric Zhao* @since 1.4.0*/
@Component("paramFlowRuleNacosPublisher")
public class ParamFlowRuleNacosPublisher implements DynamicRulePublisher<List<ParamFlowRuleEntity>> {@Autowiredprivate ConfigService configService;@Autowiredprivate Converter<List<ParamFlowRuleCorrectEntity>, String> converter;@Overridepublic void publish(String app, List<ParamFlowRuleEntity> rules) throws Exception {AssertUtil.notEmpty(app, "app name cannot be empty");if (rules == null) {return;}rules.forEach(e -> e.setApp(app));//  转换List<ParamFlowRuleCorrectEntity> list = rules.stream().map(rule -> {ParamFlowRuleCorrectEntity entity = new ParamFlowRuleCorrectEntity();BeanUtils.copyProperties(rule, entity);return entity;}).collect(Collectors.toList());configService.publishConfig(app + NacosConfigUtil.PARAM_FLOW_DATA_ID_POSTFIX,NacosConfigUtil.GROUP_ID, converter.convert(list));}}

授权规则也一样.

2.权限规则

第一步:重写AuthorityRuleEntity
package com.alibaba.csp.sentinel.dashboard.datasource.entity.rule;import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;import java.util.Date;/**- @author chengmeng- @description- @date 2021/2/4 14:08*/public class AuthorityRuleCorrectEntity implements RuleEntity{private Long id;private String app;private String ip;private Integer port;private String limitApp;private String resource;private Date gmtCreate;private Date gmtModified;private int strategy;@Overridepublic Long getId() {return id;}@Overridepublic void setId(Long id) {this.id = id;}@Overridepublic String getApp() {return app;}public void setApp(String app) {this.app = app;}@Overridepublic String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}@Overridepublic Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}public String getLimitApp() {return limitApp;}public void setLimitApp(String limitApp) {this.limitApp = limitApp;}public String getResource() {return resource;}public void setResource(String resource) {this.resource = resource;}@Overridepublic Date getGmtCreate() {return gmtCreate;}public void setGmtCreate(Date gmtCreate) {this.gmtCreate = gmtCreate;}public Date getGmtModified() {return gmtModified;}public void setGmtModified(Date gmtModified) {this.gmtModified = gmtModified;}public int getStrategy() {return strategy;}public void setStrategy(int strategy) {this.strategy = strategy;}@Overridepublic Rule toRule(){AuthorityRule rule=new AuthorityRule();return rule;}}
@Bean
public Converter<java.util.List<AuthorityRuleCorrectEntity>, String> authorityRuleCorrectEntityEncoder() {return JSON::toJSONString;
}@Bean
public Converter<String, java.util.List<AuthorityRuleCorrectEntity>> authorityRuleCorrectEntityDecoder() {return s -> JSON.parseArray(s, AuthorityRuleCorrectEntity.class);
}

1.2.3.4 步和热点一样

总结:

sentinel 在往nacos推送时候转换成客户端想要的格式。在控制台返回时候将其自定义的类转换成sentinel想要的格式。就完事了

sentinel 1.8. 2持久化Nacos动态规则热点规则和授权规则不生效的问题相关推荐

  1. Sentinel授权规则及规则持久化

    授权规则 授权规则可以对请求方来源做判断和控制. 授权规则 基本规则 授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式. 白名单:来源(origin)在白名单内的调用者允许访问 黑名单:来源 ...

  2. sentinel 不显示项目_Sentinel+Nacos实现资源流控、降级、热点、授权

    本文同名博客老炮说Java:https://www.laopaojava.com/,每天更新Spring/SpringMvc/SpringBoot/实战项目等文章资料 Sentinel+Nacos 是 ...

  3. Sentinel-Dashboard-1.8持久化Nacos全网最详细讲解(包含客户端)

    耗时2天(我的元旦假期啊-),针对Sentinel-Dashboard1.8的源码进行修改. 目前已打包并上传CSDN资源.如果你不想看这长篇大论的话. Jar包资源地址:https://downlo ...

  4. Sentinel之限流、降级、系统保护、热点、授权规则

    简介 Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度来帮助用户保护服务的稳定性. 本文主要讲限 ...

  5. Day239.RBAC模式、动态加载用户权限资源规则数据规则、【记住我】注销多次登录图片验证码session验证码验证功能 -springsecurity-jwt-oauth2

    1.RBAC权限管理模型 一.RBAC权限模型简介 RBAC权限模型(Role-Based Access Control)即:基于角色的权限控制.模型中有几个关键的术语: 用户:系统接口及功能访问的操 ...

  6. springcloud gateway 使用nacos 动态过滤器 记一次线上网关升级cpu升高的问题

    大家好,我是烤鸭: ​ 网关升级,想使用 springcloud gateway nacos 动态过滤器配置(原来是硬编码的方式),升级之后出了一些问题(cpu升高,ygc频繁),记录一下. 关于 s ...

  7. drools动态增加、修改、删除规则

    文章目录 1.背景 2.前置知识 1.如何动态构建出一个`kmodule.xml`文件 2.kmodule.xml应该被谁加载 3.我们drl规则内容如何加载 4.动态构建KieContainer 3 ...

  8. oracle 更新参数,Oracle动态、静态参数参数修改规则

    首先,查看要修改的oracle参数的属性(动态or静态) SQL> select name,value,isses_modifiable,issys_modifiable from V$PARA ...

  9. Sentinel Go 0.4.0 发布,支持热点流量防护能力

    Sentinel 是阿里巴巴开源的,面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流.流量整形.熔断降级.系统自适应保护等多个维度来帮助开发者保障微服务的稳定性.Sentinel 承接了阿 ...

最新文章

  1. 一个锁等待现象的诊断案例
  2. Redis_基本类型介绍和指令___2
  3. 当 JS 工程师遇到了 TypeScript 会发生什么?
  4. 标准Android按钮具有不同的颜色
  5. prototype.js开发笔记(转)
  6. vue-router个人总结
  7. 智能驾驶场景库设计方法-V2X
  8. ddr3ddr4 lpddr4速率_Ddr2,ddr3,ddr4内存条的读写速率
  9. mysql-mmm高可用群集
  10. html页面数据的维护
  11. Causality Inspired Representation Learning for Domain Generalization 阅读笔记
  12. 从头开始训练一个依存分析器
  13. 阿里技术类面试真题,你能做对几个?(含答案)
  14. VMware Vsphere-下
  15. java简单的增删改查项目 ATM机
  16. 图论复习之强连通分量以及缩点—Tarjan算法
  17. 速学堂 JAVA 300集 第四章练习
  18. 用Blender绘制简单地图
  19. 轨道姿态常用编程缩写
  20. 德州仪器推出3D霍尔效应位置传感器;天旦亮相2021中国IPv6创新发展大会;Amazfit发布全新品牌标志 | 全球TMT...

热门文章

  1. 解决小新pro13打开软件后软件字体太小问题
  2. 使用WSDD搭建webservice
  3. 【Google Chart Tools: Infographics】谷歌图表工具:信息图表
  4. 国产红芯浏览器免费下载,红芯企业浏览器免费下载
  5. 微信小程序展示长图与pdf
  6. 计算机毕业设计之基于微信小程序的优惠券领取系统
  7. 开源多商户商城源码代码分析
  8. 速知!PMI-ACP认证是什么?
  9. ubuntu 20.04安装wps,并安装缺失字体
  10. 实验三 LZW编解码算法实现与分析