SpringBoot讀取Nacos上配置文件的步驟詳解
前言
在 Spring Boot 應(yīng)用程序中,可以使用 Spring Cloud Nacos 來實(shí)現(xiàn)從 Nacos 服務(wù)注冊中心和配置中心讀取配置信息。以下是如何在 Spring Boot 中讀取 Nacos 上的配置文件的步驟:
1. 引入依賴
首先,在 Spring Boot 項(xiàng)目的 pom.xml 文件中添加 Spring Cloud Nacos 的依賴:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>2. 配置 Nacos 連接信息
將 Nacos 服務(wù)注冊中心和配置中心的地址、命名空間等相關(guān)信息添加到 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? 注解來讀取配置項(xiàng):
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ā)生變化時(shí)動(dòng)態(tài)刷新配置,可以在需要?jiǎng)討B(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
}當(dāng)配置發(fā)生變化后,可以通過訪問 Actuator 端點(diǎn) ?/actuator/refresh? 來觸發(fā)配置的刷新,以便及時(shí)獲取最新的配置信息。
如果nacos上面有很多個(gè)配置文件,springboot中如何獲取想要的配置文件?
1.在 Nacos 配置中心創(chuàng)建多個(gè)配置文件,例如 ?user.properties? 和 ?database.properties?,并添加相應(yīng)的鍵值對。
2.在 Spring Boot 項(xiàng)目的 ?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? 注解來讀取指定的配置文件內(nèi)容,例如:
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 值指定了要綁定的配置文件的前綴,可以直接讀取到該配置文件中的相關(guān)屬性值。
注意: 在使用 ?@ConfigurationProperties? 注解時(shí),需要確保屬性名與配置文件中的鍵名一致,Spring Boot 會(huì)自動(dòng)根據(jù)前綴匹配來綁定配置項(xiàng)。
4.多文件配置和自動(dòng)刷新也可參考如下配置:

以上就是SpringBoot讀取Nacos上配置文件的步驟詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot讀取Nacos配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java用20行代碼實(shí)現(xiàn)抖音小視頻批量轉(zhuǎn)換為gif動(dòng)態(tài)圖
這篇文章主要介紹了Java用20行代碼實(shí)現(xiàn)抖音小視頻批量轉(zhuǎn)換為gif動(dòng)態(tài)圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringCloud實(shí)戰(zhàn)之Zuul網(wǎng)關(guān)服務(wù)
服務(wù)網(wǎng)關(guān)是分布式架構(gòu)中不可缺少的組成部分,是外部網(wǎng)絡(luò)和內(nèi)部服務(wù)之間的屏障。這篇文章主要介紹了SpringCloud實(shí)戰(zhàn)之Zuul網(wǎng)關(guān)服務(wù)。一起跟隨小編過來看看吧2018-05-05
SpringBoot Mybatis多數(shù)據(jù)源配置全過程
本文詳細(xì)介紹了如何在Java項(xiàng)目中配置多數(shù)據(jù)源,包括配置文件設(shè)置、多數(shù)據(jù)源配置類、MyBatis配置等,通過兩種不同的方式配置primary和secondary數(shù)據(jù)源,展示了如何處理Mapper接口文件和.xml文件不在同一目錄下的情況,并使用@Primary注解指定默認(rèn)數(shù)據(jù)源2025-11-11

