一、简介git
Spring Cloud Config :分布式配置中心,方便服务配置文件统一管理,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。github
二、config server从本地读取配置文件web
2.一、在父工程的pom.xml中的 <modules>节点下 添加以下代码spring
<module>config-server</module> <module>config-client</module>
2.二、建立config-server工程,pom.xml文件以下apache
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lishun</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.lishun</groupId> <artifactId>cloud</artifactId> <version>1.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
2.三、在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码以下:bootstrap
@EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
2.四、application.properties配置文件,由于配置文件内容较多,感受yml格式配置起来不太直观,因此使用.properties格式服务器
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.native.search-locations= classpath:/shared
spring.profiles.active=native
spring.profiles.active = native指定从本地读取配置,读取的路径为classpath下的shared目录 app
2.五、在工程的 Resources 目录下建一个 shared 文件夹,用于存放本地配置文件。在 shared 目录下,新建一个 config-client-dev.properties文件,内容以下maven
id = config-test-native
name = study-cloud
三、构建config-client分布式
3.一、建立config-server工程,pom.xml文件以下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lishun</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.lishun</groupId> <artifactId>cloud</artifactId> <version>1.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> </project>
3.二、配置文件bootstrap.properties。
spring.application.name=config-client
spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.fail-fast=true
spring.profiles.active=dev
server.port=8881
注意这里是bootstrap.properties,而不是application.properties。bootstrap相对于application具备优先的执行顺序,在配置文件中spring.application.name指明了服务名称,spring.cloud.config.uri指定读取配置文件的路径,
若是没有读取成功,执行快速失败(fail-fast),spring.profiles.active指明读取的是dev文件,变量 { spring. application.name}和变量 { spring.profiles.active},二者以“-”相连,构成了向config server读取的配置文件名
因此这里读取的配置文件是config-client-dev.properties
3.三、程序的入口类,写一个API接口“/hi”,返回从配置中心读取的id变量的值,代码以下:
@SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${id}") String id; @RequestMapping(value = "/hi") public String hi(){ return id; } }
3.四、启动config server 和config client工程,打开网址访问:http://localhost:8881/hi,网页显示:
config-test-native
四、从git远程仓库读取配置文件
4.一、修改config-server的配置文件application.properties,修改内容以下:
spring.application.name=config-server
server.port=8888
#spring.cloud.config.server.native.search-locations= classpath:/shared
#spring.profiles.active=native
spring.cloud.config.server.git.uri=https://github.com/lis-ylfy/config-test/
spring.cloud.config.server.git.searchPaths=lis
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
若是Git仓库为公开仓库,能够不填写用户名和密码,若是是私有仓库须要填写,本例子是公开仓库,放心使用。远程仓库 https://github.com/lis-ylfy/config-test/ 中有个文件config-client-dev.properties文件内容以下:
id = config-test
name = study-cloud
4.二、重启config server 和config client工程,打开网址访问:http://localhost:8881/hi,网页显示:
config-test