前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud详细教程 | 第八篇:消息总线(Spring Cloud Bus)(Greenwich版本)

SpringCloud详细教程 | 第八篇:消息总线(Spring Cloud Bus)(Greenwich版本)

作者头像
小东啊
发布2019-06-26 15:22:06
1.7K3
发布2019-06-26 15:22:06
举报
文章被收录于专栏:李浩东的博客李浩东的博客

Spring Cloud Bus使用轻量级消息代理链接分布式系统的节点。然后,这可以用于广播状态改变(例如,配置改变)或其他管理指令。目前唯一的实现是使用AMQP代理作为传输,但是相同的基本功能集(以及一些更多取决于传输)是其他传输的路线图。

一. 简介

1.概述

Spring Cloud Bus使用轻量级消息代理链接分布式系统的节点。然后,此代理可用于广播状态更改(例如配置更改)或其他管理指令。一个关键的想法是总线就像一个分布式执行器,用于扩展的Spring Boot应用程序。但是,它也可以用作应用程序之间的通信通道。该项目为AMQP经纪人或Kafka提供启动器作为运输工具。

2.作用

SpringCloud详细教程 | 第七篇:分布式配置中心(Spring Cloud Config)(Greenwich版本)一文中介绍了 Spring Cloud Config 的使用,已经实现了配置文件的统一管理,但是,每次修改配置文件后,还需要重新启动应用才能加载到修改后的配置文件,这还没有达到我们的目的,我们最终想要的是,修改完配置文件后,不需要重启我们的应用,就可以重新加载到修改后的配置文件,其实 Spring Cloud 已经为我们提供了这样的支持,那就是 Spring Cloud Bus 组件。

3.原理

Spring Cloud Bus的工作原理是,如果它在类路径上检测到自身,则添加Spring Boot自动配置。要启用总线,请添加spring-cloud-starter-bus-amqp或 spring-cloud-starter-bus-kafka依赖于您的依赖关系管理。Spring Cloud负责其余部分。确保代理(RabbitMQ或Kafka)可用并配置

消息队列的发布订阅模型。让所有为服务来订阅这个事件,当这个事件发生改变了,就可以通知所有微服务去更新它们的内存中的配置信息。这时Bus消息总线就能解决,你只需要在springcloud Config Server端发出refresh,就可以触发所有微服务更新了。如下架构图所示:

本文将讲述RabbitMQ作为工具

二. 快速开始

1.准备工作

按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp 我们需要装rabbitMQ, 参考我的这篇文章

2.搭建消息总线

改造上篇文章的config-client服务 引入依赖

代码语言:javascript
复制
<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>

bootstrap.properties 配置

代码语言:javascript
复制
# rabbitmq配置spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guestspring.rabbitmq.virtual-host= /#消息总线配置spring.cloud.bus.enabled=truespring.cloud.bus.trace.enabled=true#暴露bus-refreshmanagement.endpoints.web.exposure.include=bus-refresh

启动类加入注解@RefreshScope 启动刷新功能

代码语言:javascript
复制
package com.li.configclient;
import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;
@RefreshScope@RestController@EnableEurekaClient@SpringBootApplicationpublic class ConfigClientApplication {
    public static void main(String[] args) {        SpringApplication.run(ConfigClientApplication.class, args);    }
    @Value("${my.name}")    String name;
    @RequestMapping("/hello")    public String hello(){        return name;    }}

这样就改动完毕 下面进行测试

依次启动eureka-server, config-server, config-client

打开浏览器访问 http://localhost:8761/ 查看服务注册中心

config-client 服务控制台打印以下信息 说明启动成功

也可以查看 RabbitMQ有没有数据 浏览器访问 http://localhost:15672

发现已经有队列显示 说明是ok的

浏览器访问 http://localhost:8770/hello 获取以下响应

修改github配置信息 修改为lhd 666

现在使用postman工具 post访问 http://localhost:8770/actuator/bus-refresh进行刷新

控制台会更新一些信息

再次访问 http://localhost:8770/hello

搞定 配置更新完毕 不需要重启服务

目前我测试 数据库和Git可以更新 本地不更新

源码下载: https://github.com/LiHaodong888/SpringCloudLearn

未经允许不得转载

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 李浩东的博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. 简介
    • 1.概述
      • 2.作用
        • 3.原理
        • 二. 快速开始
          • 1.准备工作
            • 2.搭建消息总线
            相关产品与服务
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档