首页 > 编程语言 >Java学习-Sentinel 1.8.4 规则持久化到Nacos

Java学习-Sentinel 1.8.4 规则持久化到Nacos

时间:2024-05-31 15:30:48浏览次数:28  
标签:null return rules 1.8 nacos Nacos 化到 Result entity

文章目录

一、前言

在生产环境中使用 Sentinel

在这里插入图片描述

下面使用Push模式:配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 数据源 → Sentinel
在这里插入图片描述

二、快速体验

1、部署sentinel

小编已经基于Sentinel 1.8.4版本构建好一个docker镜像,修改下相应nacos配置信息即可运行体验一下
在这里插入图片描述

# 环境准备
git clone https://gitee.com/zhengqingya/docker-compose.git
cd docker-compose/Liunx
# 运行
# docker-compose -f docker-compose-sentinel-nacos.yml -p sentinel up -d
2、SpringCloud中规则持久化到nacos

添加依赖

<!-- Sentinel规则持久化至Nacos -->
<dependency>
   <groupId>com.alibaba.csp</groupId>
   <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

添加配置

spring:
  application:
    name: demo # 应用名称
  cloud:
    sentinel:
      enabled: true # 自动化配置是否生效
      eager: true   # 禁用控制台懒加载
      transport:
        dashboard: www.zhengqingya.com:8858 # 控制台地址
        client-ip: ${spring.cloud.client.ip-address} # 获取本机IP地址
        port: 18719 # 启动该服务,会在应用程序的相应服务器上启动HTTP Server,并且该服务器将与Sentinel dashboard进行交互
      # ============== ↓↓↓↓↓↓ 增加规则持久化配置到nacos ↓↓↓↓↓↓ ==============
      datasource:
        # 流控规则
        flow:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            username: ${spring.cloud.nacos.config.username}
            password: ${spring.cloud.nacos.config.password}
            namespace: ${spring.cloud.nacos.config.namespace}
            group-id: sentinel-group
            data-id: ${spring.application.name}-sentinel-flow-rules
            # 规则类型:flow、degrade、param-flow、system、authority
            rule-type: flow
        # 熔断降级
        degrade:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            username: ${spring.cloud.nacos.config.username}
            password: ${spring.cloud.nacos.config.password}
            namespace: ${spring.cloud.nacos.config.namespace}
            group-id: sentinel-group
            data-id: ${spring.application.name}-sentinel-degrade-rules
            rule-type: degrade
        # 热点规则
        param-flow:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            username: ${spring.cloud.nacos.config.username}
            password: ${spring.cloud.nacos.config.password}
            namespace: ${spring.cloud.nacos.config.namespace}
            group-id: sentinel-group
            data-id: ${spring.application.name}-sentinel-param-flow-rules
            rule-type: param-flow
        # 系统规则
        system:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            username: ${spring.cloud.nacos.config.username}
            password: ${spring.cloud.nacos.config.password}
            namespace: ${spring.cloud.nacos.config.namespace}
            group-id: sentinel-group
            data-id: ${spring.application.name}-sentinel-system-rules
            rule-type: system
        # 授权规则
        authority:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            username: ${spring.cloud.nacos.config.username}
            password: ${spring.cloud.nacos.config.password}
            namespace: ${spring.cloud.nacos.config.namespace}
            group-id: sentinel-group
            data-id: ${spring.application.name}-sentinel-authority-rules
            rule-type: authority
3、sentinel控制台操作测试

ex: 在sentinel控制台中添加流控规则会自动在nacos中创建相应配置文件xxx-sentinel-flow-rulesxxx-sentinel-flow-rules-sentinel-dashboard,用于规则持久化存储数据,sentinel重启或我们的应用app重启,配置都会一直在!
在这里插入图片描述
在这里插入图片描述

三、sentinel-dashboard源码修改

拉取源码 https://github.com/alibaba/Sentinel,进入sentinel-dashboard模块

1、pom.xml中添加依赖
<!--添加Nacos配置中心依赖 -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>0.2.10</version>
</dependency>

