[ISSUE #2559] Add examples of shared-configs and extension-configs
pull/2596/head
Steve Rao 3 years ago committed by GitHub
commit ad5edb71d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,6 +31,8 @@
- ConfigListenerExample: 监听配置信息的例子
- DockingInterfaceExample: 对接 nacos 接口,通过接口完成对配置信息增删改查的例子
- ValueAnnotationExample: 通过 @Value 注解进行配置信息获取的例子
- SharedConfigExample: 共享配置的例子
- ExtensionConfigExample: 扩展配置的例子
### 启动 Nacos Server 并添加配置
@ -62,6 +64,37 @@
spring.cloud.nacos.config.group=GROUP
spring.cloud.nacos.config.namespace=NAMESPACE
4. 添加共享配置和扩展配置
共享配置:
```
dataId为: data-source.yaml
group 为: DEFAULT_GROUP
内容如下:
spring:
datasource:
name: datasource
url: jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&useDynamicCharsetInfo=false&useSSL=false
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
```
扩展配置:
> 可以使用扩展配置覆盖共享配置中的配置
```
dataId为: ext-data-source.yaml
group 为: DEFAULT_GROUP
内容如下:
spring:
datasource:
username: ext-root
password: ext-root
```
### 应用启动
1. 增加配置,在应用的 /src/main/resources/application.properties 中添加基本配置信息

@ -32,6 +32,8 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
- ConfigListenerExample: Example of listening configuration information
- DockingInterfaceExample: An example of docking the nacos interface and completing the addition, deletion, modification and checking of configuration information through the interface
- ValueAnnotationExample: An example of obtaining configuration information through @Value annotation
- SharedConfigExample: Example of shared configuration
- ExtensionConfigExample: Example of extended configuration
### Start Nacos Server
@ -64,6 +66,38 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
spring.cloud.nacos.config.group=GROUP
spring.cloud.nacos.config.namespace=NAMESPACE
4. Add shared configuration and extended configuration
Shared configuration:
```
dataId is: data-source.yaml
group is DEFAULT_GROUP
content is:
spring:
datasource:
name: datasource
url: jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&useDynamicCharsetInfo=false&useSSL=false
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
```
Extended configuration:
> Configuration in shared configuration can be overridden with extended configuration
```
dataId is: ext-data-source.yaml
group is DEFAULT_GROUP
content is:
spring:
datasource:
username: ext-root
password: ext-root
```
### Start Application
1. Add necessary configurations to file /src/main/resources/application.properties

@ -0,0 +1,64 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples.example;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Example of extended configuration. When a configuration in the shared configuration
* does not meet the requirements, the extended configuration can be used to override the
* shared configuration. Priority: Main Configuration > Extended Configuration > Shared
* Configuration.
* @author lixiaoshuang
*/
@RestController
@RequestMapping("/nacos/extension/config")
@RefreshScope
public class ExtensionConfigExample {
@Value("${spring.datasource.name:}")
private String name;
@Value("${spring.datasource.url:}")
private String url;
@Value("${spring.datasource.username:}")
private String username;
@Value("${spring.datasource.password:}")
private String password;
@Value("${spring.datasource.driverClassName:}")
private String driverClassName;
@GetMapping
public Map<String, String> getConfigInfo() {
Map<String, String> result = new HashMap<>(4);
result.put("name", name);
result.put("url", url);
result.put("username", username);
result.put("password", password);
result.put("driverClassName", driverClassName);
return result;
}
}

@ -0,0 +1,63 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples.example;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Shared configuration example, Used to resolve multiple projects using the same
* configuration.
* @author lixiaoshuang
*/
@RestController
@RequestMapping("/nacos/shared/config")
@RefreshScope
public class SharedConfigExample {
@Value("${spring.datasource.name:}")
private String name;
@Value("${spring.datasource.url:}")
private String url;
@Value("${spring.datasource.username:}")
private String username;
@Value("${spring.datasource.password:}")
private String password;
@Value("${spring.datasource.driverClassName:}")
private String driverClassName;
@GetMapping
public Map<String, String> getConfigInfo() {
Map<String, String> result = new HashMap<>(4);
result.put("name", name);
result.put("url", url);
result.put("username", username);
result.put("password", password);
result.put("driverClassName", driverClassName);
return result;
}
}

@ -18,18 +18,16 @@ spring.cloud.nacos.password=nacos
#spring.cloud.nacos.config.shared-data-ids=common.properties,base-common.properties
## recommended.
spring.cloud.nacos.config.shared-configs[0].data-id=test2.yaml
spring.cloud.nacos.config.shared-configs[0].data-id=data-source.yaml
spring.cloud.nacos.config.shared-configs[0].refresh=true
## the default value is 'DEFAULT_GROUP' , if not specified.
spring.cloud.nacos.config.shared-configs[0].group=GROUP_APP1
#spring.cloud.nacos.config.shared-configs[0].group=GROUP_APP1
## not recommended.
#spring.cloud.nacos.config.ext-config[0]=ext.properties
## recommended.
spring.cloud.nacos.config.extension-configs[0].data-id=extension1.properties
spring.cloud.nacos.config.extension-configs[0].data-id=ext-data-source.yaml
spring.cloud.nacos.config.extension-configs[0].refresh=true
spring.cloud.nacos.config.extension-configs[1].data-id=test1.yml
spring.cloud.nacos.config.extension-configs[1].refresh=true
# the setting of reading config log print on stdout
logging.level.com.alibaba.cloud.nacos.client=debug

Loading…
Cancel
Save