diff --git a/cloud-demo/ReadMe.md b/cloud-demo/ReadMe.md
index bbb6c3c..b95008c 100644
--- a/cloud-demo/ReadMe.md
+++ b/cloud-demo/ReadMe.md
@@ -405,3 +405,112 @@ private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
return restTemplate.getForObject(url, Product.class);
}
```
+
+### 远程配置读取
+
+#### 1、引入依赖
+
+```xml
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+```
+
+#### 2、在nacos中配置
+
+
+
+
+
+```yml
+order:
+ timeout: 30min
+ auto-confirm: true
+```
+
+#### 3、创建接口访问
+
+```java
+@RestController
+@RequestMapping("/api/order")
+@RequiredArgsConstructor
+public class OrderController {
+
+ private final OrderService orderService;
+
+ @Value("${order.timeout}")
+ private String timeout;
+
+ @Value("${order.auto-confirm}")
+ private String autoConfirm;
+
+ @Operation(summary = "读取配置")
+ @GetMapping("config")
+ public String config() {
+ return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
+ }
+}
+```
+
+#### 4、SpringBoot中配置
+
+> [!CAUTION]
+>
+> 需要注意的是,不要在`server-addr`地址中写`${nacos.server-addr}`,否则无法访问正确的地址。
+
+> [!NOTE]
+>
+> 如果某个项目模块暂时不需要动态配置,但是引入了,可以在配置中加上,导入检查。
+>
+> ```yaml
+> cloud:
+> nacos:
+> server-addr: 192.168.95.135:8848
+> config:
+> import-check:
+> enabled: false
+> ```
+>
+> 如果某个配置是可选也可以在前面加上`ptional:`。
+>
+> ```yaml
+> spring:
+> config:
+> import:
+> - optional:nacos:service-order.yml
+> ```
+
+```yaml
+server:
+ port: 8000
+spring:
+ application:
+ name: service-order
+ profiles:
+ active: dev
+ config:
+ import:
+ - nacos:service-order.yml
+ cloud:
+ nacos:
+ server-addr: 192.168.95.135:8848
+ config:
+ import-check:
+ enabled: false
+```
+
+#### 5、动态刷新读取
+
+如果需要动态刷新需要在控制器或者是启动类上加上`@RefreshScope`
+
+```java
+@SpringBootApplication
+@EnableDiscoveryClient
+@RefreshScope
+public class ProductServiceApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(ProductServiceApplication.class, args);
+ }
+}
+```
diff --git a/cloud-demo/images/image-20250526152842107.png b/cloud-demo/images/image-20250526152842107.png
new file mode 100644
index 0000000..c667a90
Binary files /dev/null and b/cloud-demo/images/image-20250526152842107.png differ
diff --git a/cloud-demo/images/image-20250526152904745.png b/cloud-demo/images/image-20250526152904745.png
new file mode 100644
index 0000000..c07dd22
Binary files /dev/null and b/cloud-demo/images/image-20250526152904745.png differ
diff --git a/cloud-demo/services/pom.xml b/cloud-demo/services/pom.xml
index 500b4bc..5ebf59a 100644
--- a/cloud-demo/services/pom.xml
+++ b/cloud-demo/services/pom.xml
@@ -64,6 +64,10 @@
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-config
+
org.springframework.cloud
spring-cloud-starter-openfeign
diff --git a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java
index 19e7969..8d2d02e 100644
--- a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java
+++ b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java
@@ -3,9 +3,11 @@ package cn.bunny.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@EnableDiscoveryClient
+@RefreshScope
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
diff --git a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/controller/OrderController.java b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/controller/OrderController.java
index 12f02ab..d268ae2 100644
--- a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/controller/OrderController.java
+++ b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/controller/OrderController.java
@@ -4,6 +4,7 @@ import cn.bunny.model.order.bean.Order;
import cn.bunny.service.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -15,9 +16,21 @@ public class OrderController {
private final OrderService orderService;
+ @Value("${order.timeout}")
+ private String timeout;
+
+ @Value("${order.auto-confirm}")
+ private String autoConfirm;
+
@Operation(summary = "创建订单")
@GetMapping("create")
public Order createOrder(Long userId, Long productId) {
return orderService.createOrder(productId, userId);
}
+
+ @Operation(summary = "读取配置")
+ @GetMapping("config")
+ public String config() {
+ return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
+ }
}
diff --git a/cloud-demo/services/service-order/src/main/resources/application-dev.yaml b/cloud-demo/services/service-order/src/main/resources/application-dev.yaml
index d0b170d..8ba16c0 100644
--- a/cloud-demo/services/service-order/src/main/resources/application-dev.yaml
+++ b/cloud-demo/services/service-order/src/main/resources/application-dev.yaml
@@ -1,6 +1,3 @@
server:
port: 8000
-
-nacos:
- server-addr: 192.168.95.135:8848
diff --git a/cloud-demo/services/service-order/src/main/resources/application.yaml b/cloud-demo/services/service-order/src/main/resources/application.yaml
index 2715228..f560f55 100644
--- a/cloud-demo/services/service-order/src/main/resources/application.yaml
+++ b/cloud-demo/services/service-order/src/main/resources/application.yaml
@@ -5,6 +5,12 @@ spring:
name: service-order
profiles:
active: dev
+ config:
+ import:
+ - nacos:service-order.yml
cloud:
nacos:
- server-addr: ${nacos.server-addr}
+ server-addr: 192.168.95.135:8848
+ config:
+ import-check:
+ enabled: false
\ No newline at end of file
diff --git a/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java b/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java
index ef092ea..38dc31e 100644
--- a/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java
+++ b/cloud-demo/services/service-product/src/main/java/cn/bunny/service/ProductServiceApplication.java
@@ -3,9 +3,11 @@ package cn.bunny.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@EnableDiscoveryClient
+@RefreshScope
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
diff --git a/cloud-demo/services/service-product/src/main/resources/application.yaml b/cloud-demo/services/service-product/src/main/resources/application.yaml
index b858b01..478cea8 100644
--- a/cloud-demo/services/service-product/src/main/resources/application.yaml
+++ b/cloud-demo/services/service-product/src/main/resources/application.yaml
@@ -5,6 +5,10 @@ spring:
name: service-product
profiles:
active: dev
+
cloud:
nacos:
server-addr: ${nacos.server-addr}
+ config:
+ import-check:
+ enabled: false