SpringCloudConfig配置中心
概述
就前面项目而言,分布面临的问题是配置问题,每一个项目都有一个yml文件,不好运维管理,所有需要一套集中式,动态的配置管理设施,SpringCloud提供了ConfigServer来解决这个问题。
SpringCloud Config是为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为 各个不同的微服务应用的环境提供了一个 中心化的外部配置。SpringCloud Config分为客户端和服务端,服务端也称 分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密和解密信息等访问接口,客户端是通过指定的配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具管理和访问配置内容。
作用
- 集中管理配置文件
- 不同环境下不同配置,动态化的配置更新,分环境部署等
- 运行期间动态调整配置,不需要在每一个服务部署的机器编码上编写文件,服务会向配置中心拉取自己的配置信息
- 当配置发生变动时,服务不需要重启即可感知配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露
config服务端与GitHub通信
GitHUb上新建一个microservicecloud-config的Repository
本地硬盘目录新建git仓库并clone
在D:\workspace2018\micorservicecloude-config\microservicecloud-config新建application.yml文件
1Spring: 2 profiles: 3 active: 4 - dev 5--- 6Spring: 7 profiles: dev 8 application: 9 name: micorservicecloud-config-luo-dev 10--- 11Spring: 12 profiles: test 13 application: 14 name: micorservicecloud-config-luo-test
注意保存为utf-8的文件格式
将yml文件推送到GitHub上
1git add . 2git commit -m"" 3git push origin master
新建项目microservicecloud-config-3344
POM.xml文件
1<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"> 2 <modelVersion>4.0.0</modelVersion> 3 <parent> 4 <groupId>com.luo.springcloud</groupId> 5 <artifactId>microservicecloud</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 </parent> 8 <artifactId>microservicecloud-config-3344</artifactId> 9<dependencies> 10 <!-- springCloud Config --> 11 <dependency> 12 <groupId>org.springframework.cloud</groupId> 13 <artifactId>spring-cloud-config-server</artifactId> 14 </dependency> 15 <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback --> 16 <dependency> 17 <groupId>org.eclipse.jgit</groupId> 18 <artifactId>org.eclipse.jgit</artifactId> 19 <version>4.10.0.201712302008-r</version> 20 </dependency> 21 <!-- 图形化监控 --> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-actuator</artifactId> 25 </dependency> 26 <!-- 熔断 --> 27 <dependency> 28 <groupId>org.springframework.cloud</groupId> 29 <artifactId>spring-cloud-starter-hystrix</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.cloud</groupId> 33 <artifactId>spring-cloud-starter-eureka</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework.cloud</groupId> 37 <artifactId>spring-cloud-starter-config</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-jetty</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-web</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 </dependency> 51 <!-- 热部署插件 --> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>springloaded</artifactId> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-devtools</artifactId> 59 </dependency> 60 </dependencies> 61 62</project>
application.yml文件
1server: 2 port: 3344 3 4spring: 5 application: 6 name: microservicecloud-config 7 cloud: 8 config: 9 server: 10 git: 11 uri: git@github.com:luokangyuan/microservicecloud-config.git #GitHub上面的git仓库名字
主启动类
1@SpringBootApplication 2@EnableConfigServer 3public class Config_3344_StartSpringCloudApp { 4 public static void main(String[] args) { 5 SpringApplication.run(Config_3344_StartSpringCloudApp.class, args); 6 } 7}
修改host文件
127.0.0.1 config-3344.com
测试通过config微服务从GitHub上获取配置内容
启动服务3344,访问http://config-3344.com:3344/application-dev.yml,http://config-3344.com:3344/application-test.yml
config客户端获取github配置
本地新建microservicecloud-config-client.yml文件,并推送到github
1server: 2 port: 8201 3spring: 4 profiles: dev 5 application: 6 name: microservicecloud-config-client 7eureka: 8 client: 9 service-url: 10 defaultZone: http://eureka-dev.com:7001/eureka/ 11--- 12server: 13 port: 8202 14spring: 15 profiles: test 16 application: 17 name: microservicecloud-config-client 18eureka: 19 client: 20 service-url: 21 defaultZone: http://eureka-test.com:7001/eureka/
新建项目microservicecloud-config-client-3355,pom.xml文件如下
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <parent> 5 <groupId>com.luo.springcloud</groupId> 6 <artifactId>microservicecloud</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 </parent> 9 <artifactId>microservicecloud-config-client-3355</artifactId> 10 11 <dependencies> 12 <!-- SpringCloud Config客户端 --> 13 <dependency> 14 <groupId>org.springframework.cloud</groupId> 15 <artifactId>spring-cloud-starter-config</artifactId> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-actuator</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.cloud</groupId> 23 <artifactId>spring-cloud-starter-hystrix</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-eureka</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-config</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-jetty</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-web</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-test</artifactId> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>springloaded</artifactId> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-devtools</artifactId> 52 </dependency> 53 </dependencies> 54</project>
新建bootstrap.yml文件
1spring: 2 cloud: 3 config: 4 name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名 5 profile: test #本次访问的配置项 6 label: master 7 uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
application.yml是用户级的资源配置文件,bootstrap.yml是系统级,优先级更高,保证不会被本地配置文件所覆盖
修改host文件,增加映射
127.0.0.1 client-config.com
新建测试controller,从github读取配置信息
1package com.luo.springcloud.rest; 2 3import org.springframework.beans.factory.annotation.Value; 4import org.springframework.web.bind.annotation.RequestMapping; 5import org.springframework.web.bind.annotation.RestController; 6 7@RestController 8public class ConfigClientRest 9{ 10 11 @Value("${spring.application.name}") 12 private String applicationName; 13 14 @Value("${eureka.client.service-url.defaultZone}") 15 private String eurekaServers; 16 17 @Value("${server.port}") 18 private String port; 19 20 @RequestMapping("/config") 21 public String getConfig() 22 { 23 String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; 24 System.out.println("******str: " + str); 25 return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; 26 } 27}
新建主启动类
1@SpringBootApplication 2public class ConfigClient_3355_StartSpringCloudApp { 3 public static void main(String[] args) { 4 SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args); 5 } 6}
测试
启动3344服务,启动3355服务,bootstrap.yml中的profile值是什么,决定从github上读取什么,ruguo
访问http://client-config.com:8201/config得到是github上的microservicecloud-config-client.yml文件中dev相关的配置信息
访问http://client-config.com:8202/config得到是github上的microservicecloud-config-client.yml文件中test相关的配置信息