简体   繁体   English

如何跨微服务拥有通用的 DTO 或实体

[英]How to have common DTOs or entities across Microservices

Say I have 5 different microservices.假设我有 5 个不同的微服务。 Each use one common DTO, that is UserDTO.java or an entity User.java.每个都使用一个公共 DTO,即 UserDTO.java 或实体 User.java。

If these DTOs are to be accessed by all, I came up with two approaches如果所有人都可以访问这些 DTO,我想出了两种方法

  1. To place all these DTOs in all the microservices将所有这些 DTO 放在所有微服务中

  2. To create a project say Project-commons and keep all the common DTOs there and then add the dependency of this project in all microservices要创建一个项目,说Project-commons并保留所有常见的 DTO,然后在所有微服务中添加该项目的依赖项

I know that the sharing common DTOs across microservices would kill the concept/principles of microservices, but I wanted to know is there any other approaches available at all?我知道跨微服务共享通用 DTO 会扼杀微服务的概念/原则,但我想知道还有其他可用的方法吗?

Actually, sharing a common code library does not kill the concept/principle of keeping changes isolated to one microservice, as long as you strictly version that library.实际上,只要您严格控制该库的版本,共享一个公共代码库并不会消除将更改隔离到一个微服务的概念/原则。 That way, one microservice team can decide it needs new functionality in the shared library, and can bump the version number of the library and add the new functionality.这样,一个微服务团队可以决定它需要共享库中的新功能,并且可以增加库的版本号并添加新功能。 Existing microservices that use that library won't be affected until they independently decide to start using the newer version of that library.使用该库的现有微服务不会受到影响,除非它们独立决定开始使用该库的较新版本。

So this is what we do.所以这就是我们要做的。 We have jar files that we share across our microservices, but we push specific versions of those jar files to Nexus and reference those jar files by version number in each microservice.我们有在我们的微服务中共享的 jar 文件,但我们将这些 jar 文件的特定版本推送到 Nexus 并按每个微服务中的版本号引用这些 jar 文件。

Note that this concept is no different than two microservices depending on a third party component, like an Apache Commons library.请注意,此概念与依赖于第三方组件(如 Apache Commons 库)的两个微服务没有区别。 Just because Apache comes out with a new version of a library doesn't mean anyone's binaries change.仅仅因为 Apache 推出了新版本的库并不意味着任何人的二进制文件都会发生变化。 Each codebase that depends on that library can decide if and when it moves to the new version independent of what version another codebase might be using or what the most recent version of the library might be.每个依赖于该库的代码库都可以决定是否以及何时迁移到新版本,而与另一个代码库可能使用的版本或库的最新版本无关。

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

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