Spring Cloud Config可以对微服务配置进行统一的外部管理,并且默认采用Git来管理配置信息。相对于传统的每个微服务对应一份自个儿的配置文件来说,通过Spring Cloud Config统一管理所有微服务配置具有如下优点:1.集中管理微服务配置,当微服务数量众多的时候,使用这种方式会更为方便;2.通过Git管理微服务配置,方便追踪配置修改记录;3.可以在应用运行期间修改配置,微服务能够自动更新配置。
Spring Cloud Config包含了服务端Server和客户端Client。服务端用于从Git仓库中加载配置,并且缓存到本地;客户端用于从服务端获取配置信息。所以为了演示Spring Cloud Config,我们先来搭建一个服务端。
一、创建git项目
我创建的地址: https://github.com/c-qi/spring_cloud_config.git
在master分支创建4个文件
chenqi-dev.yml 内容: message: 'dev properties (master v1.0)'
chenqi-pro.yml 内容: message: 'test properties (master v1.0)'
chenqi-test.yml 内容:略
chenqi.yml 内容: 略
然后在test分支创建类似的4个文件
二、创建cofig-service
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.19.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
- 启动类加上@EnableConfigServer注解
application.yml
spring: application: name: config-server cloud: config: server: git: uri: https://github.com/c-qi/spring_cloud_config.git #.git可加可不加 username: ... password: ... clone-on-start: true #默认情况下Config-Server在启动的时候并不会马上就去Git参考clone配置文件,只有当Config-Clinet从Config-Server获取相关配置信息的时候,其才会去进行clone操作。我们可以将clone-on-start属性设置为true,其Config-Server在启动的时候就进行clone操作 server: port: 12581
配置好后访问:http://localhost:12581/test/chenqi :
{"name":"test","profiles":["chenqi"],"label":null,"version":"0695c2f63cb4ab0105ed6c167b60bacb5fcc6ba5","state":null,"propertySources":[]}
访问:http://localhost:12581/test/chenqi-pre.yml :
message: default properties (test v1.0)
则config-service 搭建完成
三、创建一个新的项目Config-Client
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.19.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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>
bootstrap.yml(必须)
spring: application: name: chenqi cloud: config: profile: dev label: test uri: http://localhost:12581
配置好后,当项目启动,会从http://localhost:12581 config服务上的test分支的dev配置引入配置,然后该干嘛干嘛......
四、config service 集群
config 服务端配置,节点一,二配置其实一样,端口号不相同就行
pom增加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
- 在Config-Server的启动类中加入@EnableDiscoveryClient注解,用于将服务注册到Eureka服务注册中心上
application.yml增加配置
eureka: client: serviceUrl: # 指定Eureka服务端的地址,这里为上面定义的Eureka服务端地址 defaultZone: http://localhost:8080/eureka/,http://localhost:8081/eureka/
client端配置:和上面一样加依赖加注解,最后改配置
spring: application: name: chenqi cloud: config: profile: dev label: test uri: http://localhost:12581 discovery: enabled: true service-id: config-server eureka: client: serviceUrl: # 指定Eureka服务端的地址,这里为上面定义的Eureka服务端地址 defaultZone: http://localhost:8080/eureka/,http://localhost:8081/eureka/