分布式配置:配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。
在生产环境,由于配置繁多,造成运维人员或开发人员配置上的不便,springcloud提供了分布式配置中心组建,支持从远程配置仓库(git)中读取配置。
读取配置流程如下图:

config-server从git中读取远程配置,config-client在从config-server中读取配置。
新建项目config-server,pom文件中加入config-server依赖,完整代码如下:
4.0.0 com.dalaoyang springcloud_config_server 0.0.1-SNAPSHOT jar springcloud_config_server springcloud_config_server org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE <!-- lookup parent from repository --> UTF-8 UTF-8 1.8 Edgware.SR1 org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
启动类加入@EnableConfigServer注解,表明这是一个config-server并开启配置服务器功能。代码如下:
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigServerApplication.class, args);
}
}
application.properties配置如下:
##端口号 server.port=9000 ##服务名称 spring.application.name=config_server ##注册中心地址 eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/ ##远程仓库配置 ##git仓库地址 spring.cloud.config.server.git.uri=https://gitee.com/dalaoyang/SpringCloudConfig.git ##git仓库配置路径 spring.cloud.config.server.git.searchPaths=config ##git仓库分支 spring.cloud.config.label=master ##如果为公开仓库,用户名密码可不填 ##git仓库用户名 ##spring.cloud.config.server.git.username= ##git仓库密码 ##spring.cloud.config.server.git.password=
config-server到这里构建完成,然后新建一个config-client服务。pom文件代码如下:
4.0.0 com.dalaoyang springcloud_config_client 0.0.1-SNAPSHOT jar springcloud_config_client springcloud_config_client org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE <!-- lookup parent from repository --> UTF-8 UTF-8 1.8 Edgware.SR1 org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
然后新建一个配置文件,这里需要说明一下,config-client默认会去找8888端口的配置中心,如果配置中心使用8888端口,config-client可以使用application.properties配置文件。如果配置中心没有使用8888端口,那么config-client要使用bootstrap.properties,原因是bootstrap.* 命名的文件会被优先读取和配置。由于这个例子的项目使用端口为9000,所以新建一个bootstrap.properties,代码如下:
spring.application.name=config-client spring.cloud.config.label=master spring.cloud.config.profile=test spring.cloud.config.uri= http://localhost:9000/ ##注册中心地址 eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
然后做一个测试类,修改一下application,代码如下:
package com.dalaoyang;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringcloudConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigClientApplication.class, args);
}
@Value("${title}")
String title;
@RequestMapping("/getTitle")
public String getTitle(){
return title;
}
}
到这里就全部构建完成了,依次启动config-server和config-client。
先访问一下http://eureka.dalaoyang.cn/
可以看到服务构建成功,然后访问http://localhost:8080/getTitle,可以看到:

源码下载 :大老杨码云
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3050/viewspace-2804718/ ,如需转载,请注明出处,否则将追究法律责任。