首页 > java > 在多个微服务之间共享代码

在多个微服务之间共享代码 (Sharing code between multiple micro-services)

问题

我正在开发三种微服务

  1. 查看优惠
  2. 添加优惠(适用于管理员)
  3. 选择优惠

查看商品用于向客户显示商品和添加服务管理员用于将商品链接到客户的商品,如果商品与客户相关联,则商品视图商品将返回该商品作为回应。客户使用Opt优惠来获取优惠。

在设计时,我意识到所有3个服务共享相同的功能,但也有自己的逻辑。

我应该在开发这些API时使用共享代码,还是每个API都必须有自己的代码库?

解决方法

如果您正在使用,maven那么您可以使用Maven模块。这基本上意味着您可以将服务代码提取到单独的模块中,并将该模块作为pom所有3个微服务中的依赖项包含在内。

这是一个如何使用它的例子Spring。如果您没有使用,Spring那么您可以在这里查看另一个示例。

问题

I am developing three micro-services

  1. View offers
  2. Add offers(for administrator)
  3. Opt offers

View offers is used to show offers to customer and Add offer used by service admins to link offer to customer and if offer is linked to customer, view offer will return that offer in response. Opt offers is used by customer to avail the offer.

While designing, I realized that all 3 services share same functionality, but also have their own logic.

Should I use shared code while developing these API or each API must have its own code base?

解决方法

If you are using maven then you can use Maven Modules. This basically means you can extract the service code into separate module and include that module as a pom dependency in all 3 microservices.

Here's an example of how to do it using Spring. If you are not using Spring then you can have a look at another example here.

相似信息