简体   繁体   English

如何实现2个微服务之间的通信

[英]How to implement communication between 2 micro services

I had a discussion with my coworker about the way we should implement the communication between 2 microservices. 我与我的同事讨论了应如何实现2个微服务之间的通信的方式。 At the moment we have 2 micro services which are depended on each other because my coworker wanted to avoid code duplication by reusing the DTO objects which were used for the REST API. 目前,我们有2个相互依赖的微服务,因为我的同事希望通过重用用于REST API的DTO对象来避免代码重复。

In a perfect world we would not have any dependencies between the services but in our case it makes sense. 在理想的情况下,我们在服务之间不会有任何依赖关系,但在我们看来,这是有道理的。 In my opinion this couples the services which is worse than duplicating a DTO. 在我看来,这与复制DTO相比更糟糕。

Would you duplicate the DTO in second micro service or are there other solutions? 您将在第二个微服务中复制DTO还是有其他解决方案?

I suggest you use GRPC (protocol buffer) fro communicating between microservices. 我建议您在微服务之间进行通信时使用GRPC (协议缓冲区)。

  1. create a maven project contains protocol buffers file( it is like a project contains only DTOs wich share among other projects) 创建一个包含协议缓冲区文件的maven项目(就像一个项目仅包含在其他项目之间共享的DTO)
  2. create how many serivces you like and add protobuf as dependency, it can be even an android project 创建您喜欢的服务并将protobuf添加为依赖项,甚至可以是android项目
  3. use grpc to communicate with your separated services (in a microservices you should never have any dependencies between the services) 使用grpc与您分离的服务进行通信(在微服务中,服务之间永远不应有任何依赖关系)

I used to try different solutions for communicating between services including REST APIs but I found GRPC is a perfect world to do so. 我曾经尝试过在包括REST API在内的服务之间进行通信的不同解决方案,但是我发现GRPC是一个完美的世界。 Also you can have your REST APIs along with GRPC. 您也可以将REST API与GRPC一起使用。

For the concept of "Loose coupling", it is not recommend to design microservices communicating each other via REST. 对于“松散耦合”的概念,建议不要设计通过REST相互通信的微服务。 Here is a good blogpost to understand the concept. 这是理解该概念的好博客。 https://www.beeworks.be/blog/2017/rest-antipattern.html https://www.beeworks.be/blog/2017/rest-antipattern.html
One way is to use a message broker as an enterprice bus. 一种方法是使用消息代理作为Enterprice总线。
If you use message broker, - you may use RPC calls to request one microservice to another. 如果您使用消息代理,则-您可以使用RPC调用向另一个请求一个微服务。 - or you may publish an event(pub/sub) -或者您可以发布事件(发布/订阅)
Here is a good blogpost to understand of concepts of using message broker in microservice infrastructure. 这是一篇很好的博文,了解在微服务基础架构中使用消息代理的概念。
https://dev.to/matteojoliveau/microservices-communications-why-you-should-switch-to-message-queues--48ia https://dev.to/matteojoliveau/microservices-communications-why-you-should-switch-to-message-queues--48ia

Even in this post there were different opinions on how to share models between micro services and how to communicate. 即使在这篇文章中,对于如何在微服务之间共享模型以及如何进行通信也有不同的意见。 I would say the clean solutions is explained in this post. 我会说干净的解决方案在这篇文章中进行了解释。

Micro services: shared library vs code duplication 微服务:共享库与代码重复

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

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