sentinel1.8.4引入springboot版本为2.5.12,启动项目会报错Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
原因:在springboot2.4之后删掉了ConfigurationBeanFactoryMetadata
解决:将版本降至2.3.9.RELEASE

2、application.properties中添加nacos配置
# ============== ↓↓↓↓↓↓ 增加配置 -> 规则持久化到nacos ↓↓↓↓↓↓ ==============
server.port=8858
nacos.server-addr=127.0.0.1:8848
nacos.namespace=dev
nacos.group=sentinel-group
nacos.username=nacos
nacos.password=nacos
3、nacos配置
新增NacosConfig
@Configuration
public class NacosConfig {
<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${nacos.server-addr}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> <span class="token class-name">String</span> serverAddr<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${nacos.namespace}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> <span class="token class-name">String</span> namespace<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${nacos.group}"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> group<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${nacos.username}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> <span class="token class-name">String</span> username<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${nacos.password}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> <span class="token class-name">String</span> password<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> <span class="token class-name">ConfigService</span> <span class="token function">nacosConfigService</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token class-name">Properties</span> properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token class-name">PropertyKeyConst</span><span class="token punctuation">.</span>SERVER_ADDR<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>serverAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
    properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token class-name">PropertyKeyConst</span><span class="token punctuation">.</span>NAMESPACE<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>namespace<span class="token punctuation">)</span><span class="token punctuation">;</span>

// properties.put(PropertyKeyConst.GROUP, this.group);
properties.put(PropertyKeyConst.USERNAME, this.username);
properties.put(PropertyKeyConst.PASSWORD, this.password);
return ConfigFactory.createConfigService(properties);
}
}

