Sharing code and schema between microservices

后端 未结 4 2139
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 17:27

If you go for a microservices architecture in your organization, they can share configuration via zookeeper or its equivalent. However, how should the vario

4条回答
  •  一生所求
    2020-12-23 18:08

    The "purest" approach, i.e. the one that gives you the least amount of coupling, is to not share any code.

    If you find that two services (call them A and B) need the same functionality, your options are:

    • split if off as a separate service C, so A and B can use C
    • bite the bullet and duplicate the code

    While this may sound awkward, you avoid the (not uncommon) problem of creating a "utility" or "common" or "infrastructure" library which everyone depends on, and which is then really hard to upgrade and change (i.e. which indirectly couples the services).

    In practice, as usual, it's a tradeoff.

    • If the shared functionality is substantial, I'd go for a seperate service.
    • If it's just constants, a shared library might be the best solution. You need to be very careful about backwards compatibility, though.
    • For configuration data, you could also implement a specific service, possibly using some existing technology such as LDAP.
    • Finally, for simple code that is likely to evolve independently, just duplicating might be the best solution.

    However, what's best will depend on your specific situation and problem.

提交回复
热议问题