mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
merge from 2.2.4.RELEASE
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
<description>Example demonstrating how to use nacos config</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@@ -175,7 +175,7 @@ AccessKey|spring.cloud.nacos.config.access-key||
|
||||
SecretKey|spring.cloud.nacos.config.secret-key||
|
||||
相对路径|spring.cloud.nacos.config.context-path||服务端 API 的相对路径
|
||||
接入点|spring.cloud.nacos.config.endpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
|
||||
是否开启监听和自动刷新|spring.cloud.nacos.config.refresh.enabled|true|
|
||||
是否开启监听和自动刷新|spring.cloud.nacos.config.refresh-enabled|true|
|
||||
|
||||
|
||||
|
||||
|
@@ -25,6 +25,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||
|
||||
3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh .
|
||||
|
||||
@RefreshScope
|
||||
class SampleController {
|
||||
|
||||
|
@@ -18,6 +18,7 @@ package com.alibaba.cloud.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -34,7 +35,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -62,8 +65,12 @@ class UserConfig {
|
||||
|
||||
private String name;
|
||||
|
||||
private String hr;
|
||||
|
||||
private Map<String, Object> map;
|
||||
|
||||
private List<User> users;
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
@@ -88,10 +95,65 @@ class UserConfig {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(List<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
public String getHr() {
|
||||
return hr;
|
||||
}
|
||||
|
||||
public void setHr(String hr) {
|
||||
this.hr = hr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserConfig{" + "age=" + age + ", name='" + name + '\'' + ", map=" + map
|
||||
+ '}';
|
||||
+ ", hr='" + hr + '\'' + ", users=" + users + '}';
|
||||
}
|
||||
|
||||
public static class User {
|
||||
|
||||
private String name;
|
||||
|
||||
private String hr;
|
||||
|
||||
private String avg;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getHr() {
|
||||
return hr;
|
||||
}
|
||||
|
||||
public void setHr(String hr) {
|
||||
this.hr = hr;
|
||||
}
|
||||
|
||||
public String getAvg() {
|
||||
return avg;
|
||||
}
|
||||
|
||||
public void setAvg(String avg) {
|
||||
this.avg = avg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name='" + name + '\'' + ", hr=" + hr + ", avg=" + avg + '}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -99,7 +161,7 @@ class UserConfig {
|
||||
@Component
|
||||
class SampleRunner implements ApplicationRunner {
|
||||
|
||||
@Value("${user.name}")
|
||||
@Value("${user.name:zz}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age:25}")
|
||||
@@ -156,7 +218,10 @@ class SampleController {
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@Value("${user.name}")
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Value("${user.name:zz}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age:25}")
|
||||
@@ -168,6 +233,11 @@ class SampleController {
|
||||
+ userConfig + "!" + nacosConfigManager.getConfigService();
|
||||
}
|
||||
|
||||
@RequestMapping("/get/{name}")
|
||||
public String getValue(@PathVariable String name) {
|
||||
return String.valueOf(environment.getProperty(name));
|
||||
}
|
||||
|
||||
@RequestMapping("/bool")
|
||||
public boolean bool() {
|
||||
return (Boolean) (userConfig.getMap().get("2"));
|
||||
|
@@ -5,19 +5,26 @@ spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||
spring.cloud.nacos.username=nacos
|
||||
spring.cloud.nacos.password=nacos
|
||||
|
||||
#spring.cloud.nacos.config.namespace=public
|
||||
spring.cloud.nacos.config.name=test-aaa
|
||||
spring.cloud.nacos.config.file-extension=yaml
|
||||
|
||||
#spring.cloud.nacos.config.refreshable-dataids=common.properties
|
||||
#spring.cloud.nacos.config.shared-data-ids=common.properties,base-common.properties
|
||||
spring.cloud.nacos.config.shared-configs[0]= common333.properties
|
||||
spring.cloud.nacos.config.shared-configs[1].data-id= common111.properties
|
||||
spring.cloud.nacos.config.shared-configs[1].group= GROUP_APP1
|
||||
spring.cloud.nacos.config.shared-configs[1].refresh= true
|
||||
spring.cloud.nacos.config.shared-configs[2]= common222.properties
|
||||
#spring.cloud.nacos.config.shared-configs[0]= common333.properties
|
||||
#spring.cloud.nacos.config.shared-configs[1].data-id= common111.properties
|
||||
#spring.cloud.nacos.config.shared-configs[1].group= GROUP_APP1
|
||||
#spring.cloud.nacos.config.shared-configs[1].refresh= true
|
||||
#spring.cloud.nacos.config.shared-configs[2]= common222.properties
|
||||
spring.cloud.nacos.config.shared-configs[0].data-id= test2.yaml
|
||||
spring.cloud.nacos.config.shared-configs[0].refresh=true
|
||||
|
||||
#spring.cloud.nacos.config.ext-config[0]=ext.properties
|
||||
spring.cloud.nacos.config.extension-configs[0].data-id= extension1.properties
|
||||
spring.cloud.nacos.config.extension-configs[0].refresh= true
|
||||
spring.cloud.nacos.config.extension-configs[1]= extension2.properties
|
||||
spring.cloud.nacos.config.extension-configs[2].data-id= extension3.json
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -15,6 +15,7 @@
|
||||
<description>Example demonstrating how to use nacos discovery</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@@ -9,11 +9,13 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-discovery-provider-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Discovery Provider Example</name>
|
||||
<description>Example demonstrating how to use nacos discovery</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@@ -1,6 +1,7 @@
|
||||
server.port=18082
|
||||
spring.application.name=service-provider
|
||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||
#spring.cloud.nacos.discovery.instance-enabled=true
|
||||
|
||||
spring.cloud.nacos.username=nacos
|
||||
spring.cloud.nacos.password=nacos
|
||||
|
@@ -9,11 +9,13 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-discovery-spring-cloud-config-client-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Discovery x Config Client Example</name>
|
||||
<description>Example demonstrating how to use nacos discovery</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@@ -9,11 +9,13 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-discovery-spring-cloud-config-server-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Discovery x Config Server Example</name>
|
||||
<description>Example demonstrating how to use nacos discovery</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@@ -15,6 +15,7 @@
|
||||
<description>Example demonstrating how to use nacos discovery</description>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<modules>
|
||||
<module>nacos-discovery-consumer-example</module>
|
||||
<module>nacos-discovery-provider-example</module>
|
||||
|
@@ -30,7 +30,7 @@
|
||||
public class ProviderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
SpringApplication.run(ProviderApplication.class, args);
|
||||
}
|
||||
|
||||
@RestController
|
||||
|
@@ -29,7 +29,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
|
||||
public class ProviderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
SpringApplication.run(ProviderApplication.class, args);
|
||||
}
|
||||
|
||||
@RestController
|
||||
@@ -207,6 +207,7 @@ Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using M
|
||||
log name|spring.cloud.nacos.discovery.log-name||
|
||||
endpoint|spring.cloud.nacos.discovery.endpoint||The domain name of a service, through which the server address can be dynamically obtained.
|
||||
Integration Ribbon|ribbon.nacos.enabled|true|
|
||||
enabled|spring.cloud.nacos.discovery.enabled|true|The switch to enable or disable nacos service discovery
|
||||
|
||||
|
||||
|
||||
|
@@ -9,11 +9,13 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-gateway-discovery-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Discovery x Gateway Example</name>
|
||||
<description>Example demonstrating how to use gateway with nacos</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
@@ -9,11 +9,13 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-gateway-provider-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Discovery x Gateway - Provider Example</name>
|
||||
<description>Example demonstrating how to use gateway with nacos</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@@ -10,11 +10,13 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-gateway-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Nacos Discovery x Gateway Examples</name>
|
||||
<description>Example demonstrating how to use gateway with nacos</description>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<modules>
|
||||
<module>nacos-gateway-discovery-example</module>
|
||||
<module>nacos-gateway-provider-example</module>
|
||||
|
Reference in New Issue
Block a user