iframe的src动态修改并刷新_微服务中配置中心Config+消息总线Bus,实现分布式自动刷新配置

技术/杨33


一、分布式配置中心Config

一套集中的、动态的配置管理,实现统一配置微服务中的每个子服务。

Spring Cloud Config为微服务架构提供了集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供一个中心化的外部配置

Config分为服务端和客户端:

  • 服务端,也就是分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端获取配置信息。
  • 客户端,通过指定的配置中心来管理应用资源。

Spring Cloud默认采用GitHub来存储配置信息,这样有助于对环境配置进行版本管理。

二、Config服务端读取配置

1、在GitHub中新建一个文件夹,添加配置文件config-dev.yml

6ebd97cf488043f7a53fe734a8e0306b

GitHub仓库

config-dev.yml的内容为:

config:  info: "master,dev.yml"

2、新建module:cloud-config3344

  • pom.xml文件内容:
org.springframework.cloud            spring-cloud-config-server        org.springframework.boot            spring-boot-starter-web        org.springframework.boot            spring-boot-starter-actuator        org.springframework.boot            spring-boot-devtools            runtimetrue            cloud-common-util            com.project.cloud1.0-SNAPSHOTorg.springframework.cloud            spring-cloud-starter-netflix-eureka-client        
  • application.yml配置文件内容:
server:  port: 3344spring:  application:    name: cloud-config-center  cloud:    config:      server:        git:          uri: https://github.com/yangjian433/springcloud-config.git #git仓库的地址          search-paths:            - springcloud-config      label: master #读取分支eureka:  client:    register-with-eureka: true #true表示向注册中心注册自己    fetch-registry: true #是否从EurekaServer抓取已有的注册信息    service-url:      #defaultZone: http://localhost:7001/eureka/ #单机服务注册中心的地址      defaultZone: http://eureka7001.com:7001/eureka/ #集群服务
  • 主启动类
package com.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/** * @author 杨33 * @date 2020/5/2 12:40 */@SpringBootApplication@EnableEurekaClient@EnableConfigServerpublic class ConfigCenterMain3344 {    public static void main(String[] args) {        SpringApplication.run(ConfigCenterMain3344.class, args);    }}
  • 添加host文件映射
127.0.0.1 config-3344.com

3、启动主类:ConfigCenterMain3344

访问地址:http://config-3344.com:3344/master/config-dev.yml,读取GitHub上的配置文件信息。

d701975c774144d89cd9c30205c83fa7

三、Config客户端读取配置

上面服务端已经与GitHub直接连接,那么这块只要通过搭建的客户端访问服务端,就可以实现间接访问GitHub配置信息。

1、新建module:cloud-config-client3355

  • pom.xml文件内容:
org.springframework.cloud      spring-cloud-starter-config
  • bootstrap.yml配置文件内容:bootstrap.yml比application.yml的优先级别高,属于优先加载。
server:  port: 3355spring:  application:    name: cloud-config-client  cloud:    config:      label: master #master分支      name: config #配置文件名称      profile: dev #读取后缀名称,比如config-dev.yml、config-test.yml      uri: http://localhost:3344 #配置中心地址eureka:  client:    register-with-eureka: true #true表示向注册中心注册自己    fetch-registry: true #是否从EurekaServer抓取已有的注册信息    service-url:      #defaultZone: http://localhost:7001/eureka/ #单机服务注册中心的地址      defaultZone: http://eureka7001.com:7001/eureka/ #集群服务
  • 接口业务类
package com.cloud.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @author 杨33 * @date 2020/5/2 13:29 */@RestControllerpublic class ConfigClientController {    @Value("${config.info}")    private String configInfo;    @GetMapping("/getConfigInfo")    public String getConfigInfo() {        return configInfo;    }}
  • 主启动类
package com.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/** * @author 杨33 * @date 2020/5/2 13:27 */@SpringBootApplication@EnableEurekaClientpublic class ConfigClientMain3355 {    public static void main(String[] args) {        SpringApplication.run(ConfigClientMain3355.class, args);    }}

2、启动主启动类ConfigClientMain3355

访问接口地址:http://localhost:3355/getConfigInfo

b1f09b1a6ac1400bad9db5ea1c1714c8

四、手动刷新GitHub上配置文件的修改,避免了客户端多次重启服务

1、pom.xml文件引入actuator监控

org.springframework.boot       spring-boot-starter-actuator

2、bootstrap.yml添加暴露监控端点

#暴露监控端点management:  endpoints:    web:      exposure:        include: "*"

3、业务类ConfigClientController添加刷新能力注解@RefreshScope

@RestController@RefreshScopepublic class ConfigClientController {    @Value("${config.info}")    private String configInfo;    @GetMapping("/getConfigInfo")    public String getConfigInfo() {        return configInfo;    }}