新增NacosConfigUtil
@Component
public final class NacosConfigUtil {
<span class="token annotation punctuation">@Resource</span>
<span class="token keyword">private</span> <span class="token class-name">NacosConfig</span> nacosConfig<span class="token punctuation">;</span>

<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> GROUP_ID <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@PostConstruct</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    GROUP_ID <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>nacosConfig<span class="token punctuation">.</span>group<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> FLOW_DATA_ID_POSTFIX <span class="token operator">=</span> <span class="token string">"-sentinel-flow-rules"</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> DEGRADE_DATA_ID_POSTFIX <span class="token operator">=</span> <span class="token string">"-sentinel-degrade-rules"</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> SYSTEM_DATA_ID_POSTFIX <span class="token operator">=</span> <span class="token string">"-sentinel-system-rules"</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> PARAM_FLOW_DATA_ID_POSTFIX <span class="token operator">=</span> <span class="token string">"-sentinel-param-flow-rules"</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> AUTHORITY_DATA_ID_POSTFIX <span class="token operator">=</span> <span class="token string">"-sentinel-authority-rules"</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> DASHBOARD_POSTFIX <span class="token operator">=</span> <span class="token string">"-sentinel-dashboard"</span><span class="token punctuation">;</span>


<span class="token keyword">private</span> <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 将规则序列化成JSON文本,存储到Nacos server中
 *
 * @param configService nacos config service
 * @param app           应用名称
 * @param postfix       规则后缀 eg.NacosConfigUtil.FLOW_DATA_ID_POSTFIX
 * @param rules         规则对象
 * @throws NacosException 异常
 */</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">void</span> <span class="token function">setRuleStringToNacos</span><span class="token punctuation">(</span><span class="token class-name">ConfigService</span> configService<span class="token punctuation">,</span> <span class="token class-name">String</span> app<span class="token punctuation">,</span> <span class="token class-name">String</span> postfix<span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> rules<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">NacosException</span> <span class="token punctuation">{</span>
    <span class="token class-name">AssertUtil</span><span class="token punctuation">.</span><span class="token function">notEmpty</span><span class="token punctuation">(</span>app<span class="token punctuation">,</span> <span class="token string">"app name cannot be empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>rules <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Rule</span><span class="token punctuation">&gt;</span></span> ruleForApp <span class="token operator">=</span> rules<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>rule <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
                <span class="token class-name">RuleEntity</span> rule1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">RuleEntity</span><span class="token punctuation">)</span> rule<span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>rule1<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">Rule</span> rule2 <span class="token operator">=</span> rule1<span class="token punctuation">.</span><span class="token function">toRule</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>rule2<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> rule2<span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">String</span> dataId <span class="token operator">=</span> <span class="token function">genDataId</span><span class="token punctuation">(</span>app<span class="token punctuation">,</span> postfix<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/**
     * 俩种存储只是入参不同,为了满足功能的实现,存入nacos后,会有俩个配置,以后继续完善
     */</span>
    <span class="token comment">// 存储,控制微服务使用,即可以起到拦截作用,但是由于无法显示到控制台</span>
    configService<span class="token punctuation">.</span><span class="token function">publishConfig</span><span class="token punctuation">(</span>
            dataId<span class="token punctuation">,</span>
            <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span>GROUP_ID<span class="token punctuation">,</span>
            JSON<span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>ruleForApp<span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 存储,给控制台显示使用,由于数据太多,会出现转化异常,虽然可以提供控制台显示,但是无法对微服务进行保护</span>
    configService<span class="token punctuation">.</span><span class="token function">publishConfig</span><span class="token punctuation">(</span>
            dataId <span class="token operator">+</span> DASHBOARD_POSTFIX<span class="token punctuation">,</span>
            <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span>GROUP_ID<span class="token punctuation">,</span>
            JSON<span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>rules<span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 从Nacos server中查询响应规则,并将其反序列化成对应Rule实体
 *
 * @param configService nacos config service
 * @param appName       应用名称
 * @param postfix       规则后缀 eg.NacosConfigUtil.FLOW_DATA_ID_POSTFIX
 * @param clazz         类
 * @param &lt;T&gt;           泛型
 * @return 规则对象列表
 * @throws NacosException 异常
 */</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token function">getRuleEntitiesFromNacos</span><span class="token punctuation">(</span><span class="token class-name">ConfigService</span> configService<span class="token punctuation">,</span> <span class="token class-name">String</span> appName<span class="token punctuation">,</span> <span class="token class-name">String</span> postfix<span class="token punctuation">,</span> <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> clazz<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">NacosException</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> rules <span class="token operator">=</span> configService<span class="token punctuation">.</span><span class="token function">getConfig</span><span class="token punctuation">(</span>
            <span class="token function">genDataId</span><span class="token punctuation">(</span>appName<span class="token punctuation">,</span> postfix<span class="token punctuation">)</span> <span class="token operator">+</span> DASHBOARD_POSTFIX<span class="token punctuation">,</span>
            <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span>GROUP_ID<span class="token punctuation">,</span>
            <span class="token number">3000</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>rules<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> JSON<span class="token punctuation">.</span><span class="token function">parseArray</span><span class="token punctuation">(</span>rules<span class="token punctuation">,</span> clazz<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">genDataId</span><span class="token punctuation">(</span><span class="token class-name">String</span> appName<span class="token punctuation">,</span> <span class="token class-name">String</span> postfix<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> appName <span class="token operator">+</span> postfix<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

4、举例持久化流控规则

tips: 其它规则持久化参考本文案例源码

在这里插入图片描述

新增FlowRuleNacosProvider
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">ConfigService</span> configService<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span></span> <span class="token function">getRules</span><span class="token punctuation">(</span><span class="token class-name">String</span> appName<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span><span class="token function">getRuleEntitiesFromNacos</span><span class="token punctuation">(</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>configService<span class="token punctuation">,</span>
            appName<span class="token punctuation">,</span>
            <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span>FLOW_DATA_ID_POSTFIX<span class="token punctuation">,</span>
            <span class="token class-name">FlowRuleEntity</span><span class="token punctuation">.</span><span class="token keyword">class</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

新增FlowRuleNacosPublisher
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">ConfigService</span> configService<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">publish</span><span class="token punctuation">(</span><span class="token class-name">String</span> app<span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span></span> rules<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span><span class="token function">setRuleStringToNacos</span><span class="token punctuation">(</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>configService<span class="token punctuation">,</span>
            app<span class="token punctuation">,</span>
            <span class="token class-name">NacosConfigUtil</span><span class="token punctuation">.</span>FLOW_DATA_ID_POSTFIX<span class="token punctuation">,</span>
            rules
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

修改FlowControllerV1
@RestController
@RequestMapping(value = "/v1/flow")
public class FlowControllerV1 {
<span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">Logger</span> logger <span class="token operator">=</span> <span class="token class-name">LoggerFactory</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token class-name">FlowControllerV1</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">InMemoryRuleRepositoryAdapter</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span></span> repository<span class="token punctuation">;</span>

// @Autowired
// private SentinelApiClient sentinelApiClient;

<span class="token annotation punctuation">@Autowired</span>
<span class="token annotation punctuation">@Qualifier</span><span class="token punctuation">(</span><span class="token string">"flowRuleNacosProvider"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> <span class="token class-name">DynamicRuleProvider</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> ruleProvider<span class="token punctuation">;</span>
<span class="token annotation punctuation">@Autowired</span>
<span class="token annotation punctuation">@Qualifier</span><span class="token punctuation">(</span><span class="token string">"flowRuleNacosPublisher"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> <span class="token class-name">DynamicRulePublisher</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> rulePublisher<span class="token punctuation">;</span>

<span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/rules"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@AuthAction</span><span class="token punctuation">(</span><span class="token class-name">PrivilegeType</span><span class="token punctuation">.</span>READ_RULE<span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> <span class="token function">apiQueryMachineRules</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestParam</span> <span class="token class-name">String</span> app<span class="token punctuation">,</span>
                                                         <span class="token annotation punctuation">@RequestParam</span> <span class="token class-name">String</span> ip<span class="token punctuation">,</span>
                                                         <span class="token annotation punctuation">@RequestParam</span> <span class="token class-name">Integer</span> port<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>app<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"app can't be null or empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"ip can't be null or empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>port <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"port can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>

// List<FlowRuleEntity> rules = sentinelApiClient.fetchFlowRuleOfMachine(app, ip, port);
List<FlowRuleEntity> rules = ruleProvider.getRules(app);
rules = repository.saveAll(rules);
return Result.ofSuccess(rules);
} catch (Throwable throwable) {
logger.error(“Error when querying flow rules”, throwable);
return Result.ofThrowable(-1, throwable);
}
}

<span class="token keyword">private</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">R</span><span class="token punctuation">&gt;</span></span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">R</span><span class="token punctuation">&gt;</span></span> <span class="token function">checkEntityInternal</span><span class="token punctuation">(</span><span class="token class-name">FlowRuleEntity</span> entity<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isBlank</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"app can't be null or empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isBlank</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"ip can't be null or empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"port can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isBlank</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getLimitApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"limitApp can't be null or empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isBlank</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getResource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"resource can't be null or empty"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getGrade</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"grade can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getGrade</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> entity<span class="token punctuation">.</span><span class="token function">getGrade</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"grade must be 0 or 1, but "</span> <span class="token operator">+</span> entity<span class="token punctuation">.</span><span class="token function">getGrade</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">" got"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> entity<span class="token punctuation">.</span><span class="token function">getCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"count should be at lease zero"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getStrategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"strategy can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getStrategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> <span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isBlank</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getRefResource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"refResource can't be null or empty when strategy!=0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getControlBehavior</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"controlBehavior can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">int</span> controlBehavior <span class="token operator">=</span> entity<span class="token punctuation">.</span><span class="token function">getControlBehavior</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>controlBehavior <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> entity<span class="token punctuation">.</span><span class="token function">getWarmUpPeriodSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"warmUpPeriodSec can't be null when controlBehavior==1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>controlBehavior <span class="token operator">==</span> <span class="token number">2</span> <span class="token operator">&amp;&amp;</span> entity<span class="token punctuation">.</span><span class="token function">getMaxQueueingTimeMs</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"maxQueueingTimeMs can't be null when controlBehavior==2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">isClusterMode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> entity<span class="token punctuation">.</span><span class="token function">getClusterConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"cluster config should be valid"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span><span class="token string">"/rule"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@AuthAction</span><span class="token punctuation">(</span><span class="token class-name">PrivilegeType</span><span class="token punctuation">.</span>WRITE_RULE<span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span></span> <span class="token function">apiAddFlowRule</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">FlowRuleEntity</span> entity<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span></span> checkResult <span class="token operator">=</span> <span class="token function">checkEntityInternal</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>checkResult <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> checkResult<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    entity<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    entity<span class="token punctuation">.</span><span class="token function">setGmtCreate</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
    entity<span class="token punctuation">.</span><span class="token function">setGmtModified</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
    entity<span class="token punctuation">.</span><span class="token function">setLimitApp</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getLimitApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    entity<span class="token punctuation">.</span><span class="token function">setResource</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getResource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        entity <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">publishRules</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofSuccess</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Throwable</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Throwable</span> e <span class="token operator">=</span> t <span class="token keyword">instanceof</span> <span class="token class-name">ExecutionException</span> <span class="token operator">?</span> t<span class="token punctuation">.</span><span class="token function">getCause</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> t<span class="token punctuation">;</span>
        logger<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"Failed to add new flow rule, app={}, ip={}"</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@PutMapping</span><span class="token punctuation">(</span><span class="token string">"/save.json"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@AuthAction</span><span class="token punctuation">(</span><span class="token class-name">PrivilegeType</span><span class="token punctuation">.</span>WRITE_RULE<span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">FlowRuleEntity</span><span class="token punctuation">&gt;</span></span> <span class="token function">apiUpdateFlowRule</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">,</span> <span class="token class-name">String</span> app<span class="token punctuation">,</span>
                                              <span class="token class-name">String</span> limitApp<span class="token punctuation">,</span> <span class="token class-name">String</span> resource<span class="token punctuation">,</span> <span class="token class-name">Integer</span> grade<span class="token punctuation">,</span>
                                              <span class="token class-name">Double</span> count<span class="token punctuation">,</span> <span class="token class-name">Integer</span> strategy<span class="token punctuation">,</span> <span class="token class-name">String</span> refResource<span class="token punctuation">,</span>
                                              <span class="token class-name">Integer</span> controlBehavior<span class="token punctuation">,</span> <span class="token class-name">Integer</span> warmUpPeriodSec<span class="token punctuation">,</span>
                                              <span class="token class-name">Integer</span> maxQueueingTimeMs<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>id <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"id can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">FlowRuleEntity</span> entity <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>entity <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"id "</span> <span class="token operator">+</span> id <span class="token operator">+</span> <span class="token string">" dose not exist"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isNotBlank</span><span class="token punctuation">(</span>app<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        entity<span class="token punctuation">.</span><span class="token function">setApp</span><span class="token punctuation">(</span>app<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isNotBlank</span><span class="token punctuation">(</span>limitApp<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        entity<span class="token punctuation">.</span><span class="token function">setLimitApp</span><span class="token punctuation">(</span>limitApp<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isNotBlank</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        entity<span class="token punctuation">.</span><span class="token function">setResource</span><span class="token punctuation">(</span>resource<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>grade <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>grade <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> grade <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"grade must be 0 or 1, but "</span> <span class="token operator">+</span> grade <span class="token operator">+</span> <span class="token string">" got"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        entity<span class="token punctuation">.</span><span class="token function">setGrade</span><span class="token punctuation">(</span>grade<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>count <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        entity<span class="token punctuation">.</span><span class="token function">setCount</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>strategy <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>strategy <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> strategy <span class="token operator">!=</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> strategy <span class="token operator">!=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"strategy must be in [0, 1, 2], but "</span> <span class="token operator">+</span> strategy <span class="token operator">+</span> <span class="token string">" got"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        entity<span class="token punctuation">.</span><span class="token function">setStrategy</span><span class="token punctuation">(</span>strategy<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>strategy <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtil</span><span class="token punctuation">.</span><span class="token function">isBlank</span><span class="token punctuation">(</span>refResource<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"refResource can't be null or empty when strategy!=0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            entity<span class="token punctuation">.</span><span class="token function">setRefResource</span><span class="token punctuation">(</span>refResource<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>controlBehavior <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>controlBehavior <span class="token operator">!=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> controlBehavior <span class="token operator">!=</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> controlBehavior <span class="token operator">!=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"controlBehavior must be in [0, 1, 2], but "</span> <span class="token operator">+</span> controlBehavior <span class="token operator">+</span> <span class="token string">" got"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>controlBehavior <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> warmUpPeriodSec <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"warmUpPeriodSec can't be null when controlBehavior==1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>controlBehavior <span class="token operator">==</span> <span class="token number">2</span> <span class="token operator">&amp;&amp;</span> maxQueueingTimeMs <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"maxQueueingTimeMs can't be null when controlBehavior==2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        entity<span class="token punctuation">.</span><span class="token function">setControlBehavior</span><span class="token punctuation">(</span>controlBehavior<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>warmUpPeriodSec <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            entity<span class="token punctuation">.</span><span class="token function">setWarmUpPeriodSec</span><span class="token punctuation">(</span>warmUpPeriodSec<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>maxQueueingTimeMs <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            entity<span class="token punctuation">.</span><span class="token function">setMaxQueueingTimeMs</span><span class="token punctuation">(</span>maxQueueingTimeMs<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Date</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    entity<span class="token punctuation">.</span><span class="token function">setGmtModified</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        entity <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>entity <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"save entity fail: null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token function">publishRules</span><span class="token punctuation">(</span>entity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofSuccess</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Throwable</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Throwable</span> e <span class="token operator">=</span> t <span class="token keyword">instanceof</span> <span class="token class-name">ExecutionException</span> <span class="token operator">?</span> t<span class="token punctuation">.</span><span class="token function">getCause</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> t<span class="token punctuation">;</span>
        logger<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"Error when updating flow rules, app={}, ip={}, ruleId={}"</span><span class="token punctuation">,</span> entity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            entity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> id<span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@DeleteMapping</span><span class="token punctuation">(</span><span class="token string">"/delete.json"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@AuthAction</span><span class="token punctuation">(</span><span class="token class-name">PrivilegeType</span><span class="token punctuation">.</span>WRITE_RULE<span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">&gt;</span></span> <span class="token function">apiDeleteFlowRule</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>id <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"id can't be null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">FlowRuleEntity</span> oldEntity <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>oldEntity <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofSuccess</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        repository<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token function">publishRules</span><span class="token punctuation">(</span>oldEntity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> oldEntity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> oldEntity<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofSuccess</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Throwable</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Throwable</span> e <span class="token operator">=</span> t <span class="token keyword">instanceof</span> <span class="token class-name">ExecutionException</span> <span class="token operator">?</span> t<span class="token punctuation">.</span><span class="token function">getCause</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> t<span class="token punctuation">;</span>
        logger<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"Error when deleting flow rules, app={}, ip={}, id={}"</span><span class="token punctuation">,</span> oldEntity<span class="token punctuation">.</span><span class="token function">getApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            oldEntity<span class="token punctuation">.</span><span class="token function">getIp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> id<span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token function">ofFail</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

// private CompletableFuture<Void> publishRules(String app, String ip, Integer port) {
// List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
// return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);
// }
private void publishRules(String app, String ip, Integer port) {
// List<AuthorityRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
// return sentinelApiClient.setAuthorityRuleOfMachine(app, ip, port, rules);
List<FlowRuleEntity> rules = repository.findAllByApp(app);
try {
rulePublisher.publish(app, rules);
} catch (Exception e) {
e.printStackTrace();
}
}
}


标签:null,return,rules,1.8,nacos,Nacos,化到,Result,entity
From: https://blog.csdn.net/weixin_tank88921/article/details/139353421

相关文章

  • 借助大模型实现从数字化到智能化
    2024年5月初,国家发展改革委办公厅、国家数据局综合司印发《数字经济2024年工作要点》(以下简称《工作要点》),对2024年数字经济重点工作作出部署。国家发展改革委办公厅和国家数据局综合司印发的这份文件,提出了九方面的重点工作,包括适度超前布局数字基础设施、加快构建数据基础制度、......
  • Labels and Databases for Mac( 数据库标签制作软件)1.8.0版
     LabelsandDatabasesforMac一款数据库标签制作和设计软件。LabelsandDatabases可以帮助您使用各种内置标签格式创建标签、信封和卡片,并使用用户数据库中包含的信息填充它们,即使用MailMerge创建和打印标签。LabelsandDatabasesforMac(数据库标签制作软件)软件......
  • 行列式求值,从 $n!$ 优化到 $n^3$
    前置知识\(\sum\)为累加符号,\(\prod\)为累乘符号。上三角矩阵指只有对角线及其右上方有数值其余都是\(0\)的矩阵。如果一个矩阵的对角线全部为\(1\)那么这个矩阵为单位矩阵记作\(I\)。对于矩阵\(A_{n,m}\)和矩阵\(B_{m,n}\)满足\(A_{i,j}=B_{j,i}\)记作\(A=B^T......
  • 微服务-Nacos-安装-集成SpringBoot
    微服务-SpringCloud-ALibaba-NacosNacos是阿里巴巴推出的SpringCloud的组件官网:什么是Nacos主要是为了解决微服务的架构中服务治理的问题服务治理就是进行服务的自动化管理,其核心是服务的注册与发现。服务注册:服务实例将自身服务信息注册到注册中心。服务发现:服务实......
  • JDK1.8新特性
    Lambda表达式又称为函数式编程面向对象思想:想要做什么事情,就需要创建这个类的对象,通过对象来完成某些事情函数式的思想:忽略了面向对象复杂的语法,强调做什么,而不是以什么形式做面向对象的思想实现多线程publicclassRunnableImplimplementsRunnable{@Override......
  • 我的世界MC网页版搭建教程(1.8和1.5)
    我的世界mc网页版搭建(1.8和1.5)本教程基于开源项目eaglercraft说明网页版mc作者是lax1dude,官方网站是eaglercraft,大家可以去多多支持作者我不是作者,我只是总结了一下服务器搭建的流程下文链接随时可能失效,文件最好自己保存一下本文适合有一点linux基础的使用,有些......
  • 微服务:构建Spring Boot与Dubbo集成:以Nacos为注册中心
    一、前言  Dubbo是阿里巴巴公司开源的一个高性能、轻量级的JavaRPC框架,用于构建分布式服务架构。Dubbo提供了三大核心能力:面向接口的远程方法调用、智能容错和负载均衡,以及服务自动注册和发现。二、Dubbo的特点  面向接口代理的高性能RPC调用:提供高性能的基于代理的......
  • 基于Nacos实现Sentinel规则持久化
    基于Nacos实现Sentinel规则持久化一、Sentinel使用痛点二、解决方案2.1保存本地文件2.2保存数据库2.3保存到Nacos三、规则持久化到Nacos3.1Nacos服务端修改配置3.2Sentinel控制台修改配置3.3Nacos数据源整合到Sentinel中一、Sentinel使用痛点SpringCloudAlib......
  • nacos启动异常和动态配置未生效
    最近打算研究一下nacos动态配置的原理,于是开始照着Nacos官网快速入门。但是发现在和SpringBoot以及SpringCloud适配启动的时候,总是报错无法启动,于是深入排查了一下并作以记录。1.nacos和SpringBoot启动报错Invaliddefault:publicabstractcom.alibaba.nacos.api.config.Con......
  • Nacos的配置管理
    为什么需要配置管理?在单体应用中,配置管理可能不是什么大的事情,通常会以配置文件的方式。常见的方法比如将配置通过打包脚本打入应用包中,或者直接放到运行应用的服务器的特定目录下,或者存储到数据库中。这种方式在传统的单体应用中简单有效,但是也会有些比较棘手的问题,比如:配置......