From 36e0ba7c14b6f2c2655e4cd95327d64f153a0fab Mon Sep 17 00:00:00 2001 From: RuanSheng Date: Thu, 19 Jan 2023 16:58:13 +0800 Subject: [PATCH] update nacos-config doc (#3108) update nacos-config doc. --- .../src/main/asciidoc-zh/nacos-config.adoc | 58 +++++++++++------- .../src/main/asciidoc/nacos-config.adoc | 61 ++++++++++++------- 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc index 80799e0a4..e194bd088 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc @@ -39,8 +39,9 @@ Group : DEFAULT_GROUP 配置格式: Properties -配置内容: user.name=nacos-config-properties - user.age=90 +配置内容: +user.name=jack +user.age=90 ---- NOTE: 注意DataId是以 properties(默认的文件扩展名方式)为扩展名。 @@ -73,9 +74,8 @@ public class NacosConfigApplication { } ---- -NOTE: 注意当您```spring-cloud-alibaba```的版本为 ````2021.1```时,由于```nacos```获取配置时```bootstrap.yml ```文件将先于```application.yml```文件加载. -根据spring的官方文档中提及 [bootstrap](https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap) -为解决此问题 我们建议您在项目根```pom.xml```文件中加上如下依赖 +NOTE: ``spring-cloud-alibaba``从``2021.0.1.0``版本开始,默认使用``spring.config.import``方式引入配置,如果您想继续使用``bootstrap`` +方式加载配置,您可以在项目根``pom.xml``文件中加入以下依赖: [source,xml] ---- @@ -87,14 +87,22 @@ NOTE: 注意当您```spring-cloud-alibaba```的版本为 ````2021.1```时,由于 ---- -在运行此 NacosConfigApplication 之前, 必须使用 `bootstrap.properties` 配置文件来配置 Nacos Server 地址,例如: +在运行此 NacosConfigApplication 之前,您需要先配置 `application.yaml` 文件中的 Nacos Server 地址,并引入对应的配置,例如: -.bootstrap.properties -[source,properties] +.application.yaml +[source,yaml] ---- -# DataId 默认使用 `spring.application.name` 配置跟文件扩展名结合(配置格式默认使用 properties), GROUP 不配置默认使用 DEFAULT_GROUP。因此该配置文件对应的 Nacos Config 配置的 DataId 为 nacos-config.properties, GROUP 为 DEFAULT_GROUP -spring.application.name=nacos-config -spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +# GROUP 不配置默认使用 DEFAULT_GROUP。 +spring: + application: + name: nacos-config + cloud: + nacos: + config: + serverAddr: 127.0.0.1:8848 + config: + import: + - nacos:nacos-config.properties ---- NOTE: 注意当你使用域名的方式来访问 Nacos 时,`spring.cloud.nacos.config.server-addr` 配置的方式为 `域名:port`。 @@ -106,20 +114,28 @@ NOTE: 注意当你使用域名的方式来访问 Nacos 时,`spring.cloud.nacos [source,subs="normal"] ---- 2018-11-02 14:24:51.638 INFO 32700 --- [main] c.a.demo.provider.ConfigApplication : Started ConfigApplication in 14.645 seconds (JVM running for 15.139) -user name :nacos-config-properties; age: 90 -2018-11-02 14:24:51.688 INFO 32700 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@a8c5e74: startup date [Fri Nov 02 14:24:51 CST 2018]; root of context hierarchy +user name :jack; age: 90 ---- === 基于 DataId 为 yaml 的文件扩展名配置方式 Nacos Config 除了支持 properties 格式以外,也支持 yaml 格式。这个时候只需要完成以下两步: -1、在应用的 bootstrap.properties 配置文件中显示的声明 DataId 文件扩展名。如下所示 +1、在应用的 application.yaml 配置文件中引入配置,如下所示 -.bootstrap.properties +.application.yaml [source,yaml] ---- -spring.cloud.nacos.config.file-extension=yaml +spring: + application: + name: nacos-config + cloud: + nacos: + config: + serverAddr: 127.0.0.1:8848 + config: + import: + - nacos:nacos-config.yaml ---- 2、在 Nacos 的控制台新增一个DataId为yaml为扩展名的配置,如下所示: @@ -132,17 +148,17 @@ Group : DEFAULT_GROUP 配置格式: YAML -配置内容: user.name: nacos-config-yaml - user.age: 68 +配置内容: +user: + name: rose + age: 68 ---- 这两步完成后,重启测试程序,可以看到如下输出结果。 [source,subs="normal"] ---- -2018-11-02 14:59:00.484 INFO 32928 --- [main] c.a.demo.provider.ConfigApplication:Started ConfigApplication in 14.183 seconds (JVM running for 14.671) -user name :nacos-config-yaml; age: 68 -2018-11-02 14:59:00.529 INFO 32928 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@265a478e: startup date [Fri Nov 02 14:59:00 CST 2018]; root of context hierarchy +user name :rose; age: 68 ---- === 支持配置的动态更新 diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc index fea5b7fe2..5e382b271 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc @@ -39,8 +39,9 @@ Group : DEFAULT_GROUP Configuration format: Properties -Configuration content: user.name=nacos-config-properties - user.age=90 +Configuration content: +user.name=jack +user.age=90 ---- NOTE: The default file extension of DataId is properties. @@ -73,9 +74,8 @@ public class NacosConfigApplication { } ---- -NOTE: Note that when your ```spring-cloud-alibaba```'s version is ````2021.1```, since the ```nacos``` gets the configuration in the ```bootstrap.yml ``` file Will be loaded before the ```application.yml``` file. -According to the official documentation of spring mentioned [bootstrap](https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap) -To solve this problem, we recommend that you add the following dependencies to the project root ```pom.xml``` file +NOTE: After ``spring-cloud-alibaba`` ``2021.0.1.0``,we use ``spring.config.import`` import configuration by default, +if you want to continue to use ``bootstrap``,you can add the following dependencies to the project root ``pom.xml`` file: [source,xml] ---- @@ -86,14 +86,23 @@ To solve this problem, we recommend that you add the following dependencies to t ---- -Before running this example, we need to configure the address of the Nacos server in bootstrap.properties. For example: -.bootstrap.properties -[source,properties] +Before running this example, we need to configure the address of the Nacos server in `application.yaml`. For example: + +.`application.yaml` +[source,yaml] ---- -# DataId By default, the `spring.application.name` configuration is combined with the file extension (the configuration format uses properties by default), and the GROUP is not configured to use DEFAULT_GROUP by default. Therefore, the Nacos Config configuration corresponding to the configuration file has a DataId of nacos-config.properties and a GROUP of DEFAULT_GROUP -spring.application.name=nacos-config -spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +# GROUP is not configured to use DEFAULT_GROUP by default. +spring: + application: + name: nacos-config + cloud: + nacos: + config: + serverAddr: 127.0.0.1:8848 + config: + import: + - nacos:nacos-config.properties ---- NOTE: If you use domain name to access Nacos, the format of `spring.cloud.nacos.config.server-addr` should be `Domain name:port`. @@ -105,20 +114,28 @@ Run this example and you can see the following output: [source,subs="normal"] ---- 2018-11-02 14:24:51.638 INFO 32700 --- [main] c.a.demo.provider.NacosConfigApplication : Started NacosConfigApplication in 14.645 seconds (JVM running for 15.139) -user name :nacos-config-properties; age: 90 -2018-11-02 14:24:51.688 INFO 32700 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@a8c5e74: startup date [Fri Nov 02 14:24:51 CST 2018]; root of context hierarchy +user name :jack; age: 90 ---- === Add Configurations with DataId in YAML Format Nacos Config supports yaml format as well. You only need to complete the following 2 steps. -1、In the bootstrap.properties file, add the following line to claim that the format of DataId is yaml. As follows: +1、In the application.yaml file, add the following line to claim that the format of DataId is yaml. As follows: -.bootstrap.properties +.application.yaml [source,yaml] ---- -spring.cloud.nacos.config.file-extension=yaml +spring: + application: + name: nacos-config + cloud: + nacos: + config: + serverAddr: 127.0.0.1:8848 + config: + import: + - nacos:nacos-config.yaml ---- 2、Add a configuration with the DataId in yaml format on the Nacos console, as shown below: @@ -131,20 +148,20 @@ Group : DEFAULT_GROUP Configuration format: YAML -Configuration content: user.name: nacos-config-yaml - user.age: 68 +Configuration content: +user: + name: rose + age: 68 ---- After completing the preivous two steps, restart the testing program and you will see the following result. [source,subs="normal"] ---- -2018-11-02 14:59:00.484 INFO 32928 --- [main] c.a.demo.provider.NacosConfigApplication:Started NacosConfigApplication in 14.183 seconds (JVM running for 14.671) -user name :nacos-config-yaml; age: 68 -2018-11-02 14:59:00.529 INFO 32928 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@265a478e: startup date [Fri Nov 02 14:59:00 CST 2018]; root of context hierarchy +user name :rose; age: 68 ---- -=== Support Dynamic Configuration Udpates +=== Support Dynamic Configuration Updates Nacos Config also supports dynamic configuration updates. The code for starting Spring Boot application testing is as follows: