0


SpringCloud-SpringBoot读取Nacos上的配置文件

在 Spring Boot 应用程序中,可以使用 Spring Cloud Nacos 来实现从 Nacos 服务注册中心和配置中心读取配置信息。以下是如何在 Spring Boot 中读取 Nacos 上的配置文件的步骤:

1. 引入依赖

首先,在 Spring Boot 项目的 pom.xml 文件中添加 Spring Cloud Nacos 的依赖:

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  4. </dependency>

2. 配置 Nacos 连接信息

将 Nacos 服务注册中心和配置中心的地址、命名空间等相关信息添加到 application.properties 或 application.yml 配置文件中:

  1. spring.cloud.nacos.config.server-addr=localhost:8848
  2. spring.cloud.nacos.config.namespace=
  1. **3. 编写配置文件**

在 Nacos 配置中心创建配置文件(例如

  1. application.properties

​),并添加一些键值对,如:

  1. user.name=John Doe
  2. user.age=30

4. 读取配置信息

在 Spring Boot 的任何配置类或组件类中,可以使用

  1. @Value

​ 注解或

  1. @ConfigurationProperties

​ 注解来读取配置项:

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ConfigController {
  6. @Value("${user.name}")
  7. private String userName;
  8. @Value("${user.age}")
  9. private int userAge;
  10. @GetMapping("/userInfo")
  11. public String getUserInfo() {
  12. return "User Name: " + userName + ", Age: " + userAge;
  13. }
  14. }

5. 刷新配置

如果想要在配置发生变化时动态刷新配置,可以在需要动态更新的 Bean 类上添加

  1. @RefreshScope

​ 注解:

  1. import org.springframework.cloud.context.config.annotation.RefreshScope;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. @RefreshScope
  5. public class MyConfigBean {
  6. @Value("${my.config}")
  7. private String myConfig;
  8. // Getter and Setter
  9. }

当配置发生变化后,可以通过访问 Actuator 端点

  1. ​/actuator/refresh

​ 来触发配置的刷新,以便及时获取最新的配置信息。

如果nacos上面有很多个配置文件,springboot中如何获取想要的配置文件?

1.在 Nacos 配置中心创建多个配置文件,例如

  1. user.properties

​ 和

  1. database.properties

​,并添加相应的键值对。

2.在 Spring Boot 项目的

  1. application.properties

​ 或

  1. application.yml

​ 中,指定需要获取的配置文件的 Data ID:

  1. spring.cloud.nacos.config.shared-configs[0].data-id=user.properties
  2. spring.cloud.nacos.config.shared-configs[1].data-id=database.properties

3.可以通过

  1. @ConfigurationProperties

​ 注解来读取指定的配置文件内容,例如:

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. @ConfigurationProperties("user")
  5. public class UserConfig {
  6. private String userName;
  7. private int age;
  8. // Getters and Setters
  9. }
  10. @Component
  11. @ConfigurationProperties("database")
  12. public class DatabaseConfig {
  13. private String url;
  14. private String username;
  15. // Getters and Setters
  16. }

在上面的示例中,

  1. @ConfigurationProperties

​ 注解中的 value 值指定了要绑定的配置文件的前缀,可以直接读取到该配置文件中的相关属性值。

注意: 在使用

  1. @ConfigurationProperties

​ 注解时,需要确保属性名与配置文件中的键名一致,Spring Boot 会自动根据前缀匹配来绑定配置项。

4.多文件配置和自动刷新也可参考如下配置:


本文转载自: https://blog.csdn.net/qq873113580/article/details/136565307
版权归原作者 Teln_小凯 所有, 如有侵权,请联系我们删除。

“SpringCloud-SpringBoot读取Nacos上的配置文件”的评论:

还没有评论