在微服务之间共享代码和模式 [英] Sharing code and schema between microservices

查看:48
本文介绍了在微服务之间共享代码和模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在组织中采用微服务架构,他们可以通过 zookeeper 或其相等的.但是,各种服务应该如何共享一个公共数据库架构?常用常数?和公用事业?

If you go for a microservices architecture in your organization, they can share configuration via zookeeper or its equivalent. However, how should the various services share a common db schema? common constants? and common utilities?

一种方法是将所有微服务放在同一个代码存储库中,但这与微服务带来的解耦相矛盾...

One way would be to place all the microservices in the same code repository, but that would contradict the decoupling that comes with microservices...

另一种方法是让每个微服务完全独立,但这会导致每个微服务必须保存的单独数据库中的代码重复和数据重复.

Another way would be to have each microservice be completely independent, however that would cause code duplication and data duplication in the separate databases each microservice would have to hold.

另一种方法是在没有上下文\状态的情况下实现功能性微服务,但这通常是不现实的,并且会推动架构拥有一个中央集线器来维护上下文\状态和来自\到它的大量流量.

Yet another way would be to implement functional microservices with no context\state, but that's usually not realistic and would push the architecture to having a central hub that maintains the context\state and a lot of traffic from\to it.

在微服务之间共享代码和架构的可扩展的、高效的、实用的并且有希望的漂亮方式是什么?

推荐答案

关于通用代码,最好的做法是使用打包系统.因此,如果您使用 Java,则使用 maven,如果您使用 Ruby,则使用 Gems,如果使用 python,则使用 pypi 等.理想情况下,打包系统几乎不会增加摩擦,因此您可能有一个(例如,git)存储库用于公共库(或用于不同主题的多个公共库),并通过工件存储库(例如私有 maven/gems/pypi)发布它们的工件.然后在微服务中添加对所需库的依赖.因此代码重用很容易.在某些情况下,打包系统确实会增加一些摩擦(maven),因此人们可能更喜欢使用单个 git repo 来处理所有内容和多模块项目设置.这不像第一种方法那么干净,但效果很好而且还不错.其他选项是使用 git submodule(不太需要)或 git subtree(更好),以便将源代码包含在单个父"存储库中.

Regarding common code, the best practice it to use a packaging system. So if you use Java, then use maven, if you use Ruby then Gems, if python then pypi etc. Ideally a packaging system adds little friction so you may have a (say, git) repository for a common lib (or several common-libs for different topics) and publish their artifacts through an artifact repository (e.g. private maven/gems/pypi). Then at the microservice you add dependency on the required libs.So code reuse is easy. In some cases packaging systems do add some friction (maven for one) so one might prefer using a single git repo for everything and a multi-module project setup. That isn't as clean as the first approach but works as well and not too bad. Other options are to use git submodule (less desired) or git subtree (better) in order to include the source code in a single "parent" repository.

关于模式 - 如果你想按原样玩,那么每个微服务都有自己的数据库.他们不接触彼此的数据.这是一种非常模块化的方法,起初似乎会给您的流程增加一些摩擦,但最终我想您会感谢我的.它将允许对您的微服务进行快速迭代,例如,您可能希望将一个数据库实现替换为一个特定服务的另一个数据库实现.想象一下,当您的所有服务都使用相同的数据库时这样做!祝你好运......但是如果每个服务都使用它自己的数据库,该服务正确地抽象了数据库(例如,它不接受 SQL 查询作为 API 调用;-))然后将 mysql 更改为 Cassandra 突然变得可行.拥有完全隔离的数据库还有其他好处,例如负载和扩展、找出瓶颈、管理等.

Regarding schema - if you want to play by the book, then each microservice has its own database. They don't touch each other's data. This is a very modular approach which at first seems to add some friction to your process but eventually I think you'll thank me. It will allow fast iteration over your microservices, for example you might want to replace one database implementation with another database implementation for one specific service. Imagine doing this when all your services use the same database! Good luck with that... But if each single service uses it's own database the service abstracts the database correctly (e.g. it does not accept SQL queries as API calls for example ;-)) then changing mysql to Cassandra suddenly become feasible. There are other upsides to having completely isolated databases, for example load and scaling, finding out bottlenecks, management etc.

简而言之 - 通用代码(实用程序、常量等) - 使用打包系统或一些源代码链接,例如 git-tree

So in short - common code (utilities, constants etc) - use a packaging system or some source code linkage such as git-tree

数据库 - 你不碰我的,我不碰你的.这是解决这个问题的更好方法.

Database - you don't touch mine, I don't touch yours. That's the better way around this.

HTH,冉.

这篇关于在微服务之间共享代码和模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