MICROSERVICES - 它们之间的通信 [英] MICROSERVICES - communication between them

查看:47
本文介绍了MICROSERVICES - 它们之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于微服务架构的问题.我正在设计一个基于微服务的系统.我读了几篇文章,我想我理解这个想法.但是,我不知道微服务在具有独立的业务职责的情况下应该如何相互通信....我的意思是,如果我有一个预订火车票的系统,我会将后端应用程序划分为模块:

I've got one question concerning microservices architecture. I am designing a system based on microservices. I've read few articles and I think I understand the idea. However, I don't how microservices should communicate with each other while they have separate business responsibilities.... What I mean is that if I have a system for booking train tickets, I would divide backend application into modules:

  • 客户端(登录、注销、注册)
  • 预订(为用户预订火车座位,获取用户的所有预订)
  • 连接详情(搜索连接,获取连接详细信息)
  • 火车(有关列车的信息 - 座位号、班级等)

现在,我只能认为如果用户搜索连接模块 ConnectionsDetails 与火车模块通信并询问特定的火车详细信息.但是其他微服务如何通信呢?如果用户想登录 - 她/他直接询问客户模块,如果她/他想获得她的所有预订 - 直接询问预订模块等......

Now, I can only think that if user search for connections module ConnectionsDetails communicate with Trains module and ask about particular train details. But how could other microservices communicate? If user wants to login - she/he asks directly Client module, if she/he wants to get all her reservations - asks Reservation module DIRECTLY etc...

所以我的问题是,如果模块做不同的事情,它们应该如何通信?如果我的问题微不足道或愚蠢,我很抱歉,我只是从微服务开始.

So my question is, how should modules communicate if they do different things? I'm sorry if my question is trivial or stupid, I'm just starting with microservices.

我不是说我可以使用什么工具进行交流.我的问题是关于逻辑的.在我展示的示例中,如果客户端可以直接询问另一个微服务,为什么一个微服务可以询问另一个微服务?正如我之前所说,如果他们做不同的事情,他们应该如何沟通(关于他们应该互相问什么问题)?

I didn't mean what tools could I use for communication. My question is about logic. In the example I showed, why one microservice could ask another microservice about sth if client can directly ask the another one? As I said earlier, how they should communicate(about what should they ask each other exactly) if they do separate things?

推荐答案

恕我直言,找到正确的上下文、边界和通信渠道是微服务架构中最困难的部分之一.它是关于查找您需要的数据、关系如何以及哪个服务负责什么(负责意味着唯一允许更改它的人).查看 Martin Fowler 的博客.

To find the right contexts, borders and communication channels is imho one of the most difficult parts of a microservice architecture. It is about finding the data you need, how the relationships are and which service is responsible for what (responsible means the only one allowed to change it). Have a look at the Blog from Martin Fowler.

微服务不是模块.每个服务应该是关于开发和部署的独立服务.是的,他们可以相互交流,但客户也可以单独与他们交流.微服务方法也是关于使用正确的工具解决问题.所以每个服务都可以用不同的编程语言实现.他们可以使用不同类型的存储,如 RDMBS、NoSQL 或键值存储.它们将单独缩放 - ConnectionsDetails 实例很多,而 Reservations 实例较少,例如

Microservices is not modules. Each service should be an independent service regarding development and deployment. And yes, they may communicate to each other but a client may also communicate to them individually. The Microservice approach is also about using the right tool for the problem. So each service can be implemented in a different programming language. They can use different kind of storage like RDMBS, NoSQL or Key-Value store. An they will be scaled individually - many instances for ConnectionsDetails and fewer for Reservations e.g.

如果一项服务不可用会发生什么?每个服务都应该尽可能容错,并在没有其他可能的情况下尝试优雅地减少它的服务.您应该考虑通过选择正确的边界、使数据独立并可能引入缓存来最小化服务之间所需的通信.不要忘记 CAP 定理,微服务方法使其更加可见.以下是一些关于弹性的幻灯片,可能会有所帮助.不要共享同一个数据库或在服务之间复制所有内容.

What will happen if one service is not available? Each service should be as fault tolerant as possible and try to decrease it's service gracefully if nothing else is possible. You should think about minimising the needed communication between the services by choosing the right borders, make data independent and maybe introduce caching. Don't forget about the CAP theorem, a microservice approach makes it more visible. Here are some slides about resilience that may help. Do not share the same database or replicate everything between services.

如果模块做不同的事情,它们应该如何通信?".您应该选择一种独立于语言的通信方式,并根据您的问题选择同步或异步方法.作为语言无关的格式,JSON 或 XML 是最常见的.同步通信可以基于REST,异步通信基于消息传递.身份验证(客户端")通常是一种 REST 服务,通过电子邮件发送预订的门票更像是一种消息驱动的异步服务.

"how should modules communicate if they do different things?". You should choose a language independent way of communication and depending on your problem a synchronous or asynchronous method. As a language independent format JSON or XML are most common. Synchronous communication can be based on REST, asynchronous communication on messaging. The authentication ("Client") is typically a REST service, sending the booked tickets via Email is more a message driven asynchronous service.

这篇关于MICROSERVICES - 它们之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