最近看《深入理解Spring Cloud微服务构建》是一书之中,发现作者使用的springboot是1.x.版本之中,书中的代码示例在2.x的版本之中,有一点区别,在此读者总结一下,实现2.0之后,用spring cloud bus 自动刷新配置
首先说一下笔者用到的版本springBoot 2.1.4 ,springcloud 版本 Greenwich ,首先看下笔者的demo目录结构

eureka 为注册中心 目的是为了搭建一个高可用的配置中心config ,config-client和config-server 分别向eureka里面去注册服务,config-client通过向eureka 读取到config-server的配置文件,而config-server配置文件实际内容存入了gitee仓库。
注意一下 pom.xml依赖仅仅是贴出来了相关重要依赖, web依赖请自行加入
这里 总线采用是 rabbitMQ做为总线代理,需要首先开启rabbitMQ 如果不会安装和配置rabbitMQ
请拜访官网 https://www.rabbitmq.com
1 eureka - server
eureka-server 在pom.xml文件 只需要引入 netflix-eureka-server的相关依赖,springBoot 以上的2.x依赖和spring1.x 一点点区别,主要加入了netflix (一个公司,springcloud 很好的结合的该公司的旗下用的很多组件)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在该项目的resource下面新建application.yml文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
#关闭表示 eureka不向自己注册自己,也不注册到别的eureka里面,在eureka集群下面要设置true 默认是true
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在启动类上面加上 @EnableEurekaServer 注解 开启eurekaServer服务
2 config-server
该模块是配置中心服务端 ,同样在pom.xml引入相关依赖,版本号缺不缺省都可
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
同样resource下面创建application.yml文件,添加相关配置 ,这里默认把config-client的配置文件存入了gitee仓库里面,
search-paths 表示仓库下面存放的搜寻的文件夹,配置文件放入文件夹里面
server:
port: 8769
spring:
application:
name: config-server
cloud:
config:
#config-server相关配置
server:
git:
uri: https://gitee.com/NaNiZhenShiYiJiBang/cloud-config.git
search-paths: respo
username: xzjayx@126.com
password:
default-label: master
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
gitee仓库:

在启动类上面加上 @EnableConfigServer 注解 开启config服务 ,在加上 @EnableEurekaClient 开启eureka客户端服务
3 config-client
该模块是config客户端模块,它需要向config-server模块里面去读取相关配置
同理添加依赖
<!-- 注册到eureka server里面构建高可用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- 消息总线 spring cloud bus 可选的消息代理总线 RabbitMQ,AMQP,Kafka 可以刷新配置
如果有几十个微服务,而每 个服务又是多实例,当更改配置时,需要重新启动多个微服
务实例,会非常麻烦。 Spring Cloud Bus 个功能就是让这个过程变得简单,当远程 Git
库的配置更改后,只需要向某 个微服务实例发送 Post 请求,通过消息组件通知其他微
服务实例重新拉取配置文件。 当远程 Git 仓库的配置更改后,通过发送
“ /bus-refresh ” Post 请求给某 个微服务实例,通过消息组件,通知其他微服务实例,更新配置
文件。
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- 2.0需要加上这个监控中心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在该模块的resource里面新建 bootstrap.yml (bootstrap.yml比application)加载优先级更高
config-client启动之时,会读取resource配置文件,在配置之中 读取config discovery 发现地址 service-id 对应eureka里面的 服务注册名,则client是在eureka里面去读取config-sever注册的服务 ,而且该模块的name 是config-client ,profiles 是dev 则配置文件的名词是 config-client-dev.yml
spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-server
profiles:
active: dev
#通过消息总线更改配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#开启监控中心 springboot2.0 必要操作 * 表示监控所有信息
management:
endpoints:
web:
exposure:
include: "*"
同理在启动类加上如下注解 ,懒得打字了 ,见下图,下面这个请求接口仅仅为了测试demo使用,项目中千万别这么写

4 启动测试
先依次开启rabbitMQ eureka-server config-server config-client 最后启动config-client之后会发现虽然配置文件设置的8080端口,但是读取到的git 配置文件里面的地址,端口是 8762

见图 
此时,在浏览器上面访问 localhost:8762/foo

说明已经加载了git仓库下面的配置文件,现在来测试spring cloud Bus 实现不用重启来刷新修改一下git配置文件里面的foo 的值

这里书中由于版本 问题,在2.X并不适用 2.x所有的都是在监控里面
http://localhost:8762/actuator/bus-refresh post请求

会发现在cosole里面输入一些信息 此时再次刷新

好了,这就实现了 项目示例代码 https://github.com/xzjayx/spring-cloud-config
本文详细介绍了如何在SpringCloud 2.x版本中,使用SpringBoot 2.1.4和springcloud Greenwich版本搭建高可用配置中心。通过eureka作为服务注册中心,结合config-server和config-client实现微服务配置的动态刷新,利用rabbitMQ作为消息总线,无需重启即可更新配置。

4097

被折叠的 条评论
为什么被折叠?



