简体   繁体   English

Nuget 与微服务共享库的多回购解决方案

[英]Nuget vs Multi-repo solution for Microservices shared libraries

Story:故事:

  1. We have a lot of microservices and communication happens mostly through Service Bus by sending serialized DTOs .我们有很多microservices ,通信主要通过Service Bus通过发送serialized DTOs进行。
  2. Some microservices share the DB , so entity models , for now, are duplicated in each microservice.一些微服务share the DB ,因此实体models目前在每个微服务中都是duplicated的。

Problems:问题:

  1. Whenever we want to modify DTO which is used for communication between microservice we need to modify it in each microservice .每当我们想要modify DTO时,我们都需要modify it in each microservice
  2. Any change in the shared DB requires making changes in all related microservices and single DB field edit causes multiple PRs . change in the shared DB需要changes in all related microservices ,并且单个数据库字段编辑会导致multiple PRs

Possible solutions:可能的解决方案:

Move any shared code to other repositories (DTOs repo, Entity Models repo, etc.), and create solutions with Class Library projects .将任何共享代码移动到其他存储库(DTO 存储库、实体模型存储库等),并使用Class Library projects创建解决方案。

I have two approaches after this point:在这一点之后我有两种方法:

  • Create NuGets and add them to microservices.创建NuGets并将它们添加到微服务。
  • Add bare Class Library projects as a reference for all microservices and we'll get Multi-repo solutions with microservices.添加bare Class Library projects作为所有微服务的参考,我们将获得微服务Multi-repo solutions

Pros/Cons:优点缺点:

For NuGets I see mostly cons:对于NuGets ,我看到的主要是缺点:

  • It will require some extra infrastructure built around it to create artifacts.它将需要围绕它构建一些extra infrastructure来创建工件。
  • To test any change it will be needed to modify Nuget Solution, trigger some CI pipeline and wait to build the NuGet itself, update the microservice with a test version of NuGet , and only after that we can test the microservice itself. To test any change ,需要修改 Nuget 解决方案,触发一些 CI 管道并wait to build the NuGet本身,使用测试版本NuGet更新微服务,然后我们才能测试微服务本身。
  • If any errors occur - repeat an entire process .如果出现any errors - repeat an entire process

For bare Class Library projects I see mostly advantages:对于bare Class Library projects ,我主要看到优点:

  • VS 2022 brought some nice support for Multi-repo solutions. VS 2022 support for Multi-repo
  • It will be possible to make changes in shared projects and immediately test them with actual microservices.可以changes in shared projectsimmediately test

Questions:问题:

  • Could you add any pros/cons for my possible solutions ?你能为我possible solutions添加任何优点/缺点吗?
  • Could you recommend any other solutions for problems (with pros/cons)?你能推荐任何其他解决问题的方法吗(有优点/缺点)?

Not an answer for your question, at least a full one, but few things to think about:不是您问题的答案,至少是一个完整的问题,但需要考虑的事情很少:

I would recommend to start deeper and look into pros and cons for monorepository vs multirepositories which has been discussed many times (for example here or here ) so go through those articles first.我建议更深入地研究单存储库与多存储库的优缺点,这已经讨论了很多次(例如此处此处),因此首先通过这些文章 go。

Whenever we want to modify DTO which is used for communication between microservice we need to modify it in each microservice.每当我们想要修改用于微服务之间通信的 DTO 时,我们都需要在每个微服务中修改它。

In general case with correct design and versioning approach you don't need to modify DTOs in each microservice on every change (if that change is not relevant to that microservice), unless it is a breaking change and breaking changes should be done as rare as possible and should be handled via versioning.在一般情况下,使用正确的设计和版本控制方法,您不需要在每次更改时修改每个微服务中的 DTO(如果该更改与该微服务无关),除非它是一个重大更改,并且应该尽可能少地进行重大更改可能并且应该通过版本控制来处理。

Another thing you can try looking into - using schema registry to define shared contracts and add it to every project via git submodule and write some kind of generator to build DTOs automatically from it.您可以尝试研究的另一件事 - 使用模式注册表定义共享合同并通过git 子模块将其添加到每个项目,并编写某种生成器从中自动构建 DTO。

Any change in the shared DB requires making changes in all related microservices and single DB field edit causes multiple PRs.共享数据库中的任何更改都需要更改所有相关的微服务,并且单个数据库字段编辑会导致多个 PR。

I would argue that shared database in microservice architecture is a huge antipattern which should be addressed as soon as possible (and yes, I understand that life is not always ideal and does not always align with patterns) and fixing it will remove quite a lot of your current problems, so I would suggest to concentrate on it (if it is possible/feasible) rather then on fixing the symptoms.我认为微服务架构中的共享数据库是一个巨大的反模式,应该尽快解决(是的,我明白生活并不总是理想的,并不总是符合模式)并且修复它会消除很多你目前的问题,所以我建议集中精力解决它(如果可能/可行的话)而不是解决症状。

Also you can look into replicating somehow the VMR which .NET team is shifting to .您也可以考虑以某种方式复制.NET 团队正在转移到的 VMR

As for the actual question for "Bare Class Library projects" and VS multi-repo support - first of all currently multi-repo support is limited only to 10 repos , and not everyone is using VS (for example cause it is not available on Linux ATM), secondary there is a question of setting up build servers to work with this (which would be easier with nugets).至于“Bare Class Library projects”和 VS multi-repo support 的实际问题——首先,目前 multi-repo 支持仅限于 10 个 repos ,并不是每个人都在使用 VS(例如因为它在 Linux 上不可用) ATM),其次是设置构建服务器来处理这个问题(使用 nugets 会更容易)。

To test any change it will be needed to modify Nuget Solution, trigger some CI pipeline and wait to build the NuGet itself, update the microservice with a test version of NuGet, and only after that we can test the microservice itself.要测试任何更改,需要修改 Nuget 解决方案,触发一些 CI 管道并等待构建 NuGet 本身,使用测试版本 NuGet 更新微服务,然后我们才能测试微服务本身。

You could create a local nuget source and put your locally packed nuget in there.您可以创建本地 nuget 源并将本地打包的 nuget 放在那里。

Just be mindful of the nuget cache.请注意 nuget 缓存。 If you want to update your nuget package and still use the same version, you should remove it from http-cache first.如果你想更新你的 nuget package 并且仍然使用相同的版本,你应该先从http-cache中删除它。 The location can be obtained via do.net nuget locals --list http-cache or NUGET_HTTP_CACHE_PATH env variable.该位置可以通过do.net nuget locals --list http-cacheNUGET_HTTP_CACHE_PATH变量获取。

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

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