nacos1.4读取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());
    }

 

posted on 2023-05-02 17:15  王飞侠  阅读(1184)  评论(0编辑  收藏  举报

导航