栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud实现配置集中管理和在线更新-集中配置组件SpringCloudConfig+SpringCloudBus消息总线

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud实现配置集中管理和在线更新-集中配置组件SpringCloudConfig+SpringCloudBus消息总线

文章目录
    • 1. Spring Cloud Config简介
    • 2. 将配置文件上传到码云gitee
    • 3. 创建配置中心服务器
    • 4.配置中心客户端
    • 5. 测试集中配置
    • 6. SpringCloudBus简介
    • 7. 测试在线更新

1. Spring Cloud Config简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所 以需要分布式配置中心组件。在SpringCloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

在spring cloud config 组件中,分两个角色,一是config server,二是config client

Config Server(配置服务器) : 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储

Config Client是Config Server的客户端,用于操作存储在Config Server中的配置内容。 微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器

详细内容看在线文档 : https://www.springcloud.cc/spring-cloud-config.html

2. 将配置文件上传到码云gitee
  1. 创建仓库

  1. 上传文件,文件名称固定写法中间用 - 分割,我这里是 base-dev.yml

  1. 保存仓库地址备用

3. 创建配置中心服务器

单独创建一个微服务,作为配置中心的服务器

pom.xml

  
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
        
            org.springframework.cloud
            spring-cloud-bus
        
        
        
            org.springframework.cloud
            spring-cloud-stream-binder-rabbit
        
    

application.yml

server:
  port: 12000
spring:
  application:
    name: sunmone-config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/sanpangjwc/xxxxxx.git # gitee 仓库地址
  rabbitmq:
    host: 192.168.1.1 # rabbitmq 地址
management: # 固定语法,Bus发送消息通知更新的队列地址
  endpoints:
    web:
      exposure:
        include: bus-refresh #触发消息总线更新的队列地址 默认访问为:  http://127.0.0.1:端口/actuator/bus-refresh

Application 启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer // 开启Config服务
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class);
    }
}

启动服务浏览器输入: http://localhost:12000/文件名.yml,看到配置文件代表配置成功

4.配置中心客户端

客户端就是配置文件被上传到gitee的微服务

添加 pom.xml 依赖

		
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            org.springframework.cloud
            spring-cloud-bus
        
        
        
            org.springframework.cloud
            spring-cloud-stream-binder-rabbit
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

springboot优先识别 bootstrap.yml文件,如果没找到再去找application.yml文件

删除 application.yml 文件,新建 bootstrap.yml 文件


bootstrap.yml

spring: # 添加配置中心服务器地址,Spring就会自动加载配置文件
  cloud:
    config:
      name: base # 配置文件前缀名称 | base-dev.yml
      profile: dev #  配置文件后缀名称 | base-dev.yml
      label: master # gitee分支名称
      uri: http://localhost:12000 # 配置中心服务器的地址
5. 测试集中配置
  1. 启动配置服务器,启动配置客户端
  2. 浏览器访问客户端微服务接口,成功,可以看到配置文件放在gitee上也会被加载,只需要提供git地址即可
  3. 配置集中管理测试成功!

6. SpringCloudBus简介

如果我们更新码云中的配置文件,那客户端工程是否可以及时接受新的配置信息呢?我们现在来做一个测试,修改一下码云中的配置文件中mysql的地址,然后测试微服务接口,数据依然没有改变,证明修改服务器中的配置并没有及时更新到工程,只有重新启动程序才会读取配置。

那我们如果想在不重启微服务的情况下更新配置如何来实现呢? 我们使用SpringCloudBus来实现配置的自动更新

7. 测试在线更新
  1. 更改gitee上的配置文件内容

  1. 提交更新

  1. 使用Postman给配置中心服务器发送POST请求,通知它配置已更新

  1. 再次访问微服务接口,看数据库是否更改成功


5. 数据库更改成功,在线更新测试成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987912.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号