SpringBoot讀取Nacos上配置文件的步驟詳解
前言
在 Spring Boot 應用程序中,可以使用 Spring Cloud Nacos 來實現(xiàn)從 Nacos 服務注冊中心和配置中心讀取配置信息。以下是如何在 Spring Boot 中讀取 Nacos 上的配置文件的步驟:
1. 引入依賴
首先,在 Spring Boot 項目的 pom.xml 文件中添加 Spring Cloud Nacos 的依賴:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>2. 配置 Nacos 連接信息
將 Nacos 服務注冊中心和配置中心的地址、命名空間等相關信息添加到 application.properties 或 application.yml 配置文件中:
spring.cloud.nacos.config.server-addr=localhost:8848 spring.cloud.nacos.config.namespace=
3. 編寫配置文件
在 Nacos 配置中心創(chuàng)建配置文件(例如 ?application.properties?),并添加一些鍵值對,如:
user.name=John Doe user.age=30
4. 讀取配置信息
在 Spring Boot 的任何配置類或組件類中,可以使用 ?@Value? 注解或 ?@ConfigurationProperties? 注解來讀取配置項:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
@Value("${user.name}")
private String userName;
@Value("${user.age}")
private int userAge;
@GetMapping("/userInfo")
public String getUserInfo() {
return "User Name: " + userName + ", Age: " + userAge;
}
}5. 刷新配置
如果想要在配置發(fā)生變化時動態(tài)刷新配置,可以在需要動態(tài)更新的 Bean 類上添加 ?@RefreshScope? 注解:
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Component
@RefreshScope
public class MyConfigBean {
@Value("${my.config}")
private String myConfig;
// Getter and Setter
}當配置發(fā)生變化后,可以通過訪問 Actuator 端點 ?/actuator/refresh? 來觸發(fā)配置的刷新,以便及時獲取最新的配置信息。
如果nacos上面有很多個配置文件,springboot中如何獲取想要的配置文件?
1.在 Nacos 配置中心創(chuàng)建多個配置文件,例如 ?user.properties? 和 ?database.properties?,并添加相應的鍵值對。
2.在 Spring Boot 項目的 ?application.properties? 或 ?application.yml? 中,指定需要獲取的配置文件的 Data ID:
spring.cloud.nacos.config.shared-configs[0].data-id=user.properties spring.cloud.nacos.config.shared-configs[1].data-id=database.properties
3.可以通過 ?@ConfigurationProperties? 注解來讀取指定的配置文件內容,例如:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("user")
public class UserConfig {
private String userName;
private int age;
// Getters and Setters
}
@Component
@ConfigurationProperties("database")
public class DatabaseConfig {
private String url;
private String username;
// Getters and Setters
}在上面的示例中,?@ConfigurationProperties? 注解中的 value 值指定了要綁定的配置文件的前綴,可以直接讀取到該配置文件中的相關屬性值。
注意: 在使用 ?@ConfigurationProperties? 注解時,需要確保屬性名與配置文件中的鍵名一致,Spring Boot 會自動根據前綴匹配來綁定配置項。
4.多文件配置和自動刷新也可參考如下配置:

以上就是SpringBoot讀取Nacos上配置文件的步驟詳解的詳細內容,更多關于SpringBoot讀取Nacos配置文件的資料請關注腳本之家其它相關文章!
相關文章
Java用20行代碼實現(xiàn)抖音小視頻批量轉換為gif動態(tài)圖
這篇文章主要介紹了Java用20行代碼實現(xiàn)抖音小視頻批量轉換為gif動態(tài)圖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
SpringBoot Mybatis多數(shù)據源配置全過程
本文詳細介紹了如何在Java項目中配置多數(shù)據源,包括配置文件設置、多數(shù)據源配置類、MyBatis配置等,通過兩種不同的方式配置primary和secondary數(shù)據源,展示了如何處理Mapper接口文件和.xml文件不在同一目錄下的情況,并使用@Primary注解指定默認數(shù)據源2025-11-11

