简体   繁体   English

MICROSERVICES - 他们之间的沟通

[英]MICROSERVICES - communication between them

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: 但是,我不知道微服务应该如何相互沟通,而他们有不同的业务职责。我的意思是,如果我有一个预订火车票的系统,我会将后端应用程序划分为模块:

  • Client (login,logout,registration) 客户(登录,注销,注册)
  • Reservations (booking a train seat for user,getting all reservations for user) 预订(为用户预订火车座位,为用户预订所有预订)
  • ConnectionsDetails (searching for connections,getting connection details) ConnectionsDetails(搜索连接,获取连接详细信息)
  • Trains (information about trains- seats number,class etc.) 火车(有关火车 - 座位号码,班级等的信息)

Now, I can only think that if user search for connections module ConnectionsDetails communicate with Trains module and ask about particular train details. 现在,我只能认为如果用户搜索连接模块ConnectionsDetails与Trains模块通信并询问特定的列车详细信息。 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. 我很抱歉,如果我的问题是微不足道或愚蠢的,我只是从微服务开始。

EDIT: 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? 正如我之前所说的那样,如果他们分开的话,他们应该如何沟通(关于他们应该相互询问的内容)?

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 . 看看Martin Fowler博客

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. 他们可以使用不同类型的存储,如RDMBS,NoSQL或Key-Value存储。 An they will be scaled individually - many instances for ConnectionsDetails and fewer for Reservations eg 它们将被单独缩放 - ConnectionsDetails的许多实例和预留的更少实例,例如

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. 不要忘记CAP定理 ,微服务方法使其更加明显。 Here are some slides about resilience that may help. 这里有一些关于弹性的幻灯片可能有所帮助。 Do not share the same database or replicate everything between services. 不要共享相同的数据库或复制服务之间的所有内容。

"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. 作为独立于语言的格式,JSON或XML是最常见的。 Synchronous communication can be based on REST, asynchronous communication on messaging. 同步通信可以基于REST,消息传递上的异步通信。 The authentication ("Client") is typically a REST service, sending the booked tickets via Email is more a message driven asynchronous service. 身份验证(“客户端”)通常是REST服务,通过电子邮件发送预订的故障单更像是消息驱动的异步服务。

As I think that is a major question about classical SOA vs. Microservices. 我认为这是关于经典SOA与微服务的主要问题。

I guess you can find many opposite answers to that. 我想你可以找到许多相反的答案。

so IMHO: If in your architecture services communicate each other they are not microservices, since there are dependencies between them. 所以恕我直言:如果您的架构服务彼此通信,它们不是微服务,因为它们之间存在依赖关系。

Instead of that if each microservice has all needed functionality (or say components) and do not depend or communicate to each other then they are microservices. 如果每个微服务具有所有需要的功能(或称组件)并且不依赖于彼此或彼此通信,那么它们就是微服务。

So in your example you have 4 components. 所以在你的例子中你有4个组件。 Clients, Reservations, ConnectionsDetails, Trains. 客户,预订,ConnectionsDetails,火车。

But, microservices may not necessary match them exactly. 但是,微服务可能没有必要与它们完全匹配。 As you said "if user search Connection"... So "Search Connection" that is microservice which includes all needed components (Client, ConnectionDetails, Trains) and is independent. 正如你所说“如果用户搜索连接”......所以“搜索连接”即微服务,其中包括所有需要的组件(Client,ConnectionDetails,Trains)并且是独立的。

And finally, how components (not microservices) will communicate to each other is up to you. 最后,组件(而不是微服务)如何相互通信取决于您。 With microservices you have a luxury to use straight POJO with no transformations, protocols, transport layers at all. 使用微服务,您可以使用直接POJO而无需转换,协议,传输层。 Or you can make communications more formal, which push you back closer to classical SOA rather than microservices. 或者你可以使通信更正式,这会让你更接近传统的SOA而不是微服务。

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

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