首页 > 其他分享 >nacos1.4读取properties配置文件中的数组对象,实现动态更新

nacos1.4读取properties配置文件中的数组对象,实现动态更新

时间:2023-05-02 17:22:33浏览次数:32  
标签:nacos1.4 String 配置文件 private appConfigs wechat import cp properties

 

方法一:不可自动更新配置,有待检查。

package com.javaweb.admin.config;

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import lombok.Data;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Data
@Configuration
@NacosConfigurationProperties(dataId = "zrkj-yun.yaml", type = ConfigType.YAML, prefix = "wechat.cp")
public class TestConfig {
    /**
     * 设置企业微信的corpId
     */
    private String corpId;

    private List<AppConfig> appConfigs;

    @Data
    public static class AppConfig {
        /**
         * 设置企业微信应用的AgentId
         */
        private Integer agentId;

        /**
         * 设置企业微信应用的Secret
         */
        private String secret;

        /**
         * 设置企业微信应用的token
         */
        private String token;

        /**
         * 设置企业微信应用的EncodingAESKey
         */
        private String aesKey;

        /**
         * 私钥路径
         */
        private String privateKeyPath;

    }
}
wechat:
  cp:
    corpId: ww47e7d00dd9bb9
    appConfigs:
      - agentId: 20004
        secret: eNBpcLoO3iqto5V-v0oCQ
        token: uOT5GNg8HxSVYm
        aesKey: K3mLp83WRI7ekwGbO9ZCNNTz
        private_key_path: "-----BEGIN RSA PRIVATE KEY-----\
MIIEowIBAAKCAQEAiFSVRRdcizgETkJ9ekot2EDSQaZZzoKGyjR/CsIbUyomVV6x\
Q9PNRTIxEWZBKnqJGXEOnHmG9UK7KxIgOCqUNl4209kF+SDp3tq46jwl/E5UBh+Y\
mjb23nWyOujluysiwI21UdFxbFnDcigKp8YMbbFR4AV//6jzpjaLjAQjdWUB4/db\
QvFq44e4/W90CZ9Gc0fwLbeaA8RY9OQQxGoc1yqoivkls1CrDb77ybgBm13hOC5S\
XY8VYdY5xQcN2+FF5Naav7TuWgN7s4gCxWJcyQ+KK2akdJXEoNjWiKKvm69himT0\
X9UlS5MhXeHoHcK6lVKnIaWp2XQLoENWis2T7wIDAQABAoIBAA+xsQdlo5UxSymZ\
NOm1jWaGO84r8M25r/uqJG/gHZYq1YPhZUW6JbjQCN8IZvsVZSAFKFnyEYu9dV+F\
dCkTGcHSgbxMkQf3doTdqAjrCLJtb/XOgFpMdonwgaaPdhbgZd1F0vhKxKRlBv9m\
xac/wOGF1reT2oLbd8UMJW9mcJCMdcOmcs0s0qWJ8jFT4+kEqbJOgWY0GwLghryB\
+w/VFxsS7OvL5ivhvPJ2PioOX9q3BsecYnfFv1Uq3BXAVEfytAm1N/MZHJ+OO1yb\
tpvpGAbXlogt7lTAPh70Ia3t6V1+jDcuf95Jsk2ds3MxiRfyOmDR5XRN4c+loGTS\
-----END RSA PRIVATE KEY-----"
      - agentId: 100002
        secret: 1111
        token: 111
        aesKey: 111
        private_key_path: /root/talk/pkcs8.pem

配图

 

 

方法二:可以实现同nacos实时更新配置

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Data
@Configuration
@ConfigurationProperties(prefix = "wechat.cp")
public class WxCpProperties {
    /**
     * 设置企业微信的corpId
     */
    private String corpId;

    private List<AppConfig> appConfigs;

    @Data
    public static class AppConfig {
        /**
         * 设置企业微信应用的AgentId
         */
        private Integer agentId;

        /**
         * 设置企业微信应用的Secret
         */
        private String secret;

        /**
         * 设置企业微信应用的token
         */
        private String token;

        /**
         * 设置企业微信应用的EncodingAESKey
         */
        private String aesKey;

        /**
         * 私钥路径
         */
        private String privateKeyPath;

    }
}
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.spring.util.NacosUtils;
import com.google.common.base.Strings;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

@Configuration
public class WxCpConfig {

    @Resource
    private WxCpProperties wxCpProperties;

    @NacosConfigListener(dataId = "zrkj-yun-local", type = ConfigType.PROPERTIES)
    public void onMessage(String msg) {
        WxCpProperties newProperties = getNewProperties(msg);
        updateProperties(newProperties);
    }

    private WxCpProperties getNewProperties(String msg) {
        if (Strings.isNullOrEmpty(msg)) {
            return null;
        }
        //Map<String, Object> properties =
        final Properties properties = NacosUtils.toProperties(msg, "properties");
        List<ConfigurationPropertySource> propertySources = Collections.singletonList(new MapConfigurationPropertySource(properties));
        return new Binder(propertySources).bindOrCreate("wechat.cp", Bindable.of(WxCpProperties.class));
    }

    private void updateProperties(WxCpProperties newProperties) {
        wxCpProperties.setCorpId(newProperties.getCorpId());
        wxCpProperties.setAppConfigs(newProperties.getAppConfigs());
    }

