目录
在resources 目录下创建并配置 bootstrap.yml 文件
分布式架构的配置问题
在分布式微服务架构中,由于服务数量很多 ,使得有很多配置文件,在更新配置文件时很麻烦。我们每个微服务自已带着一个 application.yml,上百个配置文件的管理起来就很麻烦,有没有一种模式,我们把所有的配置文件放在一个统一的地方管理,应用需要这些配置信息的时候,就统一从同一个地方去拿,我们改的时候就方便多了,而且同样的多个微服务使用同一个配置文件即可,所以一套集中式的、动态的配置管理功能是必不可少的,在Spring Cloud中,有分布式配置中心组件Spring Cloud Config来解决这个问题。
Spring Cloud Config概述
什么是Spring Cloud Config
Spring Cloud Config 为微服务架构中的微服务提供 集中式的外部配置支持, 配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
Spring Cloud Config 分为 服务端与客户端 两个部分:
- 服务端 config server:
也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信
息,加密、解密信息等访问接口。
配置服务器官方推荐采用 Git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可通过Git
客户端工具来方便的管理与访问配置信息。
- 客户端 config client:
通过指定的服务端来管理服务的资源,以及与业务相关的配置内容,并在启动的时候从服务端获取和加
载配置信息。
作用
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,根据不同环境部署,如 dev/test/prod
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置,服务会向配置中心统一拉取自已的配置信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并使用修改后的配置信息
- 将配置信息以REST接口的形式暴露
与 GitHub 整合配置信息
由于 Spring Cloud Config 官方推荐使用Git来管理配置文件(也可支持其他方式,如:SVN和本地文件),
而且使用https/http访问的形式。
Spring Cloud Config 服务端配置
在Github上创建仓库,并把配置文件传上去,这里不叙述创建文件的过程。
新建Config服务端模块(配置中心)
配置pom.xml文件
<?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>parent</artifactId>
<groupId>com.haogenmin.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config</artifactId>
<dependencies>
<!-- Spring Cloud Config配置中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
配置 application.yml 文件
server:
port: 5001
spring:
application:
name: microservice-config
cloud:
config:
server:
git: # 远程库的git地址
uri: https://github.com/haogenmin/springcloudDemoConfig.git
创建启动类
package com.haogenmin.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author :HaoGenmin
* @Title :ConfigApplication
* @date :Created in 2020/8/24 16:37
* @description:
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
配置的读取规则
/{application}-{profile}.yml
/读取的配置文件名-环境配置项 (默认为master分支)
如:http://localhost:5001/microservice-config-application-dev.yml
/{application}/{profile}[/{label}]
/读取的配置文件名/环境配置项/分支名
如:http://localhost:5001/microservice-config-application/dev/master
/{label}/{application}-{profile}.yml
/分支名/读取的配置文件名/环境配置项
如:http://localhost:5001/master/microservice-config-application-dev.yml
测试
到此服务端就配置完成了。
Spring Cloud Config 客户端配置
这里以注册中心为例子
把eureka配置文件推送到远端库
配置文件
<?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>parent</artifactId>
<groupId>com.haogenmin.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka_01</artifactId>
<dependencies>
<!-- Spring Cloud 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-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
在resources 目录下创建并配置 bootstrap.yml 文件
spring:
cloud:
config:
name: microservice-config-eureka-1 #github上的配置名称,注意没有yml后缀名
#profile: dev #本次访问的环境配置项
label: master #远程库的分支名
uri: http://localhost:5001 #Config配置中心地址,通过它获取microservice-config-eureka.yml配置信息
功能测试
把原来的application.yml删掉或者注释掉。
先开启配置中心服务,再开启注册中心。
用同样的方法,把其他的微服务配置起来,如果配置文件中使用了profile,就开启相应环境的配置项即可。再把对consumer的路由配置一下,具体的配置,见GitHub。
结果:
自此所有的配置文件全部放在了git上面
我们就可以集中修改配置文件了。
但是……
每次修改完配置文件,难道要再回去重启服务吗?如果这样,配置中心还有什么意义。
Spring Cloud Bus 消息总线组件
Spring Cloud Bus 被国内很多都翻译为消息总线。大家可以将它理解为管理和传播所有分布式项目中的消息即可,
其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用Bus的机制
可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一,我们用一张图来描述Bus在配置中心使用
的机制。
根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:
- 1、提交配置后发送post方式的/bus-refresh请求给Config客户端
- 2、Config客户端接收到请求从Server端更新配置并且发送消息给消息总线
- 3、消息总线接到消息并通知给其它客户端
- 4、其它客户端接收到通知,请求Server端获取最新配置
- 5、全部客户端均获取到最新的配置
Spring Cloud Bus实现在线刷新配置
在这之前,我们先安装一下相应的工具:
- Postman测试工具
- RabbitMQ
安装步骤可以去问度娘。
添加依赖
首先,在所有需要从配置心中拉去配置的微服务,添加依赖,之前添加过监控依赖的不必重复添加。
<!--Bus 与 rabbitMQ依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--监听器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改配置文件
添加rabbitmq消息队列和暴露触发消息总线的地址management:。例如:provider的配置文件
server:
port: 8002
spring:
application:
name: microservice-provider #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
url: jdbc:mysql://127.0.0.1:3306/springcloud_db02?serverTimezone=GMT%2B8 # 数据库名称
username: root
password: 123456
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 150 # 等待连接获取的最大超时时间
mybatis:
configuration:
map-underscore-to-camel-case: true #驼峰命名
eureka:
client:
registerWithEureka: true # 服务注册开关
fetchRegistry: true # 服务发现开关
serviceUrl: # 注册到哪一个Eureka Server服务注册中心,多个中间用逗号分隔
defaultZone: http://eureka6001.com:6001/eureka,http://eureka6002.com:6002/eureka
instance:
instanceId: ${spring.application.name}:${server.port} # 等价于microservice-provider:8002
prefer-ip-address: true #访问路径就会显示成IP地址
management:
endpoints:
web:
exposure:
include: [bus-refresh,hystrix.stream]
测试
我们把网关的配置文件修改一下。
并没有变化。
发送post请求。
此时,微服务重新拉取了配置文件。
已经改变了。
另外,在配置文件中修改端口,你会发现注册中心的端口改变了,但实际上,那个端口还是不可以访问,因为,微服务只是重新读取了配置文件,并没有重启,服务没有关闭就不会改变端口。
webhook
每次修改文件之后都要发送个请求才能刷新,有没有觉得很麻烦,能不能修改之后让它自己自动刷新?
github 提供了一种 webhook 的方式,当有代码变更的时候,会调用我们设置的地址,来实现我们想达到的目的。
1、进入 github 仓库配置页面,选择 Webhooks ,并点击 add webhook;
2、之后填上回调的地址,也就是上面提到的 actuator/refresh 这个地址,但是必须保证这个地址是可以被 github 访问到的。如果是内网就没办法了。这也仅仅是个演示,一般公司内的项目都会有自己的代码管理工具,例如自建的 gitlab,gitlab 也有 webhook 的功能,这样就可以调用到内网的地址了。
因为我这里是内网,就不在这里演示了,需要的朋友可以去尝试一下。
自此,配置中心的内容入门学习完毕,以后更深的内容,在工作中慢慢探索……