1. spring cloud config 概述
1.1 官网介绍
1.2 统合说明
Spring Cloud Config 就是对微服务的配置文件进行统一管理的。其工作原理是,我们首 先需要将各个微服务公共的配置信息推送到 GitHub 远程版本库。然后我们再定义一个 Spring Cloud Config Server,其会连接上这个 GitHub 远程库。这样我们就可以定义 Config 版的 Eureka Server、提供者与消费者了,它们都将作为 Spring Cloud Config Client 出现,它们都会通过连 接 Spring Cloud Config Server 连接上 GitHub 上的远程库,以读取到指定配置文件中的内容。
1.3 原理
Config Server 可以组装的最终配置文件格式有三种:yml、properties、json。
1.4 能干什么?
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
将配置信息以REST接口的形式暴露
2 创建配置文件工程 msc-configserver-9999
2.1 SpringCloud config 服务端配置
1 用自己的gitee账号在gitee上新建一个名为msc-config-center的新Repository
2 由上一步获得SSH协议的git地址 git@gitee.com:coderTomato/msc-config-center.git
3 clone到本地目录上
2.2 创建工程
复制 msc-zuul-9000 工程,并重命名为 msc-configserver9999。
2.3 添加依赖
<!--spring cloud config server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.4 修改配置文件
server:
port: 9999
spring:
cloud:
config:
server:
git:
# 指定git远程库地址
uri: git@gitee.com:coderTomato/msc-config-center.git
# 指定获取到git远程库连接的超时时限 默认5秒
timeout: 5
# 指定要操作的git远程库分支,默认master
default-label: master
2.5 修改启动类
@EnableConfigServer //开启config服务器功能
@SpringBootApplication
public class MscConfigserver9999Application {
public static void main(String[] args) {
SpringApplication.run(MscConfigserver9999Application.class, args);
}
}
3 定义 Config 版的 Eurekaserver
3.1 创建工程
复制 msc-eurekaserver8761 工程,并重命名为 msc-config-eurekaserver
3.2 添加 config 客户端依赖
在原工程依赖的基础上添加 spring cloud config 的客户端依赖
<!--spring cloud config客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.3 定义 bootstrap.yml
- bootstrap.yml中配置的是应用启动时所必须的配置信息。
- application.yml中配置的是应用运行过程中所必须的配置信息
- bootstrap.yml优先于application.yml进行加载。
spring:
cloud:
config:
#指定configserver的地址
uri: http://localhost:9999
#指定要访问的远程库分支
label: master
#指定要从远程库读取的配置文件名称, 无需扩展名
name: application-eureka-config
#环境选择
profile: dev
启动msc-configserver9999\ msc-config-eurekaserver工程
访问配置文件里的 eureka注册地址 http://eureka7001.com:7001/
4 定义 Config 版的提供者 msc-config-provider
4.1 创建工程
复制 msc-provider-8081 工程,重命名为 msc-config-provider。
4.2 添加 config 客户端依赖
在原工程依赖的基础上添加 spring cloud config 的客户端依赖
<!--spring cloud config客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
4.3 定义 bootstrap.yml
spring:
cloud:
config:
#指定configserver的地址
uri: http://localhost:9999
#指定要访问的远程库分支
label: master
#指定要从远程库读取的配置文件名称, 无需扩展名
name: application-provider-config
#环境选择
profile: dev
5 定义 Config 版的消费者 msc-config-consumer
5.1 创建工程
复制 msc-consumer8080 工程,并重命名为 msc-config-consumer
5.2 添加 config 客户端依赖
在原工程依赖的基础上添加 spring cloud config 的客户端依赖。
<!--spring cloud config客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
5.3 定义 bootstrap.yml
spring:
cloud:
config:
#指定configserver的地址
uri: http://localhost:9999
#指定要访问的远程库分支
label: master
#指定要从远程库读取的配置文件名称, 无需扩展名
name: application-consumer-config
#环境选择
profile: dev
启动以下工程
- msc-configserver9999
- msc-config-eurekaserver工程
- msc-config-provider
- msc-config-consumer
访问http://localhost:7070/consumer/depart/get/2
6 把config组件部署到k8s
- 参考k8s部署eureka
- K8s部署服务提供者
- K8s部署服务消费者