接口名为 : /springbootService/server01,完整接口路径如下
http://127.0.0.1:10001/springbootService/server01
注 : springbootService在其他服务中定义,server01为配置名称
接口响应格式与内容如下
{
"name":"springbootService",
"profiles":[
"server01"
],
"label":null,
"propertySources":[
{
"name":"http://127.0.0.1:10001/springbootService/server01",
"source":{
"name":"001",
"server.port":"8002"
}
}
],
"version":null,
"state":null
}
说明 : 只需要关注 source 属性即可
对象类如下
Environment
package com.aliyun.industry.configuration.biz.model.config;
import java.util.List;
/**
* @Auther: liyue
* @Date: 2020/5/6 18:13
* @Description:
*/
public class Environment {
private String name;
private String[] profiles;
private String label;
private List propertySources;
private String version;
private String state;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getProfiles() {
return profiles;
}
public void setProfiles(String[] profiles) {
this.profiles = profiles;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List getPropertySources() {
return propertySources;
}
public void setPropertySources(List propertySources) {
this.propertySources = propertySources;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Environment() {
}
public Environment(String name, String[] profiles, String label, List propertySources, String version, String state) {
this.name = name;
this.profiles = profiles;
this.label = label;
this.propertySources = propertySources;
this.version = version;
this.state = state;
}
}
PropertySource
import java.util.Map;
/**
* @Auther: liyue
* @Date: 2020/5/6 18:14
* @Description:
*/
public class PropertySource {
private String name;
private Map, ?> source;
public PropertySource() {
}
public PropertySource(String name, Map, ?> source) {
this.name = name;
this.source = source;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map, ?> getSource() {
return source;
}
public void setSource(Map, ?> source) {
this.source = source;
}
}
封装工具类 : ConfigWrapperUtil ; type是配置文件名,content是map类型,key是配置名称,value是配置值
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @Auther: liyue
* @Date: 2020/5/8 17:09
* @Description:
*/
public class ConfigWrapperUtil {
public static Environment wrapper(String type, Map content){
Environment environment = new Environment();
environment.setName("springbootService");
environment.setLabel(null);
String[] profiles = {type};
environment.setProfiles(profiles);
PropertySource propertySource = new PropertySource("http://127.0.0.1:10001/springbootService/"+type,content);
List propertySources = new LinkedList(){{
add(propertySource);
}};
environment.setPropertySources(propertySources);
return environment;
}
}
控制层 : SpringCloudConfigController
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.TreeMap;
/**
* @Auther: liyue
* @Date: 2020/5/8 17:05
* @Description:
*/
@RestController
public class SpringCloudConfigController {
@RequestMapping("/{springbootService}/{type}")
public Object getMap(@PathVariable String type) {
Map map = new TreeMap<>();
map.put("server.port", "8002");
map.put("name", "张三");
return ConfigWrapperUtil.wrapper(type, map);
}
}
注 : springbootService 两边不加大括号的话,该名称必须和其他服务的 spring.application.name 配置的数值对应才能读取,两边加了大括号之后可以通用匹配,不需要管其他服务的 spring.application.name 配置内容
引入maven配置
org.springframework.cloud
spring-cloud-starter-config
1.4.0.RELEASE
resources下新建 bootstrap.properties 文件,profile为接口的第二个URI,内容如下
spring.cloud.config.uri=http://localhost:10001/
spring.cloud.config.profile=server01
application.properties 中定义 spring.application.name,改数值必须与配置接口的第一个URI相同
spring.application.name=springCloudConfig
server.port=8001
配置接口正常访问时,启动测试服务,若以8002端口运行说明成功,8001端口说明获取配置失败
END。