微服务篇-C 深入理解第一代微服务(SpringCloud)_VIII 深入理解Bus消息总线

原创作者:田超凡(程序员田宝宝)

版权所有,引用请注明原作者,严禁复制转载

Part 1 理论部分

1 配置文件如何刷新?

在SpringCloud中,配置文件的刷新有手动刷新和实时刷新两种方式。

手动刷新使用Actuator端点实现,

实时刷新使用SpringCloud Bus消息总线实现。

2 什么是SpringCloud Bus消息总线?

在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题连接到各个微服务实例,它广播的消息会被注册中心中所有的微服务实例监听和消费,这就是消息总线。

SpringCloud中也有对应的解决方案,SpringCloud Bus将分布式的节点用轻量的消息代理连接起来,可以很容易搭建消息总线,配合SpringCloud Config实现微服务应用配置信息的动态更新。

需要注意的是,消息总线并不属于消息中间件,消息总线只是为了能够整合消息中间件实现服务之间的消息通讯,类似于消息代理,从应用程序中传入消息并执行一些特别的操作,市面上开源的消息中间件有很多,比如ActiveMQ,RabbitMQ,Kafka,RocketMQ等等,目前SpringCloud Bus消息总线仅支持整合RabbitMQ和Kafka消息中间件。

Part 2 实践部分

Git环境搭建

使用码云环境搭建git服务器端

Git服务器上传配置文件

命名规范 服务名称-版本.yml 例如configclient_dev.yml

搭建Eureka 注册中心

搭建config-server

Maven依赖

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--spring-cloud 整合 ++config++-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-++config++-server</artifactId> <version>2.0.2.RELEASE</version> </dependency> <!-- SpringBoot整合++eureka++ 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-++netflix++ -++eureka++-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone\</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> |

application.yml

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ###服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka spring: application: ####注册中心应用名称 name: config-server cloud: config: server: git: ###git环境地址 uri: https://gitee.com/ittcf/config_003.git ####搜索目录 search-paths: - config ####读取分支 label: master ####端口号 server: port: 8888 |

启动config-server

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class AppConfigServer { public static void main(String[] args) { SpringApplication.run(AppConfigServer.class , args); } } |

读取配置文件http://127.0.0.1:8888/configclient-dev.yml

搭建config-client

Maven依赖

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus-parent</artifactId> <version>2.0.0.RC2</version> <type>++pom++ </type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-++config++-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> <!-- SpringBoot整合++eureka++ 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-++netflix++ -++eureka++-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> <!--核心jar包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-++amqp++</artifactId> </dependency> <!-- actuator监控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone\</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> |

application.yml

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| spring: application: ####注册中心应用名称 name: configclient cloud: config: ####读取后缀 profile: dev ####读取config-server注册地址 discovery: service-id: config-server enabled: true ##### eureka服务注册地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka server: port: 8882 |

启动config-client

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @RestController public class ConfigClientController { // 年龄 @Value("${userAge}") private String userAge; @RequestMapping("/userAge") public String userAge() { return userAge; } } |

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @SpringBootApplication @EnableEurekaClient public class AppConfigClient { public static void main(String[] args) { SpringApplication.run(AppConfigClient.class , args); } } |

使用消息总线

三个微服务项目中都加上该依赖信息

Maven依赖

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <!--核心jar包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-++amqp++</artifactId> </dependency> <!-- actuator监控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |

配置文件

|-----------------------------------------------------------------------|
| ###开启bus刷新 management: endpoints: web: exposure: include: bus-refresh |

刷新接口 http://127.0.0.1:8882/actuator/bus-refresh

本文部分素材转载自蚂蚁课堂

相关推荐
什么想法都无10 分钟前
stream
java·java stream
m0_7482336411 分钟前
WebService简介
java
love静思冥想11 分钟前
Stream `Collectors.toList()` 和 `Stream.toList()` 的区别(Java)
java·stream
Ch.yang30 分钟前
【Spring】 Bean 注入 HttpServletRequest 能保证线程安全的原理
java·spring·代理模式
web1508509664132 分钟前
基于Mysql、JavaScript、PHP、ajax开发的MBTI性格测试网站(前端+后端)
java
昙鱼39 分钟前
springboot创建web项目
java·前端·spring boot·后端·spring·maven
eternal__day40 分钟前
数据结构(哈希表(中)纯概念版)
java·数据结构·算法·哈希算法·推荐算法
天之涯上上44 分钟前
JAVA开发 在 Spring Boot 中集成 Swagger
java·开发语言·spring boot
2402_857583491 小时前
“协同过滤技术实战”:网上书城系统的设计与实现
java·开发语言·vue.js·科技·mfc
白宇横流学长1 小时前
基于SpringBoot的停车场管理系统设计与实现【源码+文档+部署讲解】
java·spring boot·后端