简体   繁体   English

微服务之间基于消息的通信

[英]Message based communication between microservices

I would like to build few number of microservices that send and receive message using a message broker: ActiveMQ . 我想构建一些使用消息代理发送和接收消息的微服务: ActiveMQ Now I am exploring options for message type (ie String , byte[] , object type): 现在,我正在探索消息类型(即Stringbyte[]对象类型)的选项:

  1. publish msg in XML/JSON format and once received they are parsed XML / JSON格式发布味精,并且一旦收到它们就会被解析
  2. publish msg in XML/JSON format, using schema to convert them to objects 使用模式将它们转换为对象,以XML / JSON格式发布msg
  3. Create a Domain object and add to microservices dependency for data exchange 创建一个Domain对象并添加到微服务依赖项以进行数据交换

These are the 3 options I have looked at, and I am leaning towards option 1 for below mentioned reasons : 这些是我研究过的3个选项,出于以下原因,我倾向于选择1:

  1. when a new field needs to be added only those microservices that require this field need change. 当需要添加新字段时,仅需要更改需要该字段的微服务。
  2. Different version of microservices can be deployed without breaking existing communication 可以部署不同版本的微服务,而不会中断现有通信
  3. microservices remain decoupled 微服务保持解耦

However this raises the issue of parsing and extracting data which is very error prone. 但是,这引起了非常容易出错的解析和提取数据的问题。

I would like to know if anyone else has done a similar design and could share their experience and suggest solutions. 我想知道是否还有其他人做过类似的设计并可以分享他们的经验并提出解决方案。 Also if there is a better way to implement communication between microservices using messaging and queues. 同样,如果有更好的方法可以使用消息传递和队列在微服务之间实现通信。

This is the approach I usually take: for each message type, a class is responsible for both encoding the message (generating JSON/XML/whatnot), validating it and parsing it. 这是我通常采用的方法:对于每种消息类型,一个类负责对消息进行编码(生成JSON / XML / whatnot),对其进行验证并对其进行解析。 Classes go into a library that is then used on both the service which is sending the message and on the service that is receiving it. 类进入一个库,然后该库将在发送消息的服务和接收消息的服务上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM