-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
binghe
committed
Dec 12, 2023
1 parent
951d397
commit b8186be
Showing
10 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: 第05节:MQ消息发送通用代码设计与实现 | ||
pay: https://articles.zsxq.com/id_wo2h9ier7nqr.html | ||
--- | ||
|
||
# 《分布式IM系统》通用模型-第05节:MQ消息发送通用代码设计与实现 | ||
|
||
作者:冰河 | ||
<br/>星球:[http://m6z.cn/6aeFbs](http://m6z.cn/6aeFbs) | ||
<br/>博客:[https://binghe.gitcode.host](https://binghe.gitcode.host) | ||
<br/>文章汇总:[https://binghe.gitcode.host/md/all/all.html](https://binghe.gitcode.host/md/all/all.html) | ||
<br/>源码获取地址:[https://t.zsxq.com/0dhvFs5oR](https://t.zsxq.com/0dhvFs5oR) | ||
|
||
> 沉淀,成长,突破,帮助他人,成就自我。 | ||
|
||
* 本节难度:★★☆☆☆ | ||
* 本节重点:实现MQ消息发送的通用代码设计,使代码满足需求的同时,兼具优雅性,并具备良好的性能与扩展性。 | ||
|
||
**大家好,我是冰河~~** | ||
|
||
消息中间件是解耦分布式系统的利器,使用消息中间件不仅能够让分布式系统中的各个微服务之间进行解耦,也能够起到削峰填谷的作用,还能异步执行一些非关键链路的业务逻辑。可以这么说,消息中间件已经成为构建分布式系统不可或缺的重要基础组件。 | ||
|
||
## 一、前言 | ||
|
||
在分布式系统中,如何降低各个微服务之间的耦合度?如何对流量进行削峰填谷?相信很多小伙伴心中已经有了答案:使用消息中间件。另外,使用消息中间件也能够异步执行一些非关键链路的业务逻辑,提升系统的性能和吞吐量。 | ||
|
||
那么问题来了,引入消息中间件的话,我们又如何编写优雅的代码来引入消息中间件呢?能不能做到系统不强制依赖某一款消息中间件呢?如何使我们的系统在引入消息中间件时,也具备高度的可扩展性呢? | ||
|
||
## 二、本节诉求 | ||
|
||
如果一个单体应用需要解耦来异步执行一些业务逻辑时,可以使用JDK中提供的队列来实现,也可以使用阿里巴巴开源的[Cola](https://github.com/alibaba/cola)框架实现。但是在分布式场景下,无论时使用JDK提供的队列,还是使用阿里巴巴开源的Cola框架,都已经显得力不从心了。此时,就需要引入消息中间件来解决相应的问题。 | ||
|
||
本章,就实现MQ消息发送的通用代码设计。 | ||
|
||
## 三、代码设计 | ||
|
||
在设计MQ消息发送的通用代码时,同样会从代码场景、代码扩展性和代码类结构等方面进行详情的说明。 | ||
|
||
### 3.1 代码场景设计 | ||
|
||
如果分布式系统中的各个微服务之间需要异步解耦,此时可以引入消息中间件来完成各个微服务之间的异步解耦。例如,假设存在服务A和服务B两个微服务,服务A在接收到请求后,执行对应的业务逻辑,经过一系列的处理,将数据发送给消息中间件即可返回结果。此时监听消息中间件中数据的服务B就会接收到对应的数据,服务B开始执行业务逻辑,如图5-1所示。 | ||
|
||
<div align="center"> | ||
<img src="https://binghe.gitcode.host/images/project/im/2023-12-13-001.png?raw=true" width="70%"> | ||
<br/> | ||
</div> | ||
|
||
可以看到,使用消息中间件降低了服务A和服务B之间的耦合度。并且使用消息中间件时,服务A可以扩展成集群模式,服务B同样可以扩展成集群模式。此时服务A集群中将消息发送到消息中间件后,即可返回,期间并不关心消息数据会被服务B集群中的哪个具体服务实例消费。同样的,服务B集群消费消息中间件中的数据,并不关心这些消息是服务A集群中具体哪个服务实例发送的。 | ||
|
||
### 3.2 代码扩展性设计 | ||
|
||
分布式IM即时通讯系统总体上是基于SpringBoot实现,那基于SpringBoot实现的代码如何才能具备良好的扩展性呢?这里,我们再来加深下对代码具备良好扩展性的原则的理解。**总体的原则就是面向接口编程,而非面向具体的实现类编程,具体业务逻辑里依赖的是接口,而非实现类,在接口不变的前提下,可以随时切换具体的实现类,也可以随时新增接口的实现类。业务中可以根据配置加载接口的某个具体实现类。** | ||
|
||
<div align="center"> | ||
<img src="https://binghe.gitcode.host/images/project/im/2023-12-13-002.png?raw=true" width="70%"> | ||
<br/> | ||
</div> | ||
|
||
## 查看完整文章 | ||
|
||
加入[冰河技术](https://public.zsxq.com/groups/48848484411888.html) 知识星球,解锁完整技术文章、小册、视频与完整代码 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters