diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/nacos-producer-8081/pom.xml b/nacos-producer-8081/pom.xml
index 5bfc36f..0e0f0dc 100644
--- a/nacos-producer-8081/pom.xml
+++ b/nacos-producer-8081/pom.xml
@@ -17,4 +17,12 @@
UTF-8
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-config
+
+
+
\ No newline at end of file
diff --git a/nacos-producer-8081/src/main/java/org/example/App.java b/nacos-producer-8081/src/main/java/org/example/App.java
index 4fe6697..01a23b3 100644
--- a/nacos-producer-8081/src/main/java/org/example/App.java
+++ b/nacos-producer-8081/src/main/java/org/example/App.java
@@ -2,6 +2,9 @@ package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ConfigurableApplicationContext;
+
+import java.util.concurrent.TimeUnit;
/**
* Hello world!
@@ -10,8 +13,23 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App
{
- public static void main( String[] args )
- {
- SpringApplication.run(App.class,args);
+ public static void main( String[] args ) throws InterruptedException {
+ ConfigurableApplicationContext applicationContent = SpringApplication.run(App.class, args);
+ while (true){
+ String name = applicationContent.getEnvironment().getProperty("name");
+ String password = applicationContent.getEnvironment().getProperty("password");
+ String text = applicationContent.getEnvironment().getProperty("text");
+ String common = applicationContent.getEnvironment().getProperty("common");
+ System.out.println("------name--------:"+name);
+ System.out.println("------password--------:"+password);
+ System.out.println("------common--------:"+common);
+ TimeUnit.SECONDS.sleep(1);
+ }
+
+
+ // nacos客户端 每10ms根据MD5去注册中心进行判断,如果发生变化会重新拉取
+ // 但出现客户端一致拉取的情况,原因可能是 1、客户端的nacos与服务端的nacos版本不一致,2、设置了命名空间
+
+
}
}
diff --git a/nacos-producer-8081/src/main/java/org/example/controller/ConfigController.java b/nacos-producer-8081/src/main/java/org/example/controller/ConfigController.java
new file mode 100644
index 0000000..2042648
--- /dev/null
+++ b/nacos-producer-8081/src/main/java/org/example/controller/ConfigController.java
@@ -0,0 +1,21 @@
+package org.example.controller;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/config")
+@RefreshScope
+public class ConfigController {
+
+ @Value("${name}")
+ private String name;
+
+ @RequestMapping("/get")
+ public String get() {
+ return name;
+ }
+
+}
\ No newline at end of file
diff --git a/nacos-producer-8081/src/main/resources/application-prod.yml b/nacos-producer-8081/src/main/resources/application-prod.yml
new file mode 100644
index 0000000..897e3f6
--- /dev/null
+++ b/nacos-producer-8081/src/main/resources/application-prod.yml
@@ -0,0 +1,11 @@
+server:
+ port: 8081
+# cloud:
+# nacos:
+# discovery:
+# server-addr: 127.0.0.1:8848
+#management:
+# endpoints:
+# web:
+# exposure:
+# include: *
\ No newline at end of file
diff --git a/nacos-producer-8081/src/main/resources/application.yml b/nacos-producer-8081/src/main/resources/application.yml
index 43c77a3..90385b2 100644
--- a/nacos-producer-8081/src/main/resources/application.yml
+++ b/nacos-producer-8081/src/main/resources/application.yml
@@ -1,14 +1,3 @@
-server:
- port: 8081
spring:
- application:
- name: nacos-producer
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
-#management:
-# endpoints:
-# web:
-# exposure:
-# include: *
\ No newline at end of file
+ profiles:
+ active: prod
\ No newline at end of file
diff --git a/nacos-producer-8081/src/main/resources/bootstrap.yml b/nacos-producer-8081/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..1329879
--- /dev/null
+++ b/nacos-producer-8081/src/main/resources/bootstrap.yml
@@ -0,0 +1,37 @@
+# bootstrap文件是spring cloud扩展的配置文件,关于nacos的配置信息可以放置在这里
+
+spring:
+ application:
+ # 会自动根据服务名拉去 dataid 对应的配置文件(服务名与dataid一致)
+ # 如果dataid与服务名不一致,则需要手动置顶,跟服务名一致的的dataid文件称为默认的配置文件
+ # 默认的配置文件支持profiles配置,即:nacos-producer-dev,nacos-producer-prod
+ # ${spring.application.name}-${profile}. ${file-extension:properties}
+ name: nacos-producer
+ cloud:
+ nacos:
+ # 如果服务端开启了权限管理,则必须填写用户信息和分组
+ username: nacos
+ password: nacos
+
+ discovery:
+ server-addr: 127.0.0.1:8848
+ config:
+ namespace: 64b4b4b4-c07d-4049-a3d5-34e08abcb093
+ group: test
+ server-addr: 127.0.0.1:8848
+ file-extension: yaml # nacos配置中心的配置文件默认是properties格式的(只针对默认配置文件和profile),如果改为yaml文件需要添加声明,除了默认的配置文件都需要加上文件后缀
+ #refresh-enable: false #禁止更新,一般不用
+ shared-configs: #读取公共配置
+ - data-id: producer-common.properties #下标[0]
+ group: test # 默认是DEFAULT_GROUP
+ refresh: true
+ - data-id: producer-common2.properties #下标[1]
+ group: test # 默认是DEFAULT_GROUP
+ refresh: true
+ extension-configs[0]:
+ data-id: producer-common.properties
+ group: test # 默认是DEFAULT_GROUP
+ refresh: true
+
+# 配置文件的优先级(优先级大的会覆盖优先级晓得,并且会形成互补)
+# profile > 默认配置文件 > extension-configs > shared-configs(下标越大优先级就越大)
diff --git a/pom.xml b/pom.xml
index 14e00d9..2624eb4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,6 +67,7 @@
spring-boot-starter-actuator
+
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery