mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1531 from zkzlx/config
[fix & enhance issue #1492 #1506 #1257 #1578 ]Improved nacos configuration parsing,
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -20,7 +20,6 @@ import com.alibaba.cloud.examples.service.EchoService;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
* <p>
|
||||
* sentinel 降级处理
|
||||
*/
|
||||
@@ -35,7 +34,7 @@ public class EchoServiceFallback implements EchoService {
|
||||
/**
|
||||
* 调用服务提供方的输出接口.
|
||||
* @param str 用户输入
|
||||
* @return
|
||||
* @return String
|
||||
*/
|
||||
@Override
|
||||
public String echo(String str) {
|
||||
|
@@ -22,7 +22,6 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
*/
|
||||
@Component
|
||||
public class EchoServiceFallbackFactory implements FallbackFactory<EchoServiceFallback> {
|
||||
|
@@ -24,7 +24,6 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
* <p>
|
||||
* example feign client
|
||||
*/
|
||||
|
@@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
*/
|
||||
@RestController
|
||||
public class EchoController {
|
||||
|
Reference in New Issue
Block a user