    public WxCpProperties getWxCpProperties() {
        return wxCpProperties;
    }
}
wechat.cp.corpId=ww47e7f4ad00jksdfjksl
wechat.cp.appConfigs[0].agentId=20004
wechat.cp.appConfigs[0].secret=eNBpcLoUcXXkDsdfdtRFzpJ3H3IIO3iqto5V-v0oCQ
wechat.cp.appConfigs[0].token=uOT5GNgssst3cSVYm
wechat.cp.appConfigs[0].aesKey=K3dffI6z6bU6I7ekwGbO9ZCNNTz
wechat.cp.appConfigs[0].privateKeyPath=-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAiFSVRRdcizgETkJ9ekot2EDSQaZZzoKGyjR/CsIbUyomVV6xQ9PNRTIxEWZBKnqJGXEOnHmG9UK7KxIgOCqUNl4209kF+SDp3tq46jwl/E5UBh+Ymjb23nWyOujluysiwI21UdFxbFnDcigKWXuNeYT7XEpwet3a3qy9kK9oj6CXjWHcpaJAc35xUh6gAzHXkhy1lgv8/bV3ny6dUCnBRWAHfI5qOf/L4CJ/mfH6PLEO6wizLIGQlngMStRfkNg2LaES4FWi/iRxXcTlLxO6OVF+DA4ylJc8diUGYH5WopVoRiXoV8nBY86ycLYGt8hQsjywDxLI7jzd-----END RSA PRIVATE KEY-----
wechat.cp.appConfigs[1].agentId=1002
wechat.cp.appConfigs[1].secret=1111-0ppp
wechat.cp.appConfigs[1].token=111
wechat.cp.appConfigs[1].aesKey=111
wechat.cp.appConfigs[1].privateKeyPath=/root/talk/pkcs8.pem

最后在类中调用

@Resource
    private WxCpProperties wxCpProperties;

    public JsonResult qqq() {
        log.info("-------wxCpProperties---->" + wxCpProperties.getAppConfigs());
        log.info("------getCorpId------>" +  wxCpProperties.getCorpId());
        return JsonResult.success(null, wxCpProperties.getAppConfigs().get(1).getSecret());
    }

 

标签:nacos1.4,String,配置文件,private,appConfigs,wechat,import,cp,properties
From: https://www.cnblogs.com/wangfx/p/17367947.html

相关文章

  • vscode配置文件
    vscode用户配置文件{/*editor*/"editor.cursorBlinking":"smooth",//使编辑器光标的闪烁平滑,有呼吸感"editor.formatOnPaste":true,//在粘贴时格式化代码"editor.formatOnType":true,//敲完一行代码自动格式化"editor.smoothScrolling"......
  • 06 Spring集合注入&读取properties文件
    文章目录1Spring注入集合1.1在目标类中添加接收集合的的变量并提供setter方法1.2在配置文件中进行配置2读取properties文件2.1新开一个命名空间2.2在resources目录下新建.properties配置文件2.3在spring配置文件applicationContext.xml中引用配置文件信息2.4配置文件需要......
  • tileserver在配置文件中配置 CORS 可跨域
    您可以在Tileserver配置文件中设置Access-Control-Allow-Origin头来启用CORS,以便您的地图数据可以被跨域请求。以下是如何在Tileserver配置文件中设置CORS的步骤:打开Tileserver配置文件,通常位于您的tiles目录下的config.json文件中。找到headers配置项,这个配置项......
  • SpringBoot读取.yml配置文件最常见的两种方式-源码及其在nacos的应用
    三、第二种方式(推荐)这种方式是小编比较推荐的,虽然看似比​​@Value​​麻烦不少,但是更加的规范,在配合nacos的时候也可以动态的修改,会立即生效,一会小编带大家试一下哈!!为什么推荐这种方式呢,是因为spring他们都是使用这种方式进行配置的,所以跟着官方走不会有错的!!1.修改yml文件我们......
  • 常见配置文件在Python中的使用
     配置文件主要为了存储常用的常量,如数据库的信息,通用的账号和密码等。常见的配置文件格式有ini,yaml,toml,json,env等,在做自动化测试的时候,它们都起什么样的作用?在什么样的场合下应用哪些配置文件? 一、ini配置文件简介:ini配置文件是最直接的配置文件,也是最简单的配置文件,将变量......
  • 控制台报错:[Vue warn]: Error in render: "TypeError: Cannot read properties of nu
    可能原因在调取接口获取返回值时,由于各种原因(参数错误、返回格式不规范等),导致接收返回时数据类型与初始值不同。data(){return{list:[]//原本是个数组对象}},methods:{getList(){letparams={}apiRequest(params).then(r......
  • FastFDS中的配置文件
    1、client.conf#connecttimeoutinseconds#defaultvalueis30sconnect_timeout=30       #连接超时 #networktimeoutinseconds#defaultvalueis30snetwork_timeout=60       #网络超时 #thebasepathtostorelogfiles......
  • Spring17_配置文件知识要点5
    <bean>标签id属性:在容器中Bean实例的唯一标识,不允许重复class属性:要实例化的Bean的全限定名scope属性:Bean的作用范围,常用是Singleton(默认)和prototype<property>标签:属性注入,set方法注入使用name属性:属性名称va......
  • TypeError: Cannot read properties of undefined (reading 'filter')
    TypeError:Cannotreadpropertiesofundefined(reading'filter')constfilterTableData=computed(()=>store.data.users!.filter((data)=>!search.value||data.nick.toLowerCase().includes(search.value......
  • Spring17_配置文件依赖注入4
    一、Bean的依赖注入入门1.创建UserService,UserService内部再调用UserDao的save()方法 2.将UserServiceImpl的创建权交给Spring3.从Spring容器中获得UserService进行操作执行UserController中的main方法,检查控制台输出:二、Bean的依赖......