概述
分布式系统面临的配置问题
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.ymI,上百个配置文件的管理…/(ToT)/~~
是什么
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
能干嘛
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署,比如dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露
与GitHub整合配置
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式
Config 服务端
GitHub新建仓库 上传yml文件
用自己的GitHub账号在GitHub上新建一个名为microservicecloud-config的新Repository
由上一步获得SSH协议的git地址
本地硬盘目录上新建git仓库并clone(本地仓库路径 D:\GIT_GitHub)
application.yml
在本地D:\GIT_GitHub\microservicecloud-config里面新建一个application.yml,文件格式必须是utf-8
spring:
profiles:
active:
- dev
---
spring:
profiles: dev # 开发环境
application:
name: microservicecloud-config-demo-dev
---
spring:
profiles: test # 测试环境
application:
name: microservicecloud-config-demo-test
# 请保存为UTF-8格式
将YML文件推送到github上
$ pwd
/d/GIT_GitHub/microservicecloud-config
$ git add .
$ git commit -m "init file"
$ git push origin master
配置中心模块
新建Module模块microservicecloud-config-3344,它即为Cloud的配置中心模块
POM
<!--主要添加以下依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
YML
server:
port: 3344
spring:
application:
name: microservicecloud-config-3344
cloud:
config:
server:
git:
uri: git@github.com:用户名/microservicecloud-config.git # github上的地址
主启动类
主启动类Config_3344_StartSpringCloudApp添加注解@EnableConfigServer
hosts文件修改
C:\Windows\System32\drivers\etc下的hosts文件
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
http://config-3344.com:3344/application-xx.yml(不存在的配置)
配置的读取规则
The HTTP service has resources in the form:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
where the “application” is injected as the
spring.config.name
in the SpringApplication (i.e. what is normally
“application” in a regular Spring Boot app), “profile” is an active profile (or comma-separated list of properties), and
“label” is an optional git label (defaults to “master”.)
Spring Cloud Config Server pulls configuration for remote clients from a git repository (which must be provided)
/{application}/{profile}[/{label}] 形式例如:
http://config-3344.com:3344/application/dev/master
返回的是json格式的数据
/{application}-{profile}.yml 形式例如:
http://config-3344.com:3344/application-dev.yml
/{label}/{application}-{profile}.yml 形式例如:
http://config-3344.com:3344/master/application-dev.yml
Config 客户端
GitHub上传yml
在本地D:\GIT_GitHub\microservicecloud-config路径下新建文件microservicecloud-config-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/
将上一步提交到GitHub中
git add .
git commit -m "test config"
git push origin master
配置客户端模块
新建模块microservicecloud-config-client-3355
POM
<!--SpringCloud Config 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
bootstrap.yml
applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更高
Spring Cloud会创建一个
Bootstrap Context
,作为Spring应用的Application Context
的父上下文
。初始化的时候,
Bootstrap Context
负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
。
Bootstrap
属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context
和Application Context
有着不同的约定,所以新增了一个bootstrap.yml文件, 保证Bootstrap Context
和Application Context
配置的分离。
内容
spring:
cloud:
config:
name: microservicecloud-config-client # 需要从github上读取的资源名称,注意没有yml后缀名
profile: dev # 本次访问的配置项
label: master
uri: http://config-3344.com:3344 # 本微服务启动后 先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
application.yml
实际上bootstrap.yml配置好了之后不必再配置application.yml
spring:
application:
name: microservicecloud-config-client
hosts
C:\Windows\System32\drivers\etc下的hosts文件
127.0.0.1 client-config.com
ConfigClientRest.java
新建rest类ConfigClientRest.java,验证是否能从GitHub上读取配置
package com.demo.springCloud.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;
}
}
主启动类
有个基本的@SpringBootApplication
注解就可以
测试
启动Config配置中心3344微服务并自测 http://config-3344.com:3344/application-dev.yml
启动3355作为Client准备访问 http://client-config.com:8201/config http://client-config.com:8202/config
bootstrap.yml里面的profile值是什么,决定从github上读取什么
成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息
SpringCloud Config 配置实战
Config服务端配置OK且测试通过,我们可以和config+GitHub进行配置修改并获得内容。
此时我们做一个 eureka服务 + 一个Dept访问的微服务,将两个微服务的配置统一由github获得实现统一配置分布式管理,完成多环境的变更。
Git配置文件本地配置
GitHub上传yml
在本地D:\GIT_GitHub\microservicecloud-config路径下新建文件microservicecloud-config-eureka-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 7001
spring:
profiles: dev
application:
name: microservicecloud-config-eureka-client
eureka:
instance:
hostname: euraka7001.com
client:
register-with-eureka: false #false表示不向注册中心注册自己。只有客户端才向Eureka进行注册
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
server:
port: 7001
spring:
profiles: test
application:
name: microservicecloud-config-eureka-client
eureka:
instance:
hostname: euraka7001.com
client:
register-with-eureka: false #false表示不向注册中心注册自己。只有客户端才向Eureka进行注册
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
在本地D:\GIT_GitHub\microservicecloud-config路径下新建文件microservicecloud-config-dept-client.yml
# 可类比复制dept-8001模块的配置文件,新增或修改如下部分
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
datasource:
url: 连接cloudDB01数据库
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
spring:
profiles: test
datasource:
url: 连接cloudDB02数据库
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
将上一步提交到GitHub中
git add .
git commit -m "two new file"
git push origin master
Config版的eureka服务端
新建工程microservicecloud-config-eureka-client-7001
POM
<!--SpringCloud Config 配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka-server服务端-->
<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>
bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-eureka-client # 需要从github上读取的资源名称,注意没有yml后缀名
profile: dev # 本次访问的配置项
label: master
uri: http://config-3344.com:3344 # 本微服务启动后 先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
application.yml
spring:
application:
name: microservicecloud-config-eureka-client
主启动类
Config_Git_EurekaServerApplication
@SpringBootApplication
@EnableEurekaServer //EurekaServer服务器端启动类,接受其它微服务注册进来
测试
先启动microservicecloud-config-3344微服务,保证Config总配置是OK的
再启动microservicecloud-config-eureka-client-7001微服务
http://eureka7001.com:7001/
Config版的dept微服务
参考之前的8001拷贝后新建工程【microservicecloud-config-dept-client-8001】
POM
<!--SpringCloudConfig配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-eureka-client # 需要从github上读取的资源名称,注意没有yml后缀名
profile: test # 本次访问的配置项
label: master
uri: http://config-3344.com:3344 # 本微服务启动后 先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
application.yml
spring:
application:
name: microservicecloud-config-dept-client
主启动类DeptProvider8001_App
@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进Eureka服务中
@EnableDiscoveryClient //服务发现
测试
启动顺序:config-3344 -> eureka-client-7001 -> config-dept-client-8001
test配置默认访问 http://localhost:8001/dept/list 可以看到数据库配置是02
换配置成dev http://localhost:8001/dept/list 可以看到数据库配置是01