用Java代码从Nacos配置中心读取Spring boot程序的 yaml 格式配置文件内容

文章详细描述了SpringCloudAlibaba集成示例中,YamlConfigParser类如何从Nacos配置中心读取并解析`integrated-consumer.yaml`文件的内容,提取SpringBoot程序的配置信息,如topic、consumerGroup等。
摘要由CSDN通过智能技术生成

Spring Cloud Alibaba (https://gitee.com/mirrors/Spring-Cloud-Alibaba)示例代码 spring-cloud-alibaba-examples 的 Integerated-example 项目中,integrated-praise-consumer 模块中,在YamlConfigParser类中,实现了从Nacos配置中心读取Spring boot程序的 yaml 格式配置文件内容的功能。

这个类的代码如下:

public class YamlConfigParser {
    @Autowired
    private NacosConfigManager nacosConfigManager;

    private Integer pullInterval = 4000;
    private Integer pullBatchSize = 4;

    private Integer minThread = 2; // 设置最小消费线程数
    private Integer maxThread = 32; // 设置最大消费线程数
    private String consumerGroup; // 消费者组
    private String  nameSrvAddr; // 命名服务器地址和端口号
    private String topic; // 消息主题

    public YamlConfigParser() {
    }

    public void getConfig(){
        String cfg = "";
        try {
            ConfigService configService = nacosConfigManager.getConfigService();
            cfg = configService.getConfig("integrated-consumer.yaml", "integrated-example", 2000);
            System.out.println("获取到配置信息--------------");
            System.out.println(cfg);
        }catch (NacosException e){
            e.printStackTrace();
        }
        if(cfg.length() == 0)
            return ;

        // 将字符串转换为字节数组
        byte[] bytes = cfg.getBytes(StandardCharsets.UTF_8);
        Yaml yaml = new Yaml();
        try (InputStream in = new ByteArrayInputStream(bytes)) {
            Map<String, Object> config = yaml.load(in);

            // 需要按照配置文件逐层解析并访问配置信息
            Map<String, Object> springConfig = (Map<String, Object>) config.get("spring");
            Map<String, Object> cloudConfig = (Map<String, Object>) springConfig.get("cloud");
            Map<String, Object> streamConfig = (Map<String, Object>) cloudConfig.get("stream");
            Map<String, Object> bindingsConfig = (Map<String, Object>) streamConfig.get("bindings");
            Map<String, Object> inputConfig = (Map<String, Object>) bindingsConfig.get("praise-input");

            // topic = (String) config.get("spring.cloud.stream.bindings.praise-input.destination"); // 不支持这种用法,取不到值
            topic = (String) inputConfig.get("destination");
            consumerGroup = (String) inputConfig.get("group");

            Map<String, Object> rocketmqConfig = (Map<String, Object>) streamConfig.get("rocketmq");
            Map<String, Object> binderConfig = (Map<String, Object>) rocketmqConfig.get("binder");
            nameSrvAddr = (String)binderConfig.get("name-server");

            Map<String, Object> mqbindingsConfig = (Map<String, Object>) rocketmqConfig.get("bindings");
            Map<String, Object> mqpraiseinputConfig = (Map<String, Object>) mqbindingsConfig.get("praise-input");
            Map<String, Object> consumerinputConfig = (Map<String, Object>) mqpraiseinputConfig.get("consumer");

            pullInterval = (Integer)consumerinputConfig.get("pullInterval");
            pullBatchSize = (Integer)consumerinputConfig.get("pullBatchSize");

            // 输出配置信息
            System.out.println("nameSrvAddr: " + nameSrvAddr);
            System.out.println("consumerGroup: " + consumerGroup);
            System.out.println("topic: " + topic);
            System.out.println("pullInterval: " + pullInterval);
            System.out.println("pullBatchSize: " + pullBatchSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Integer getPullInterval() {
        return pullInterval;
    }
    public void setPullInterval(Integer pullInterval) {
        this.pullInterval = pullInterval;
    }
    public Integer getPullBatchSize() {
        return pullBatchSize;
    }
    public void setPullBatchSize(Integer pullBatchSize) {
        this.pullBatchSize = pullBatchSize;
    }
    public Integer getMinThread() {
        return minThread;
    }
    public void setMinThread(Integer minThread) {
        this.minThread = minThread;
    }
    public Integer getMaxThread() {
        return maxThread;
    }
    public void setMaxThread(Integer maxThread) {
        this.maxThread = maxThread;
    }
    public String getConsumerGroup() {
        return consumerGroup;
    }
    public void setConsumerGroup(String consumerGroup) {
        this.consumerGroup = consumerGroup;
    }
    public String getNameSrvAddr() {
        return nameSrvAddr;
    }
    public void setNameSrvAddr(String nameSrvAddr) {
        this.nameSrvAddr = nameSrvAddr;
    }
    public String getTopic() {
        return topic;
    }
    public void setTopic(String topic) {
        this.topic = topic;
    }
}

读取的 integrated-consumer.yaml 文件内容如下:

spring:
  datasource:
    url: jdbc:mysql://integrated-mysql:3306/integrated_praise?useSSL=false&characterEncoding=utf8
  cloud:
    stream:
      bindings:
        praise-input:
          destination: PRAISE-TOPIC-01
          content-type: application/json
          group: praise-consumer-group-PRAISE-TOPIC-01
      rocketmq:
        binder:
          name-server: rocketmq:9876
        bindings:
          praise-input:
            consumer:
              pullInterval: 4000
              pullBatchSize: 4

代码中getConfig()方法功能是从Nacos配置中心获取名为integrated-consumer.yaml的配置文件,并将其内容解析为Java对象,然后从这些对象中提取特定的配置信息。以下是该代码的主要步骤和功能:

  1. 获取配置:使用ConfigService从Nacos配置中心获取名为integrated-consumer.yaml的配置,配置文件的命名空间(namespace)是integrated-example,并设置超时时间为2000毫秒。

ConfigService configService = nacosConfigManager.getConfigService();
cfg = configService.getConfig("integrated-consumer.yaml", "integrated-example", 2000);

   2.配置解析:将获取到的字符串配置cfg转换为字节数组,然后使用Yaml库将其解析为Map<String, Object>对象。

byte[] bytes = cfg.getBytes(StandardCharsets.UTF_8);
Yaml yaml = new Yaml();
try (InputStream in = new ByteArrayInputStream(bytes)) {
Map<String, Object> config = yaml.load(in);
}

3.逐层解析配置:通过逐层访问Map对象,提取特定的配置信息,如topicconsumerGroupnameSrvAddr等。

Map<String, Object> springConfig = (Map<String, Object>) config.get("spring");
Map<String, Object> cloudConfig = (Map<String, Object>) springConfig.get("cloud");
// ... 更多的逐层解析 ...
topic = (String) inputConfig.get("destination");
consumerGroup = (String) inputConfig.get("group");
nameSrvAddr = (String)binderConfig.get("name-server");

需要注意的是,这段代码假设YAML配置文件的结构是已知的,并且能够通过链式调用get方法访问到相应的配置信息。如果配置文件的结构发生变化,或者某个键不存在,这段代码将会抛出NullPointerException

此外,这段代码还展示了如何从一个复杂的嵌套结构中提取特定的配置值,这在处理复杂的配置文件时是非常常见的。

代码中

// 不支持这种用法,取不到值

topic = (String) config.get("spring.cloud.stream.bindings.praise-input.destination");

这是尝试通过直接拼接键名来获取配置值的方式,证明是不可行的,必须逐层解析配置对象来获取值。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot中,你可以使用Nacos作为配置中心,以便动态获取配置信息。下面是一些步骤来实现在Spring Boot读取Nacos配置文件: 1. 添加依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 2. 配置Nacos连接信息:在`application.properties`或`application.yml`中添加以下配置: ```yaml spring.cloud.nacos.config.server-addr=${NACOS_SERVER_ADDR:localhost:8848} spring.application.name=your-application-name spring.cloud.nacos.config.namespace=${NACOS_NAMESPACE:} ``` 其中`${NACOS_SERVER_ADDR}`是Nacos服务器地址,`${NACOS_NAMESPACE}`是命名空间,`your-application-name`是你的应用名称。 3. 创建配置类:创建一个`@ConfigurationProperties`注解的类,用于绑定Nacos配置: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "your-config-prefix") public class YourConfigProperties { private String property1; private String property2; // getters and setters } ``` 这里的`your-config-prefix`是你在Nacos中存储配置的前缀。 4. 注入配置属性:在需要使用配置的地方,使用`@Autowired`注解将配置类注入: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @Autowired private YourConfigProperties configProperties; @GetMapping("/your-endpoint") public String yourEndpoint() { String property1 = configProperties.getProperty1(); String property2 = configProperties.getProperty2(); // 使用配置属性做一些操作 return "Some result"; } } ``` 现在,你可以在`YourController`中使用注入的配置属性来读取Nacos配置文件中的。 注意:确保Nacos服务器已经启动,并且配置文件已经在Nacos中正确配置。如果需要动态刷新配置,请参考Spring Cloud Alibaba的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因上精进,果上随缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值