From e02ef61969417e55a58a4f35b1b30f94fdb52da7 Mon Sep 17 00:00:00 2001 From: Freeman Lau Date: Wed, 16 Mar 2022 12:42:59 +0800 Subject: [PATCH] Add config preference example --- .../nacos-config-preference-example/pom.xml | 55 +++++++++++++++++++ .../readme-zh.md | 22 ++++++++ .../nacos-config-preference-example/readme.md | 22 ++++++++ .../examples/ConfigPreferenceApplication.java | 36 ++++++++++++ .../examples/controller/UserController.java | 41 ++++++++++++++ .../examples/model/UserProperties.java | 45 +++++++++++++++ .../src/main/resources/application-dev.yml | 3 + .../src/main/resources/application.yml | 25 +++++++++ spring-cloud-alibaba-examples/pom.xml | 1 + .../nacos-tests/nacos-config-test/pom.xml | 4 ++ 10 files changed, 254 insertions(+) create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme-zh.md create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme.md create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/ConfigPreferenceApplication.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/controller/UserController.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/model/UserProperties.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application-dev.yml create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application.yml diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/pom.xml new file mode 100644 index 000000000..5c6d8c1a4 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/pom.xml @@ -0,0 +1,55 @@ + + + + + com.alibaba.cloud + spring-cloud-alibaba-examples + ${revision} + ../../pom.xml + + 4.0.0 + + nacos-config-preference-example + Spring Cloud Starter Alibaba Nacos Config Preference Example + Example demonstrating how to use nacos config preference in spring boot + jar + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-configuration-processor + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme-zh.md new file mode 100644 index 000000000..49221573a --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme-zh.md @@ -0,0 +1,22 @@ +# Nacos Config Preference + +## 项目说明 + +本项目演示如何使用配置偏好配置 + +## 示例 + +1. 启动一个 Nacos server +添加配置 `test.yml` + +```yaml +configdata: + user: + name: freeman +``` + +2. 设置配置偏好 `spring.cloud.nacos.config.preference=remote` + +3. 访问 `localhost` +应该能看到值为 `freeman`,因为优先使用了配置中心配置 +修改配置为 `spring.cloud.nacos.config.preference=local` 那么值应该为 `aa` \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme.md new file mode 100644 index 000000000..a5dfa7b8e --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/readme.md @@ -0,0 +1,22 @@ +# Nacos Config Preference + +## Project instruction + +This project demonstrates how to use config preferences. + +## Example + +1. Start a Nacos server +add configuration `test.yml` + +```yaml +configdata: + user: + name: freeman +``` + +2. Set config preference `spring.cloud.nacos.config.preference=remote` + +3. access `localhost` +You should see a value of `freeman`, because the configuration center configuration is used first. +Modify the configuration to `spring.cloud.nacos.config.preference=local` then the value should be `aa`. \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/ConfigPreferenceApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/ConfigPreferenceApplication.java new file mode 100644 index 000000000..51963cf9b --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/ConfigPreferenceApplication.java @@ -0,0 +1,36 @@ +/* + * 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.configpreference.examples; + +import com.alibaba.cloud.configpreference.examples.model.UserProperties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +/** + * @author freeman + */ +@SpringBootApplication +@EnableConfigurationProperties(UserProperties.class) +public class ConfigPreferenceApplication { + + public static void main(String[] args) { + SpringApplication.run(ConfigPreferenceApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/controller/UserController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/controller/UserController.java new file mode 100644 index 000000000..2521efdd7 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/controller/UserController.java @@ -0,0 +1,41 @@ +/* + * 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.configpreference.examples.controller; + +import com.alibaba.cloud.configpreference.examples.model.UserProperties; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * + * @author freeman + */ +@RestController +public class UserController { + + @Autowired + private UserProperties userProperties; + + @GetMapping + public String getName() { + return userProperties.getName(); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/model/UserProperties.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/model/UserProperties.java new file mode 100644 index 000000000..63b6eb472 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/model/UserProperties.java @@ -0,0 +1,45 @@ +/* + * 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.configpreference.examples.model; + +import java.util.List; +import java.util.Map; + +import lombok.Data; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * + * + * @author freeman + */ +@Data +@ConfigurationProperties(prefix = "configdata.user") +public class UserProperties { + private String name; + private Integer age; + private Map map; + private List users; + + @Data + public static class User { + private String name; + private Integer age; + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application-dev.yml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application-dev.yml new file mode 100644 index 000000000..3ea6b3fac --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application-dev.yml @@ -0,0 +1,3 @@ +configdata: + user: + name: aa \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application.yml new file mode 100644 index 000000000..1274e41eb --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/resources/application.yml @@ -0,0 +1,25 @@ +server: + port: 80 +spring: + application: + name: nacos-config-import-example + cloud: + nacos: + config: + group: DEFAULT_GROUP + server-addr: 127.0.0.1:8848 + preference: remote + config: + import: + - optional:nacos:test.yml + profiles: + active: dev + +--- +spring: + config: + activate: + on-profile: dev +configdata: + user: + name: bb \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index ce939c0d0..736c7adc8 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -26,6 +26,7 @@ nacos-example/nacos-discovery-example nacos-example/nacos-config-example nacos-example/nacos-config-2.4.x-example + nacos-example/nacos-config-preference-example nacos-example/nacos-gateway-example seata-example/business-service seata-example/order-service diff --git a/spring-cloud-alibaba-tests/nacos-tests/nacos-config-test/pom.xml b/spring-cloud-alibaba-tests/nacos-tests/nacos-config-test/pom.xml index c33d0237f..0f1681b88 100644 --- a/spring-cloud-alibaba-tests/nacos-tests/nacos-config-test/pom.xml +++ b/spring-cloud-alibaba-tests/nacos-tests/nacos-config-test/pom.xml @@ -21,6 +21,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config + + org.springframework.boot + spring-boot-configuration-processor + org.projectlombok lombok