简体   繁体   English

微服务、共享实体或其他地狱之间的通信

[英]Communication between microservices, share entity or other hell

read many books about microservices, and dont find answer on my question.阅读了很多关于微服务的书籍,但没有找到关于我的问题的答案。

Situation: Has service A Service A, has entity Cat情况:有服务A Service A,有实体Cat

Has service B Service B need get Cat from service A add some new fields in class and send to service C.有服务 B 服务 B 需要从服务 A 获取 Cat 在 class 中添加一些新字段并发送到服务 C。

Question: 1) i need share entity Cat between services?问题:1)我需要在服务之间共享实体 Cat 吗? (I known is bad idea, because we get tight-coupling) 2) Create some data class in Service B for deserialise response to object (so.. we get code duplication) 3) maybe has good pattern to communicate? (我知道这是个坏主意,因为我们得到了紧耦合) 2)在服务 B 中创建一些数据 class 以反序列化对 object 的响应(所以..我们得到代码重复) 3)可能有良好的通信模式?

IMHO it's not longer a Cat after service B enriched the data from service A. Since a micro-service should encapsulate a domain there should only be a single service that produces Cat s which in your example is service A. Service B is producing something else, maybe it's a CatHistory or CatWithToys but not a Cat .恕我直言,在服务 B 丰富了服务 A 的数据之后,它不再是Cat 。由于微服务应该封装一个域,因此应该只有一个服务产生Cat ,在您的示例中是服务 A。服务 B 正在产生其他东西,也许是CatHistoryCatWithToys但不是Cat There is no code duplication here as service A is not aware of the fields that are enriched by service B, they are outside service A domain.这里没有代码重复,因为服务 A 不知道服务 B 丰富的字段,它们在服务 A 域之外。

Short answer: #2简短回答:#2

Long answer:长答案:

I was involved in a large microservices project where we, initially, shared code (DTOs) between projects to make comms easier.我参与了一个大型微服务项目,最初我们在项目之间共享代码 (DTO) 以使通信更容易。 We learnt it's a painful approach.我们了解到这是一种痛苦的方法。 First, an update to the "api" library meant updating all the clients of the service, even if most of them didn't need the changes.首先,“api”库的更新意味着更新服务的所有客户端,即使它们中的大多数不需要更改。 Second, there was no way to ensure backwards compatibility.其次,没有办法确保向后兼容。 Even though we could update all services to the same version of the library and everything would pass, deployments don't typically work atomically like that in an always-on environment, so changes were inherently unsafe.尽管我们可以将所有服务更新到相同版本的库并且一切都会通过,但部署通常不会像在永远在线的环境中那样以原子方式工作,因此更改本质上是不安全的。 Lastly, almost no clients need all the fields that come out of the API, yet they had an object with all the fields being used in their code.最后,几乎没有客户端需要来自 API 的所有字段,但他们有一个 object,所有字段都在其代码中使用。

Eventually, we did away with API projects.最终,我们取消了 API 项目。 Really, it's the JSON produced by a service that's the API, not the DTO used to generate the JSON.实际上,它是由 API 服务生成的 JSON,而不是用于生成 JSON 的 DTO。 Clients would have their own DTO containing just the fields they needed.客户将拥有自己的 DTO,其中仅包含他们需要的字段。 Contract testing using Pact ensured that APIs didn't change in backwards-incompatible ways.使用 Pact 进行合同测试可确保 API 不会以向后不兼容的方式更改。

There is probably not just one right answer for this, as it will always be a bit opinionated.这可能不只有一个正确的答案,因为它总是有点固执己见。

I would prefer the loose coupling and go for a contract approach.对于合同方法,我更喜欢松散耦合和 go。 As I guess that you are not interested in making SOAP based web services, my best guess is that you would be making some kind of RESTful services.我猜你对制作基于 SOAP 的 web 服务不感兴趣,我最好的猜测是你会制作某种 RESTful 服务。

I would take a look at Swagger (or some similar technology) for making a "contract first" approach.我会看看Swagger (或一些类似的技术)来制定“合同优先”的方法。 That way you should be able to generate the API classes in the server (producer) and client (consumer) from the contract.这样,您应该能够在合约的服务器(生产者)和客户端(消费者)中生成 API 类。

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

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