Add config preference example

pull/2459/head
Freeman Lau 3 years ago
parent 5d49b63200
commit e02ef61969

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<version>${revision}</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-config-preference-example</artifactId>
<name>Spring Cloud Starter Alibaba Nacos Config Preference Example</name>
<description>Example demonstrating how to use nacos config preference in spring boot</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -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`

@ -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`.

@ -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);
}
}

@ -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();
}
}

@ -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<String, Object> map;
private List<User> users;
@Data
public static class User {
private String name;
private Integer age;
}
}

@ -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

@ -26,6 +26,7 @@
<module>nacos-example/nacos-discovery-example</module>
<module>nacos-example/nacos-config-example</module>
<module>nacos-example/nacos-config-2.4.x-example</module>
<module>nacos-example/nacos-config-preference-example</module>
<module>nacos-example/nacos-gateway-example</module>
<module>seata-example/business-service</module>
<module>seata-example/order-service</module>

@ -21,6 +21,10 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

Loading…
Cancel
Save