SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端两个部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
github上面创建一个仓库
复制https路径 https://github.com/BushRo/microservicecloud-config.git
创建一个目录在当前路径下把github的仓库关联过来
创建一个yml配置文件并且用utf-8的形式保存,并提交上去
spring: profiles: active: - dev --- spring: profiles: dev #开发环境 application: name: microservicecloud-config-dev --- spring: profiles: test #测试环境 application: name: microservicecloud-config-test
创建微服务与git相关联 microservicecloud-config-3344
引入依赖
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.smxy.lq</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-3344</artifactId> <dependencies> <!-- springCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback --> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>4.10.0.201712302008-r</version> </dependency> <!-- 图形化监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熔断 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
yml配置
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: https://github.com/BushRo/microservicecloud-config.git #GitHub上面的git仓库名字
启动类添加@EnableConfigServer注解就可以访问到github上面的配置了
http://config-3344.com:3344/master/application-test.yml
http://config-3344.com:3344/master/application-dev.yml
创建Spring Cloud Config客户端,通过服务端从git获取配置文件信息
首先创建一个往git上面创建一个microservicecloud-config-client.yml配置文件
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicec loud-config-client eureka: client: serviceUrl: defaultZone: http://eureka7001:7001/eureka/ --- server: port: 8202 spring: profiles: test application: name: microservicec loud-config-client eureka: client: serviceUrl: defaultZone: http://eureka7001:7001/eureka/
创建一个microservicecloud-config-client-3355微服务
引入依赖
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.smxy.lq</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-client-3355</artifactId> <dependencies> <!-- SpringCloud Config客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
Spring Cloud会创建一个Bootstrap Context',作为Spring应用的Application Context的父上下文。初始化的时候,'Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的' Environment'.
'Bootstrap属性有高代先級,默以情况下,它们不会被本地配置覆盖。'Bootstrap context和Application Context有着不同的約定,
所以新増了一个bootstrap.yml文件,保证Bootstrap Context'和' Application Context配置的分离。
新增bootstrap.yml
spring: cloud: config: name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名 profile: test #本次访问的配置项 label: master uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
获取配置文件信息类
package com.smxy.lq.rest; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientRest { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping("/config") public String getConfig() { String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; System.out.println("******str: " + str); return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; } }
启动服务端和客户端微服务,由于客户端的bootstrap.yml配置的是test所有访问的是git上面的8202端口
我们做一个eureka服务+一个访问的微服务,将两个微服务的配置统由github获得实现统一配置分布式管理,完成多环境的变更。
eureka的git配置文件可以根据dev或者test切换不同的配置
spring: profiles: active: - dev --- server: port: 7001 spring: profiles: dev application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001 #eureka服务端的实例名称 prefer-ip-address: true #访问路径可以显示IP地址 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001:7001/eureka/ --- server: port: 7001 spring: profiles: test application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001 #eureka服务端的实例名称 prefer-ip-address: true #访问路径可以显示IP地址 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001:7001/eureka/
提供接口的微服务的git配置文件可以根据dev或者test切换不同的配置
spring: profiles: active: - dev --- server: port: 8001 spring: profiles: dev application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称 username: root password: 671354 dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.smxy.lq.entities # 所有Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://eureka7001:7001/eureka/ instance: instance-id: microservicecloud-dept8001 #服务名称 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: microservicecloud company.name: www.baidu.com --- server: port: 8001 spring: profiles: test application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB02 # 数据库名称 username: root password: 671354 dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.smxy.lq.entities # 所有Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://eureka7001:7001/eureka/ instance: instance-id: microservicecloud-dept8001 #服务名称 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: microservicecloud company.name: www.baidu.com
新建microservicecloud-config-eureka-client-7001微服务,添加依赖
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.smxy.lq</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-eureka-client-7001</artifactId> <dependencies> <!-- SpringCloudConfig配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
添加bootstrap.yml,指向github的配置文件
spring: cloud: config: name: microservicecloud-config-eureka-client #需要从github上读取的资源名称,注意没有yml后缀名 profile: dev #本次访问的配置项 label: master uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
其他的与之前的7001没什么区别
新建microservicecloud-config-dept-client-8001微服务添加依赖
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.smxy.lq</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-dept-client-8001</artifactId> <dependencies> <!-- SpringCloudConfig配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>com.smxy.lq</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
添加bootstrap.yml配置,name填写github的配置文件
spring: cloud: config: name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名 #profile配置是什么就取什么配置dev or test #profile: dev profile: test label: master uri: http://config-3344.com:3344 #SpringCloudConfig获取的服务地址
其它的配置与之前的8001一致
依次启动3344,7001,8001的微服务,更改bootstrap.yml中的profile的属性就可以实现动态切换配置了