spring cloud

This commit is contained in:
罗祥
2019-08-26 10:38:53 +08:00
parent da5b55e19f
commit 39d8ca6880
8 changed files with 320 additions and 94 deletions

View File

@ -1,36 +1,51 @@
# spring-cloud-config
# Spring-Cloud-Config
## 目录<br/>
<a href="#一config-简介">一、config 简介</a><br/>
<nav>
<a href="#一Config-简介">一、Config 简介</a><br/>
<a href="#二项目结构">二、项目结构</a><br/>
<a href="#三config-server-配置中心的实现">三、config-server 配置中心的实现</a><br/>
<a href="#四config-client-搭建">四、config-client 搭建</a><br/>
<a href="#五集成-spring-cloud-bus-实现配置热更新">五、集成 spring-cloud-bus 实现配置热更新</a><br/>
## 正文<br/>
<a href="#三配置中心">三、配置中心</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#31-导入依赖">3.1 导入依赖 </a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#32-添加注解">3.2 添加注解</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#33--项目配置">3.3 项目配置</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#34---启动服务">3.4 启动服务</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#35-请求地址映射">3.5 请求地址映射</a><br/>
<a href="#四Config-客户端">四、Config 客户端</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#41-导入依赖">4.1 导入依赖</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#42-bootstrapyml">4.2 bootstrap.yml</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#43-配置映射类">4.3 配置映射类</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#44-启动项目">4.4 启动项目</a><br/>
<a href="#五实现配置热更新">五、实现配置热更新</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#51-消息总线">5.1 消息总线</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#52-导入依赖">5.2 导入依赖</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#53-修改-bootstrapyml">5.3 修改 bootstrap.yml </a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#54-RefreshScope">5.4 @RefreshScope</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#55-启动项目">5.5 启动项目</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#56-测试热刷新">5.6 测试热刷新</a><br/>
</nav>
## 一、config 简介
## 一、Config 简介
spring cloud config 分为服务端和客户端,服务端称为分布式配置中心,集中管理配置文件客户端为各个业务单元,它们从配置中心获取相关配置同时 config 还实现了配置热更新,在服务不停机的情况下刷新配置。
Spring Cloud Config 分为服务端和客户端,服务端称为分布式配置中心,集中管理配置文件客户端为各个业务单元,它们从配置中心获取相关配置同时 Config 还支持配置热更新,可以在服务不停机的情况下刷新配置。
## 二、项目结构
+ config-server: 配置中心;
+ config-client: 服务单元,可以从配置中心获取相关配置;
+ eureka: 注册中心。
+ **config-server**配置中心;
+ **config-client**服务单元,可以从配置中心获取相关配置;
+ **eureka**注册中心。
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-cloud-config.png"/> </div>
## 三、配置中心
## 三、config-server 配置中心的实现
Config 配置中心的具体实现如下:
#### 3.1 导入依赖
### 3.1 导入依赖
```xml
<?xml version="1.0" encoding="UTF-8"?>
@ -75,7 +90,9 @@ spring cloud config 分为服务端和客户端,服务端称为分布式配置
#### 3.2 在启动类上添加@EnableDiscoveryClient和@EnableConfigServer 注解
### 3.2 添加注解
在启动类上添加 @EnableDiscoveryClient@EnableConfigServer 注解:
```java
@SpringBootApplication
@ -92,7 +109,9 @@ public class ConfigServerApplication {
#### 3.3 指定注册中心地址,并配置git仓库地址的配置文件路径
### 3.3 项目配置
Spring Cloud Config 支持从 Git 仓库中获取统一的配置文件,具体配置如下:
```yaml
server:
@ -123,32 +142,35 @@ spring:
label: master
```
这里的 git 仓库就是本用例的仓库,是公开的仓库,所以不用配置用户名和密码,配置文件如下
这里的 Git 仓库就是本用例的仓库,是公开的仓库,所以不用配置用户名和密码,配置文件如下
- application.yml 为主配置;
- application-dev.yml 为开发环境配置。
- **application.yml** 为主配置;
- **application-dev.yml**为开发环境配置。
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/config-git.png"/> </div>
### 3.4 启动服务
#### 3.4 启动eureka和config-server服务访问 http://localhost:8020/application-dev.yml
启动 Eureka 和 Config-Server 服务,访问 http://localhost:8020/application-dev.yml , 此时界面如下:
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/config-application-dev.png"/> </div>
这里需要注意的拉取配置的时候,我们此时指定拉取的是 dev 配置application.yml 实际 配置如下:
注意此时我们访问是 dev 分支,即开发环境配置,其实际的配置文件的内容如下:
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/config-dev.png"/> </div>
这说明在用配置中心拉取配置的时候,和我们在本地开发的时候是一致的,配置是互补的,即 dev 中的实际配置应该是主配置和 dev 配置的结合,且遵循同名属性精确优先的原则。
可以看到访问页面的内容远多于实际内容,这说明配置中心自动进行了配置的合并,即 dev 中的实际配置应该是主配置和 dev 配置的结合,且遵循同名属性精确优先的原则,这和我们本地开发时的情况是一致的。
#### 3.5 http请求地址和资源文件映射
### 3.5 请求地址映射
在本用例中如果我们想要直接访问主配置,用以下路径 http://localhost:8020/application.yml 是不行的,会得到错误页面。如果想要访问主配置,,可以用 http://localhost:8020/application-X.yml其中可以是任意字符原因是
请求地址和实际的配置文件应该遵循以下规则application 为配置文件名profile 为环境label 为分支(如果不指定默认就是 master 分支)。
在本用例中如果我们想要直接访问主配置,用 http://localhost:8020/application.yml 地址是不行的,会得到错误页面。如果想要访问主配置,,可以用 http://localhost:8020/application-X.yml ,其中 X 可以是任意字符,原因是请求地址和实际的配置文件应该遵循以下的映射规则
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
@ -156,15 +178,15 @@ spring:
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
访问主配置
其中 application 为配置文件名profile 为环境label 为分支(如果不指定默认就是 master 分支)。从上面的规则中我们可以看出并不存在单独的 `/{application}` 访问路径,所以必须接上一个任意字符,示例如下
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/config-a.png"/> </div>
## 四、config-client 搭建
## 四、Config 客户端
#### 4.1 导入依赖
### 4.1 导入依赖
```xml
<?xml version="1.0" encoding="UTF-8"?>
@ -210,15 +232,14 @@ spring:
```
#### 4.2 新建 `bootstrap.yml`配置文件,指定注册中心地址和配置中心服务名,并在启动类上开启自动注册@EnableDiscoveryClient
### 4.2 bootstrap.yml
这里需要特别说明的是,在之前的所有项目中我们采用的配置文件都是 application.yml,但是这里**一定要采用 bootstrap.yml**
新建 `bootstrap.yml` 配置文件,指定注册中心地址和配置中心服务名,并在启动类上开启自动注册 @EnableDiscoveryClient这里需要特别说明的是,在之前的所有项目中我们采用的配置文件都是 application.yml但是这里一定要采用 **bootstrap.yml**。两者的区别如下:
假设我们的数据库配置是放在远程配置中心的,那么我们应该先去远程配置中心拉取配置,然后再去进行数据库的自动化配置,反之如果我们先进行了数据库的自动化配置,那么就会因为找不到 url 或驱动而抛出异常
- **bootstrap.yml** 或 bootstrap.properties用来程序引导时执行应用于更加早期配置信息读取bootstrap.yml 先于 application.yml 加载
- **application.yml**(或 application.properties) 应用程序各个模块的配置信息。
- bootstrap.ymlbootstrap.properties用来程序引导时执行应用于更加早期配置信息读取bootstrap.yml 先于 application.yml 加载
- application.ymlapplication.properties) 应用程序各个模块的配置信息。
使用 bootstrap.yml 后 ,如果我们的数据库配置放在远程配置中心,那么程序会先去远程配置中心拉取配置,然后再进行数据库的自动化配置,反之如果不使用 bootstrap.yml就会先进行数据库的自动化配置此时会因为找不到 url 或驱动而抛出异常
```yaml
server:
@ -260,7 +281,9 @@ public class ConfigClientApplication {
```
#### 4.3 创建配置映射类用于测试
### 4.3 配置映射类
创建配置映射类用于测试:
```java
@Component
@ -296,13 +319,14 @@ public class ConfigController {
}
```
#### 4.4 依次启动eureka,config-server,config-client ,访问 http://localhost:8030/programmer
### 4.4 启动项目
这里需要注意是在启动 eureka 和 config-server要稍等一会启动 config-client这里是为了确保 config-server 已经将服务注册到 eureka然后我们的 config-client 才能从 eureka 中获取配置中心的服务
依次启动 eurekaconfig-serverconfig-client 三个项目,访问 http://localhost:8030/programmer 。在启动 eureka 和 config-server,要稍等一会启动 config-client这里是为了确保 config-server 已经将服务注册到 Eureka然后我们的 config-client 才能从 Eureka 中获取配置中心的服务
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/config-client-programmer.png"/> </div>
启动的时候可以从控制台看到如下拉取服务的信息:
启动时可以从控制台上看到拉取配置的相关信息:
```shell
Fetching config from server at : http://localhost:8020/
@ -311,31 +335,29 @@ Located environment: name=config-client, profiles=[dev], label=master, version=5
## 五、集成 spring-cloud-bus 实现配置热更新
## 五、实现配置热更新
#### 5.1 消息总线简介
### 5.1 消息总线
在微服务的架构中,我们通常想要构建一个共同的消息主题被所有微服务实例所监听,以便对所有微服务实例管理和通知,这就是消息总线,spring cloud bus 就是消息总线的一种实现。
在微服务的架构中,通常会构建一个可以被所有微服务实例所监听的消息主题,以便对所有微服务实例进行管理和通知,这就是消息总线,Spring Cloud Bus 就是消息总线的一种实现,我们可以使用它来实现配置的热更新。目前 Spring Cloud Bus 支持的消息中间件有 RabbitMQ 和 Kafka, 我们下面的整合采用的是 RabbitMQ。另外关于热更新只需要对配置客户端config-client做更改不需要对 config-server 做改动
目前 spring cloud bus 支持的消息中间件有 RabbitMQ 和 kafka, 我们下面的整合采用的是 RrabbitMQ。
关于热更新只需要对配置客户端config-client做更改不需要对config-server做改动。
#### 5.1 导入bus依赖
### 5.2 导入依赖
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--因为要用到端点功能(主要是刷新端点),所以需要导入actuator-->
<!--因为要用到端点功能(主要是刷新端点),所以需要导入actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```
#### 5.2 修改bootstrap.yml 配置开启总线配置配置rabbitmq 和 开启热刷新[端点](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-actuator)
### 5.3 修改 bootstrap.yml
修改 bootstrap.yml 配置,开启总线配置,主要是配置 RabbitMQ 和 开启热刷新 [端点](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-actuator)
```yml
server:
@ -347,7 +369,8 @@ spring:
config:
discovery:
enabled: true
# 这里我们指定的是服务名 如果配置中心有多个,且用同一个服务名,我们的客户端拉取配置的时候是负载均衡的,配置中心也就是高可用
# 这里我们指定的是服务名 如果配置中心有多个,且用同一个服务名,
# 那么我们的客户端拉取配置时会自动负载均衡,此时配置中心也就同时实现了高可用
serviceId: config-server
# 指定分支
label: master
@ -383,7 +406,9 @@ management:
```
#### 5.3 用@RefreshScope指定需要热刷新的配置
### 5.4 @RefreshScope
@RefreshScope 指定需要热刷新的配置:
```java
@Component
@ -406,30 +431,33 @@ public class Programmer{
}
```
#### 5.4 依次启动eurekaconfig-server, config-client 服务
### 5.5 启动项目
在 client 服务端启动时候,可以在控制台 看到 bus 自动创建了交换机、队列等
依次启动 eurekaconfig-serverconfig-client 四个项目,在 config 服务端启动时候,可以在控制台 看到 Bus 自动创建了交换机、队列等组件:
```
Created new connection: rabbitConnectionFactory#496c6d94:22/SimpleConnection@185d85d2 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 63713]
o.s.amqp.rabbit.core.RabbitAdmin : Auto-declaring a non-durable, auto-delete, or exclusive Queue (springCloudBus.anonymous.iY4TIIi9TSe0bL-TWAMhWg) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
```
也可以在 rabbitmq 管控台查看
也可以在 RabbitMQ 管控台查看
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-cloud-bus-exchange.png"/> </div>
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-cloud-bus-queue.png"/> </div>
#### 5.6 直接在 git 上修改配置文件,然后用 `post` 触发热刷新端点 http://localhost:8030/actuator/bus-refresh ,即可看到配置已经热刷新
### 5.6 测试热刷新
注意 这里的只能用 post 方式请求 ,可以用 postman 等测试软件
直接在 Git 上修改配置文件,然后用 `post` 请求触发热刷新端点 http://localhost:8030/actuator/bus-refresh ,即可看到配置已经热刷新。注意这里的只能用 post 方式请求 可以用 Postman 等测试软件来发送:
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/bus-refresh.png"/> </div>
热刷新的过程在控制台有详细的打印,部分日志如下:
热刷新的过程在控制台上有详细的输出,部分日志如下:
```shell
# 消息传播