Micro services: shared library vs code duplication

前端 未结 3 1360
长情又很酷
长情又很酷 2021-01-02 16:39

Similar questions were asked a few times, but as every use-case can be different I thought it worth to ask it again with the specific case I\'m facing with. So, we are devel

3条回答
  •  走了就别回头了
    2021-01-02 16:53

    When it comes to model classes, I say duplicate code. The entire idea of using JSON or some other language independent protocol instead of serialized native Java objects (for example) is to avoid side-effects of class changes in one microservice rippling throughout your entire ecosystem. A shared library of model classes locks multiple microservices into a polygamous marriage contract where divorce precipitated by even small disagreements between a pair of members can be very expensive. Service A may require changes to its model that not only are unneeded by Service B, but might be totally incompatible with it.

    This doesn't mean that code cannot be shared across different microservices. There's nothing wrong with sharing libraries among different microservices as long as their functionality is limited to addressing cross-cutting concerns. Perhaps your common timeout and retry functionality you mentioned falls in the category. Model classes that do nothing but hold a data representation of a language independent protocol do not fall into this category in my opinion.

提交回复
热议问题