SpringCloud-Config概述
- 做项目,那么肯定就少不了配置
- 微服务架构中,配置文件众多,各个服务的配置文件也有可能不一样
- Spring 为我们提供了相应的
配置中心
组件也就是SpringCloud-Config
- 他是一个配置管理中心,用于集中管理程序中各个环境下的配置,我们可以将配置通过
git
或svn
等方式推送到我们的共享仓库中进行管理 - 同 Eureka 一样,他也分为 server 端与 client 端,也就是服务端与客户端
优点
- 提供了服务端和客户端的支持
- 集中式的管理,分布式环境下的应用配置
- 基于 Spring 环境,无缝与 Spring 应用集成
- 可应用于任何语言开发的程序
- 默认实现基于
git仓库
,可以进行版本管理 - 可以替换,需要自定义实现
SpringCloud-ConfigServer服务端
- 拉取配置时更新
git仓库副本
,保证是最新结果 - 支持数据结构丰富,
yml
、json
、properties
等… - 配合
eureke
可实现服务发现,配合cloud bus
可实现配置推送更新 - 配置存储基于
git仓库
,可进行版本管理 - 简单可靠,有丰富的配套方案
SpringCloud-ConfigClient客户端
-
SpringBoot
项目不需要改动任何代码 - 加入一个启动配置文件指明使用
ConfigServer
上哪个配置文件即可
ConfigServer服务端搭建
工程搭建
- 创建工程
- 创建一个
ConfigServer
工程,创建好之后添加相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- 创建启动类,然后在启动类上面添加如下注解:
- 启动类最终代码如下:
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
}
- 在自己 GitHub 当中创建仓库添加配置文件,使用 Git 推送上去
- 创建仓库的方式我就不一一说了,都学到了这里不会的话你也可以去看我的
Git
文章里面有讲解 - 还有文件的推送,自行去我的 Blog 中的 Git 分类中查找
- 在
ConfigServer
工程当中添加核心配置文件application.yml
server
port8080
spring
application
name ConfigServer
cloud
config
server
git
# 仓库地址
uri https //github.com/XiaoHuiHuiT/springcloud-config.git
- 以上的步骤完毕之后
- 启动工程,在浏览器当中访问如下地址:
http://localhost:8080/test-config.yml
访问规则
- 仓库中文件的内容如下:
- 规则
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
- 解释一下上面的关键字代表的内容:
- application:配置文件的名字
- profile:对应的环境
- label:不同的分支
- 访问示例:http://localhost:8080/test-config-dev.yml
- 访问不同分支的我就不演示了因为我这里没有弄分支
- 配置文件放入到了目录当中了呢,那么该如下访问呢类似下方图中所示:
- 修改
ConfigServer
服务中的application.yml
配置文件修改内容如下:
...
# 查询 GitHub 仓库根目录,子目录,以及所有以 goods、foo、Config 开头的目录
search-paths foo, goods, Config
# 启动时,就 clone 仓库到本地,默认是在配置首次请求时,clone 到本地
clone-on-starttrue
...
ConfigClient客户端搭建
- 在需要使用配置中心,中的配置文件的客户端中添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 在要使用 配置中心中的文件的微服务当中添加一个
bootstrap.yml
的配置文件
- 添加如下配置:
spring
cloud
config
# 读取 GitHub 配置中心的文件名,对应获取规则的{application}
name eureka01-application
# 指定需要获取那个环境下的配置文件,对应获取规则的{profile}
# profile: dev
# 指定获取那个分支上的,对应获取规则的{label}
label master
# 配置中心的地址,也就是 ConfigServer的地址
uri http //localhost 8080/
删除原来 application.yml
的文件,把这个文件上传至配置文件中心
配置中心集群
- 紧接着上面的内容在创建一个
ConfigServier2
- 添加
Eureka
依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 在启动类上添加如下注解开启客户端配置:
- 添加
application.yml
配置文件 - 配置内容如下:
server
port8081
spring
application
name ConfigServer
cloud
config
server
git
# 仓库地址
uri https //github.com/XiaoHuiHuiT/springcloud-config.git
# 查询 GitHub 仓库根目录,子目录,以及所有以 goods、foo、Config 开头的目录
search-paths foo, goods, Config
# 启动时,就 clone 仓库到本地,默认是在配置首次请求时,clone 到本地
clone-on-starttrue
eureka
client
serviceUrl
# eureka服务端提供的注册地址 参考服务端配置的这个路径
defaultZone http //eureka 3000/eureka,http //eureka1 3001/eureka,http //eureka2 3002/eureka
instance
# 此实例注册到eureka服务端的唯一的实例ID
instance-id config-server-02
# 是否显示IP地址
prefer-ip-addresstrue
# eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30 秒 (与下面配置的单位都是秒)
leaseRenewalIntervalInSeconds10
# Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
leaseExpirationDurationInSeconds30
-
ConfigServer1
的application.yml
配置内容如下: - 记得添加
eureka
依赖
server
port8080
spring
application
name ConfigServer
cloud
config
server
git
# 仓库地址
uri https //github.com/XiaoHuiHuiT/springcloud-config.git
# 查询 GitHub 仓库根目录,子目录,以及所有以 goods、foo、Config 开头的目录
search-paths foo, goods, Config
# 启动时,就 clone 仓库到本地,默认是在配置首次请求时,clone 到本地
clone-on-starttrue
eureka
client
serviceUrl
# eureka服务端提供的注册地址 参考服务端配置的这个路径
defaultZone http //eureka 3000/eureka,http //eureka1 3001/eureka,http //eureka2 3002/eureka
instance
# 此实例注册到eureka服务端的唯一的实例ID
instance-id config-server-01
# 是否显示IP地址
prefer-ip-addresstrue
# eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30 秒 (与下面配置的单位都是秒)
leaseRenewalIntervalInSeconds10
# Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
leaseExpirationDurationInSeconds30
- 在客户端工程当中进行配置,也就是说某个模块需要将自己的配置文件放入配置中心了就需要进行如下配置,以后就直接改远程上的配置文件即可
- 添加配置中心的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
- 在 resources 当中创建配置文件
bootstrap.yml
spring
cloud
config
# 读取 GitHub 配置中心的文件名,对应获取规则的{application}
name user-application
# 指定需要获取那个环境下的配置文件,对应获取规则的{profile}
# profile: dev
# 指定获取那个分支上的,对应获取规则的{label}
label master
# 配置中心的地址,也就是 ConfigServer的地址
# uri: http://localhost:8080/
discovery
enabledtrue
# client0server的名称
service-id ConfigServer
eureka
client
serviceUrl
# eureka服务端提供的注册地址 参考服务端配置的这个路径
defaultZone http //eureka 3000/eureka,http //eureka1 3001/eureka,http //eureka2 3002/eureka