SpringCloud Config简介
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,
它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,
用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,
它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
Spring Cloud Config 实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于 Spring 构建的应用程序之外,
也可以在任何其他语言运行的应用程序中使用。由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,
所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。
当然它也提供了对其他存储方式的支持,比如:GIT仓库、SVN 仓库、本地化文件系统。
Config Server端主要和Git/SVN服务器
通俗点,就是统一管理配置,包括方便切换环境配置,以及修改配置无需动代码,省心省力;
如果用上SpringCloud Bus,能实现无需重启,自动感知配置变化以及应用新配置;
Config Server基本使用
根据前面SpringCloud架构图,首先第一步,要搞个 configServer来联通远程GIT仓库,来读取远程配置;
这里GIT仓库,我们一般选用GitHub https://github.com/,或者码云 https://gitee.com/
我们这里用GitHub演示
建个仓库 microservice-config 然后 Git下载本地;
将仓库克隆到本地
上传一个配置文件上到git仓库,application.yml 记住要utf-8编码,否则乱码,解析各种问题;
文件内容:
profile: hello
目前只要能读取到配置即可;
新建module:microservice-config-server-4001
添加pom依赖
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-config-server</artifactId> 4 </dependency>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-config-server-4001</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 <exclusions> 27 <exclusion> 28 <groupId>org.junit.vintage</groupId> 29 <artifactId>junit-vintage-engine</artifactId> 30 </exclusion> 31 </exclusions> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-config-server</artifactId> 36 </dependency> 37 </dependencies> 38 39 <build> 40 <plugins> 41 <plugin> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-maven-plugin</artifactId> 44 </plugin> 45 </plugins> 46 </build> 47 48 </project>
启动类ConfigServerApplication_4001:
1 package com.yuan.microserviceconfigserver4001; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.config.server.EnableConfigServer; 6 7 @SpringBootApplication 8 @EnableConfigServer 9 public class MicroserviceConfigServer4001Application { 10 11 public static void main(String[] args) { 12 SpringApplication.run(MicroserviceConfigServer4001Application.class, args); 13 } 14 15 }
这里要加下注解:@EnableConfigServer
这里我们搞下仓库的Http地址:
然后项目的application.yml配置下:
1 server: 2 port: 4001 3 4 spring: 5 application: 6 name: microservice-config 7 cloud: 8 config: 9 server: 10 git: 11 uri: https://github.com/Me-yuan/t226microservice-config.git
主要是要配置一个git请求地址:
本地Hosts加个本地域名映射:
启动
然后我们请求:http://configserver.yuan.com:4001/application-xxx.yml
返回结果了正确的文本结果;
Config Client基本使用
根据前面的config原理图,我们需要建立Client端调用server端,最终实现client端获取远程git配置信息;
为了后面演示方便,我们提交三个配置文件到远程git库;
application.yml:
1 --- 2 spring: 3 profiles: 4 active: dev 5 --- 6 spring: 7 profiles: dev 8 port: 111 9 --- 10 spring: 11 profiles: test 12 port: 222
crm-dev.yml
1 port: 2 777
crm-test.yml
1 port: 2 888
提交到github
然后我们新建一个module microservice-config-client-5001
加下依赖:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-config</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-tomcat</artifactId> 8 </dependency> 9 <dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-web</artifactId> 12 </dependency>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-config-client-5001</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 <exclusions> 27 <exclusion> 28 <groupId>org.junit.vintage</groupId> 29 <artifactId>junit-vintage-engine</artifactId> 30 </exclusion> 31 </exclusions> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-config</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-tomcat</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-web</artifactId> 45 </dependency> 46 </dependencies> 47 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 </plugin> 54 </plugins> 55 </build> 56 57 </project>
我们项目启动的时候,就要调用server config端,
获取配置信息,所以这里要新建一个bootstrap.yml配置文件,优先级最高:
1 spring: 2 application: 3 name: application-dev 4 cloud: 5 config: 6 name: crm 7 uri: http://configserver.yuan.com:4001 8 profile: test 9 label: master 10 fail-fast: true
application.yml:
1 server: 2 port: 5001 3 context-path: /
再搞一个 ConfigClientController 类 测试显示端口:
1 package com.yuan.microserviceconfigclient5001.controller; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 @RestController 8 public class ConfigClientController { 9 10 @Value("${port}") 11 private String port; 12 13 @Value("${server.port}") 14 private String serverPort; 15 16 @GetMapping("/getPort") 17 public String getPort() { 18 return "测试你访问的yml文件的端口是:【"+port+"】"+",serverPort:【"+serverPort+"】"; 19 } 20 21 }
最后 本地hosts我们加给配置:
C:WindowsSystem32driversetchosts
我们启动项目:然后页面访问:
http://client-config.yuan.com:5001/getPort
即可获取远程端口配置信息;
更改bootstrap.yml中的profile即可访问不同的yml文件;
访问: http://configserver.yuan.com:4001/application-dev.yml
Config整合Eureka
我们现在搞个实例来演示下,eureka整合config以及服务器提供者整合config,这样大伙可以举一反一,方便理解;
首先是eureka整合config
我们先搞个配置文件到git;
eureka_config.yml
1 spring: 2 profiles: 3 active: 4 - dev 5 --- 6 server: 7 port: 2004 8 context-path: / 9 spring: 10 profiles: dev 11 eureka: 12 instance: 13 hostname: localhost 14 client: 15 register-with-eureka: false 16 fetch-registry: false 17 service-url: 18 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 19 --- 20 server: 21 port: 2005 22 context-path: / 23 spring: 24 profiles: test 25 eureka: 26 instance: 27 hostname: localhost 28 client: 29 register-with-eureka: false 30 fetch-registry: false 31 service-url: 32 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
我们启动 microservice-config-server-4001
再启动 microservice-eureka-server-config
测试连接
http://eureka2001.yuan.com:2004/
说明成功读取远程Git配置,然后eureka启动OK;
然后我们就是把服务提供者和config整合,把服务提供者注册到eureka;
我们搞个配置provider_config.yml,push到远程GIT;
1 spring: 2 profiles: 3 active: dev 4 --- 5 server: 6 port: 1007 7 context-path: / 8 9 # 数据源配置 10 spring: 11 profiles: dev 12 application: 13 name: microservice-student 14 datasource: 15 type: com.alibaba.druid.pool.DruidDataSource 16 driver-class-name: com.mysql.jdbc.Driver 17 url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8 18 username: root 19 password: 12345 20 jpa: 21 hibernate: 22 ddl-auto: update 23 show-sql: true 24 25 eureka: 26 instance: 27 hostname: localhost 28 appname: microservice-student 29 instance-id: microservice-student:1007 30 prefer-ip-address: true 31 client: 32 service-url: 33 defaultZone: http://localhost:2004/eureka 34 35 info: 36 groupId: com.yuan.testSpringcloud 37 artifactId: microservice-student-provider-config-1007 38 version: 1.0-SNAPSHOT 39 userName: http://yuan.com 40 phone: 123456 41 --- 42 server: 43 port: 1008 44 context-path: / 45 46 # 数据源配置 47 spring: 48 profiles: test 49 application: 50 name: microservice-student 51 datasource: 52 type: com.alibaba.druid.pool.DruidDataSource 53 driver-class-name: com.mysql.jdbc.Driver 54 url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8 55 username: root 56 password: 12345 57 jpa: 58 hibernate: 59 ddl-auto: update 60 show-sql: true 61 62 eureka: 63 instance: 64 hostname: localhost 65 appname: microservice-student 66 instance-id: microservice-student:1008 67 prefer-ip-address: true 68 client: 69 service-url: 70 defaultZone: http://localhost:2004/eureka 71 72 info: 73 groupId: com.yuan.testSpringcloud 74 artifactId: microservice-student-provider-config-1008 75 version: 1.0-SNAPSHOT 76 userName: http://yuan.com 77 phone: 123456
新建module:microservice-student-provider-config
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-student-provider-config</artifactId> 11 12 <properties> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-web</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-test</artifactId> 24 <scope>test</scope> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-data-jpa</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>mysql</groupId> 32 <artifactId>mysql-connector-java</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-tomcat</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>com.alibaba</groupId> 40 <artifactId>druid-spring-boot-starter</artifactId> 41 </dependency> 42 <!-- 修改后立即生效,热部署 --> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>springloaded</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-devtools</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>com.yuan</groupId> 53 <artifactId>microservice-common</artifactId> 54 <version>1.0-SNAPSHOT</version> 55 <scope>compile</scope> 56 </dependency> 57 58 <!--添加注册中心Eureka相关配置--> 59 <dependency> 60 <groupId>org.springframework.cloud</groupId> 61 <artifactId>spring-cloud-starter-eureka</artifactId> 62 </dependency> 63 64 <!-- actuator监控引入 --> 65 <dependency> 66 <groupId>org.springframework.boot</groupId> 67 <artifactId>spring-boot-starter-actuator</artifactId> 68 </dependency> 69 <dependency> 70 <groupId>org.springframework.cloud</groupId> 71 <artifactId>spring-cloud-starter-config</artifactId> 72 </dependency> 73 </dependencies> 74 75 <build> 76 <plugins> 77 <plugin> 78 <groupId>org.springframework.boot</groupId> 79 <artifactId>spring-boot-maven-plugin</artifactId> 80 </plugin> 81 </plugins> 82 </build> 83 84 </project>
bootstrap.yml:
1 spring: 2 application: 3 name: microservice-student-provider-config 4 cloud: 5 config: 6 name: provider_config 7 uri: http://configserver.yuan.com:4001 # 配置configserver地址 8 profile: dev # 级别 9 label: master # 分支 git中 默认master
application.yml
1 spring: 2 application: 3 name: microservice-student-provider-config
其他类文件从 原先的服务提供者里直接复制一份即可,这里不贴了;
启动下这个项目;
说明成功注册到服务注册中心了;
Config配置搜索路径
前面我们所有的GIT远程端配置文件都是跟目录的,所有请求默认都是根目录,但是有时候,项目很多,
配置文件需要根据子目录来划分,这时候,就需要来配置搜索路径了;比如aaa项目的配置文件放aaa目录下,
bbb项目的配置文件放bbb目录下,不配置的话 是找不到的那些配置文件的,我们需要配置search-paths属性实现;
microservice-config-server-4001 configserver端 加个配置
1 server: 2 port: 4001 3 4 spring: 5 application: 6 name: microservice-config 7 cloud: 8 config: 9 server: 10 git: 11 uri: https://github.com/Me-yuan/t226microservice-config.git 12 search-paths: aaa,bbb,ccc
分别搞3个目录aaa,bbb,ccc 里面分别放3个配置文件 nns.yml,nns2.yml,nn3.yml;
配置内容大体差不多,随便写;
aaa/nns.yml
1 spring: 2 profiles: 3 active: dev 4 --- 5 spring: 6 profiles: dev 7 name: aaadev 8 --- 9 spring: 10 profiles: test 11 name: aaatest
我们启动:microservice-config-server-4001
浏览器:http://configserver.yuan.com:4001/nns-test.yml
浏览器:http://configserver.yuan.com:4001/nns2-test.yml
浏览器:http://configserver.yuan.com:4001/nns-3test.yml