1️⃣ 工程结构
nacos-demo├── pom.xml└── src├── main│ ├── java│ │ └── com.example.nacosdemo│ │ ├── NacosDemoApplication.java│ │ ├── config│ │ │ └── AppProperties.java│ │ └── controller│ │ └── HelloController.java│ └── resources│ └── bootstrap.yml
2️⃣ pom.xml
使用 Spring Boot + Spring Cloud Alibaba Nacos Config 依赖:
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>nacos-demo</artifactId><version>1.0.0</version><properties><java.version>17</java.version><spring.boot.version>3.2.5</spring.boot.version><spring.cloud.version>2023.0.1</spring.cloud.version><spring.cloud.alibaba.version>2023.0.1.0</spring.cloud.alibaba.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring.boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring.cloud.alibaba.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Nacos Config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- 方便调试 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>
</project>
3️⃣ bootstrap.yml
bootstrap.yml
会在应用启动早期加载,确保 Nacos 配置优先注入:
spring:application:name: nacos-democloud:nacos:config:server-addr: 127.0.0.1:8848namespace: publicgroup: DEFAULT_GROUPfile-extension: yamlrefresh-enabled: truemanagement:endpoints:web:exposure:include: "*"
4️⃣ 配置类 AppProperties.java
package com.example.nacosdemo.config;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;@Component
@RefreshScope // 支持动态刷新
@ConfigurationProperties(prefix = "app")
public class AppProperties {private String message = "default message";public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
5️⃣ 控制器 HelloController.java
package com.example.nacosdemo.controller;import com.example.nacosdemo.config.AppProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {private final AppProperties appProperties;public HelloController(AppProperties appProperties) {this.appProperties = appProperties;}@GetMapping("/hello")public String hello() {return "Message from Nacos: " + appProperties.getMessage();}
}
6️⃣ 启动类 NacosDemoApplication.java
package com.example.nacosdemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class NacosDemoApplication {public static void main(String[] args) {SpringApplication.run(NacosDemoApplication.class, args);}
}
7️⃣ Nacos 配置中心添加配置
在 Nacos 控制台添加一个配置:
- Data ID:
nacos-demo.yaml
(与spring.application.name
+.yaml
对应) - Group:
DEFAULT_GROUP
- 内容:
app:message: Hello from Nacos!
8️⃣ 启动与测试
- 启动本地 Nacos(默认端口 8848)
- 启动 Spring Boot 应用
- 访问
http://localhost:8080/hello
→ 返回Message from Nacos: Hello from Nacos!
- 在 Nacos 控制台修改
app.message
→ 保存 - 再次访问
/hello
,无需重启应用即可看到新值(因为@RefreshScope
+refresh-enabled: true
)
✅ 总结
这个示例展示了:
@ConfigurationProperties
从 Nacos 拉取配置@RefreshScope
支持动态刷新bootstrap.yml
提前加载远程配置- 最小依赖组合,直接可运行