4、命令行手动刷新端口。这最后一步执行完,才能实现在GitHub上修改配置文件,客户端cloud-config-client3355请求接口,修改立即生效,否则查出来的内容还是修改之前的。

curl -X POST "http://localhost:3355/actuator/refresh"
f66481a0670e423685f3a0403c822850
72990ea94ffb4f0c9645531a35d71c0b

查出来修改之后的文件内容

五、消息总线是什么

在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有的微服务实例都连接上来。由于该主题中产生的消息会被所有的实例监听和消费,所以称它为消息总线。

在总线上的各个实例,都可以便捷的广播一些需要消息,让该主题上连接的其他实例知道。

六、分布式自动刷新配置文件:消息总线Spring Cloud Bus

如果服务数量很多,手动刷新配置文件肯定是人力消耗非常大,不现实,那么自动刷新配置就解放了人力,提高了效率。

Spring Cloud Bus能管理和传播分布式系统间的消息,用于广播状态更改、事件推送等。

Bus支持两种消息代理:RabbitMQ和Kafka。本次以RabbitMQ为例开发测试:

1、在ConfigServer模块cloud-config3344上修改几处代码配置:

  • pom.xml文件,添加内容:
org.springframework.cloud    spring-cloud-starter-bus-amqp
  • application.yml文件,添加内容:rabbitmq节点和暴露端点配置
server:  port: 3344spring:  application:    name: cloud-config-center  cloud:    config:      server:        git:          uri: https://github.com/yangjian433/springcloud-config.git #git仓库的地址          search-paths:            - springcloud-config      label: master #读取分支  rabbitmq:    addresses: 192.168.109.130    username: guest    password: guest    port: 5672#暴露监控端点management:  endpoints:    web:      exposure:        include: "bus-refresh"eureka:  client:    register-with-eureka: true #true表示向注册中心注册自己    fetch-registry: true #是否从EurekaServer抓取已有的注册信息    service-url:      #defaultZone: http://localhost:7001/eureka/ #单机服务注册中心的地址      defaultZone: http://eureka7001.com:7001/eureka/ #集群服务

2、在ConfigClient模块cloud-config-client3355上修改几处代码配置:

  • pom.xml文件,添加内容:
org.springframework.cloud    spring-cloud-starter-bus-amqp
  • bootstrap.yml文件,添加内容:rabbitmq节点和暴露端点配置
server:  port: 3355spring:  application:    name: cloud-config-client  cloud:    config:      label: master #master分支      name: config #配置文件名称      profile: dev #读取后缀名称,比如config-dev.yml、config-test.yml      uri: http://localhost:3344 #配置中心地址  rabbitmq:    addresses: 192.168.109.130    username: guest    password: guest    port: 5672eureka:  client:    register-with-eureka: true #true表示向注册中心注册自己    fetch-registry: true #是否从EurekaServer抓取已有的注册信息    service-url:      #defaultZone: http://localhost:7001/eureka/ #单机服务注册中心的地址      defaultZone: http://eureka7001.com:7001/eureka/ #集群服务#暴露监控端点management:  endpoints:    web:      exposure:        include: "*"

3、启动ConfigServer和ConfigClient的主启动类

4、修改GitHub上的配置文件内容

config:  info: "master,dev.yml,+2"

此时在没有执行刷新端口命令,ConfigClient服务器还是查询到修改前的数据。

5、只需刷新ConfigServer这一台服务的端口,多个ConfigClient服务器都可以立即查询到GitHub上修改后的配置文件内容。

刷新ConfigServer这一台服务的端口的命令是:

curl -X POST "http://localhost:3344/actuator/bus-refresh"
193f4236a1b6401a8640b104581a600c
82e745f5d74f4872b64b16e30a763cb6

查询到修改后的数据

上面是使用RabbitMQ做消息代理,才实现一处刷新,到处可用的功能,它的原理就是:

当一个服务刷新数据的时候,它会把这个消息放入到MQ的topic中,这样其他监听同一个topic的服务器都能得到通知,然后去更新自身的配置。这个topic默认是springCloudBus。

39858f342dd74c8f8b8856998873532b

6、上面是通过刷新ConfigServer这一台服务的端口,实现全部的ConfigClient数据同步,那如何做,可以只同步某一台客户端ConfigClient服务的数据呢?

还是刷新ConfigServer这一台服务端口,但是后面需要加参数destination,指定需要更新的服务或实例,命令如下:

destination

比如只刷新cloud-config-client:3355服务:

curl -X POST "http://localhost:3344/actuator/bus-refresh/cloud-config-client:3355"

这个名称取自application.yml中的微服务名称:spring: application: name: cloud-config-client,再加上端口号。

执行命令后,就实现了只更新某一台服务的数据。

e1086c2504ac4af88cc04fd88a4c59de

作者:杨33,北京互联网公司在职Java开发,专注分享写作干货。欢迎关注我,期待你的点赞评论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值