parent
36e0ba7c14
commit
a8cf02e74f
@ -1,51 +0,0 @@
|
||||
<?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-2.4.x-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Config 2.4.x Example</name>
|
||||
<description>Example demonstrating how to use nacos config in spring boot 2.4.x</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>
|
||||
</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>
|
@ -1,99 +0,0 @@
|
||||
# Nacos Config 2.4.x Example
|
||||
|
||||
## 项目说明
|
||||
|
||||
Spring Boot 2.4.0 版本开始默认不启动 bootstrap 容器
|
||||
本项目演示如何在 Spring boot >= 2.4.0 版本不启用 bootstrap 容器情况下如何使用 nacos
|
||||
|
||||
***<font color=red>适用于 Spring boot >= 2.4.0 并且使用 import 方式导入配置,将不会再默认拉取配置,需要手动配置 dataId。</font>***
|
||||
|
||||
## 示例
|
||||
|
||||
### 如何接入
|
||||
|
||||
1. 首先,修改 pom.xml 文件,引入 Nacos Config Starter
|
||||
```xml
|
||||
<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>
|
||||
```
|
||||
|
||||
|
||||
2. 在应用的 /src/main/resources/***application.yml*** 配置文件中配置 Nacos Config 元数据
|
||||
```yaml
|
||||
server:
|
||||
port: 8888
|
||||
spring:
|
||||
application:
|
||||
name: nacos-config-import-example
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
group: DEFAULT_GROUP
|
||||
server-addr: 127.0.0.1:8848
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:test.yml
|
||||
- optional:nacos:test01.yml?group=group_02
|
||||
- optional:nacos:test02.yml?group=group_03&refreshEnabled=false
|
||||
```
|
||||
|
||||
3. 在 nacos 创建 test.yml
|
||||
```yaml
|
||||
configdata:
|
||||
user:
|
||||
age: 21
|
||||
name: freeman
|
||||
map:
|
||||
hobbies:
|
||||
- art
|
||||
- programming
|
||||
intro: Hello, I'm freeman
|
||||
users:
|
||||
- name: dad
|
||||
age: 20
|
||||
- name: mom
|
||||
age: 18
|
||||
```
|
||||
|
||||
4. 完成上述操作后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中
|
||||
```java
|
||||
// controller
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserConfig userConfig;
|
||||
|
||||
@GetMapping
|
||||
public String get() throws JsonProcessingException {
|
||||
return new ObjectMapper().writeValueAsString(userConfig);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "configdata.user")
|
||||
public class UserConfig {
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Map<String, Object> map;
|
||||
private List<User> users;
|
||||
// getters and setters ...
|
||||
|
||||
public static class User {
|
||||
private String name;
|
||||
private Integer age;
|
||||
// getters and setters ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
验证动态刷新
|
||||
访问 http://localhost:8888
|
||||
再从 nacos 修改配置, 再次访问即可验证动态配置生效
|
@ -1,99 +0,0 @@
|
||||
# Nacos Config 2.4.x Example
|
||||
|
||||
## Project Instruction
|
||||
|
||||
Spring Boot version 2.4.0 does not start the bootstrap container by default.
|
||||
This project demonstrates how to use nacos when Spring boot >= 2.4.0 version does not enable the bootstrap container.
|
||||
|
||||
***<font color=red>Applicable to Spring boot >= 2.4.0 and import the configuration using the import method, the configuration will no longer be pulled by default, and the dataId needs to be configured manually.</font>***
|
||||
|
||||
## Demo
|
||||
|
||||
### How to use
|
||||
|
||||
1. First, modify the pom.xml file and introduce Nacos Config Starter
|
||||
```xml
|
||||
<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>
|
||||
```
|
||||
|
||||
|
||||
2. Configure Nacos Config metadata in the application's src/main/resources/***application.yml*** configuration file
|
||||
```yaml
|
||||
server:
|
||||
port: 8888
|
||||
spring:
|
||||
application:
|
||||
name: nacos-config-import-example
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
group: DEFAULT_GROUP
|
||||
server-addr: 127.0.0.1:8848
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:test.yml
|
||||
- optional:nacos:test01.yml?group=group_02
|
||||
- optional:nacos:test02.yml?group=group_03&refreshEnabled=false
|
||||
```
|
||||
|
||||
3. Create test.yml in nacos
|
||||
```yaml
|
||||
configdata:
|
||||
user:
|
||||
age: 21
|
||||
name: freeman
|
||||
map:
|
||||
hobbies:
|
||||
- art
|
||||
- programming
|
||||
intro: Hello, I'm freeman
|
||||
users:
|
||||
- name: dad
|
||||
age: 20
|
||||
- name: mom
|
||||
age: 18
|
||||
```
|
||||
|
||||
4. After completing the above operations, the application will obtain the corresponding configuration from Nacos Config and add it to the PropertySources of Spring Environment
|
||||
```java
|
||||
// controller
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserConfig userConfig;
|
||||
|
||||
@GetMapping
|
||||
public String get() throws JsonProcessingException {
|
||||
return new ObjectMapper().writeValueAsString(userConfig);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "configdata.user")
|
||||
public class UserConfig {
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Map<String, Object> map;
|
||||
private List<User> users;
|
||||
// getters and setters ...
|
||||
|
||||
public static class User {
|
||||
private String name;
|
||||
private Integer age;
|
||||
// getters and setters ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Verify dynamic refresh
|
||||
access http://localhost:8888
|
||||
Then modify the configuration from nacos, and visit again to verify that the dynamic configuration takes effect.
|
@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2018 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.imports.examples;
|
||||
|
||||
import com.alibaba.cloud.imports.examples.model.UserConfig;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author freeman
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(UserConfig.class)
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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.imports.examples.controller;
|
||||
|
||||
import com.alibaba.cloud.imports.examples.model.UserConfig;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
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 UserConfig userConfig;
|
||||
|
||||
@GetMapping
|
||||
public String get() throws JsonProcessingException {
|
||||
return new ObjectMapper().writeValueAsString(userConfig);
|
||||
}
|
||||
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/*
|
||||
* 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.imports.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 UserConfig {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
server:
|
||||
port: 8888
|
||||
spring:
|
||||
application:
|
||||
name: nacos-config-import-example
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
group: DEFAULT_GROUP
|
||||
server-addr: 127.0.0.1:8848
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:test.yml
|
||||
- optional:nacos:test01.yml?group=group_02
|
||||
- optional:nacos:test02.yml?group=group_03&refreshEnabled=false
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
server.port=18084
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.endpoint.health.show-details=always
|
@ -0,0 +1,25 @@
|
||||
server:
|
||||
port: 18084
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: nacos-config-example
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
serverAddr: 127.0.0.1:8848
|
||||
config:
|
||||
import:
|
||||
- nacos:nacos-config-example.properties?refresh=true
|
||||
management:
|
||||
endpoint:
|
||||
health:
|
||||
show-details: always
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: '*'
|
||||
logging:
|
||||
level:
|
||||
com.alibaba.cloud.nacos.configdata: debug
|
||||
|
@ -1,37 +0,0 @@
|
||||
spring.application.name=nacos-config-example
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||
|
||||
#nacos certification information
|
||||
spring.cloud.nacos.username=nacos
|
||||
spring.cloud.nacos.password=nacos
|
||||
|
||||
## nacos-namespace cannot user 'public',cause by 'public' has special handing inside.
|
||||
#spring.cloud.nacos.config.namespace=public
|
||||
|
||||
## you can specify a custom name if you don't want to use the application name.
|
||||
#spring.cloud.nacos.config.name=test-aaa
|
||||
#spring.cloud.nacos.config.file-extension=yaml
|
||||
|
||||
## not recommended.
|
||||
#spring.cloud.nacos.config.refreshable-dataids=common.properties
|
||||
## not recommended.
|
||||
#spring.cloud.nacos.config.shared-data-ids=common.properties,base-common.properties
|
||||
|
||||
## recommended.
|
||||
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
|
||||
|
||||
## not recommended.
|
||||
#spring.cloud.nacos.config.ext-config[0]=ext.properties
|
||||
## recommended.
|
||||
spring.cloud.nacos.config.extension-configs[0].data-id=ext-data-source.yaml
|
||||
spring.cloud.nacos.config.extension-configs[0].refresh=true
|
||||
|
||||
# the setting of reading config log print on stdout
|
||||
logging.level.com.alibaba.cloud.nacos.client=debug
|
||||
|
||||
|
||||
spring.cloud.nacos.config.refresh-enabled=true
|
||||
|
Loading…
Reference in New Issue