当前位置: 首页 > news >正文

Nacos学习笔记-占位符读取其他命名空间内容

Nacos当前命名空间下的配置文件需要跨命名空间读取其他配置文件的内容。可以先通过Nacos提供的API接口获取配置文件内容,然后解析数据将其放入环境的PropertySource中。
  • 相关依赖包
<!-- Nacos依赖包 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.2.6.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>2.2.6.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>2.3.0</version>
</dependency>
  • 部分代码

@Order(Ordered.HIGHEST_PRECEDENCE)
public class NacosEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";

    private static final String[] dataIds = new String[]{"db-sample.yml"};

    private static final String LOGIN_URL = "http://$host/nacos/v1/auth/users/login";

    private static final String CONFIGS_URL = "http://$host/nacos/v1/cs/configs?dataId=&group=&appName=&config_tags=&pageNo=1&pageSize=100&tenant=$tenant&search=blur&accessToken=$accessToken";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        if (environment instanceof StandardServletEnvironment || environment instanceof StandardReactiveWebEnvironment) {
            return;
        }
        addPropertySource(environment);
    }

    private void addPropertySource(ConfigurableEnvironment environment) {
        Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
        Object namespaceIdObj = systemEnvironment.get("NACOS_NAMESPACE");
        if (null == namespaceIdObj) {
            return;
        }

        String host = String.valueOf(systemEnvironment.get("NACOS_HOST"));
        String username = String.valueOf(systemEnvironment.get("NACOS_USERNAME"));
        String password = String.valueOf(systemEnvironment.get("NACOS_PASSWORD"));
        String group = String.valueOf(systemEnvironment.get("NACOS_GROUP"));
        String namespaceId = String.valueOf(namespaceIdObj);
        log.info("{} {} {} {} {}", host, username, password, group, namespaceId);

        processV2(environment, host, username, password, namespaceId);
    }

    private void processV1(ConfigurableEnvironment environment, String host, String username, String password,
            String namespaceId, String group) {
        NacosConfigProperties nacosConfigProperties = new NacosConfigProperties();
        nacosConfigProperties.setEnvironment(new StandardEnvironment());
        nacosConfigProperties.setServerAddr(host);
        nacosConfigProperties.setUsername(username);
        nacosConfigProperties.setPassword(password);
        nacosConfigProperties.setNamespace(namespaceId);
        nacosConfigProperties.setGroup(group);

        ConfigService configService = null;
        try {
            configService = NacosFactory.createConfigService(nacosConfigProperties.assembleConfigServiceProperties());
        } catch (NacosException e) {
            log.error(e.getMessage(), e);
        }

        if (null == configService) {
            return;
        }

        Properties properties = new Properties();

        for (String dataId : dataIds) {
            addProperties(dataId, getPropertySources(configService, dataId, group), properties);
        }

        environment.getPropertySources().addFirst(new PropertiesPropertySource(NACOS_PROPERTY_SOURCE_NAME, properties));
    }

    private List<PropertySource<?>> getPropertySources(ConfigService configService, String dataId, String group) {
        List<PropertySource<?>> propertySources = null;
        try {
            String config = configService.getConfig(dataId, group, 10000);
            log.info("{} {} config {}", dataId, group, config);
            propertySources = NacosDataParserHandler.getInstance().parseNacosData(dataId, config, null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return propertySources;
    }

    private void processV2(ConfigurableEnvironment environment, String host, String username, String password,
            String namespaceId) {
        if (null != host && host.contains(",")) {
            host = host.split(",")[0];
        }

        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        String loginResp = HttpClientUtils.sendPost(LOGIN_URL.replace("$host", host), params, "UTF-8");
        log.info("login response {}", loginResp);
        LoginDTO loginDTO = JsonUtils.json(loginResp, LoginDTO.class);
        if (null == loginDTO) {
            return;
        }

        String configsResp = HttpClientUtils.sendGet(CONFIGS_URL.replace("$host", host)
            .replace("$tenant", namespaceId).replace("$accessToken", loginDTO.getAccessToken()), "UTF-8");
        ConfigDTO configDTO = JsonUtils.json(configsResp, ConfigDTO.class);
        if (null == configDTO) {
            return;
        }

        List<ConfigDTO.Item> pageItems = configDTO.getPageItems();
        if (null == pageItems) {
            return;
        }

        Properties properties = new Properties();

        pageItems.forEach(item -> addProperties(item.getDataId(), getPropertySources(item), properties));

        environment.getPropertySources().addFirst(new PropertiesPropertySource(NACOS_PROPERTY_SOURCE_NAME, properties));
    }

    private List<PropertySource<?>> getPropertySources(ConfigDTO.Item item) {
        List<PropertySource<?>> propertySources = null;
        try {
            propertySources = NacosDataParserHandler.getInstance().parseNacosData(item.getDataId(), item.getContent(), null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return propertySources;
    }

    private void addProperties(String dataId, List<PropertySource<?>> propertySources, Properties properties) {
        if (null == propertySources || propertySources.isEmpty()) {
            return;
        }
        propertySources.forEach(propertySource -> {
            Object source = propertySource.getSource();
            if (source instanceof Map) {
                Map<String, Object> map = (Map) source;
                map.forEach((key, value) -> {
                    if (!key.equals("spring.application.name") && !key.startsWith("server")) {
                        int i = dataId.lastIndexOf(".");
                        if (i != -1) {
                            properties.setProperty(dataId.substring(0, i + 1) + key, String.valueOf(value));
                        }
                    }
                });
            }
        });
    }

}

相关文章:

  • 练习题:76
  • 免费开源抓包工具Wireshark介绍
  • AWS IoT Core:支持 MQTT、HTTP、WebSocket 多种协议转换。
  • 【哇! C++】类和对象(五) - 赋值运算符重载
  • 机试题——公网下线方案
  • MongoDB学习笔记
  • Python|基于DeepSeek大模型,自动生成语料数据(10)
  • IDE集成开发环境MyEclipse中安装SVN
  • 每日一题——763. 划分字母区间
  • 【面试】Java 并发
  • 基于stm32的模拟电磁曲射炮研究
  • mysql的Innodb最大支持的索引长度是多少,以及索引长度怎么计算
  • Leetcode 3479. Fruits Into Baskets III
  • 蓝桥杯第二天:2023省赛C 1题 分糖果
  • unordered_set 的常用函数
  • 【python】Flask web框架
  • ble.sh 的安装和用法
  • 如何在SpringBoot中灵活使用异步事件?
  • C++—vector类的使用及模拟实现
  • Windows 11下Git Bash执行cURL脚本400问题、CMD/PowerShell不能执行多行文本等问题记录及解决方案
  • 学校网站建设的优势和不足/seo关键字优化价格
  • 无锡住房建设网站/google安卓版下载
  • 网站建设的一些销售技巧/软件外包公司排名
  • 濮阳新闻综合频道直播/关键词排名优化营销推广
  • 网站建设工作领导小组/自己创建网站
  • wordpress搬站/抖音引流推广怎么做