想要了解如果我们必须在 Vert.x 中构建多个(在我的例子中为三个)API,我们应该遵循的方法
我们是否应该为每个 API 运行单独的垂直,即有一个主 verticle,并为每个 API 运行三个 verticle。主Verticle向每个Verticle发送消息,该Verticle负责制定消息并将其发回
或者
只有一个 verticle,只需通过方法调用将其路由到单独的方法
请建议我们应如何决定该方法。
最佳答案
我同意@berserkk。将其保留在一个 verticle 中,否则 API 的不同部分必须在不同端口上运行 - 因为您无法在监听同一端口的多个 verticle 中创建多个 HTTP 服务器。
但是,拆分 API 的一个好方法可能是基于 vert.x 路由器。假设您有这样结构的 API 子模块
/products/...
/inventory/...
/orders/...
对于每个子模块,您都可以创建一个 Router
提供程序,例如ProductsRouterProvider
、InventoryRouterProvider
或 OrdersRouterProvider
。
每个提供商都负责特定的 API 模块 - 基本上注册所需的路由。
class ProductsRouterProvider {
static Router create(Vertx vertx) {
Router router = Router.router(vertx)
router.get("/").handler(new ListAllProductsHandler())
router.get("/:productId").handler(new ProductDetailHandler())
...
return router;
}
}
最后,在基础 Router
上注册子模块 Routers
。
Router router = Router.router(vertx)
router.mountSubRouter("/", ProductsRouterProvider.create(vertx))
router.mountSubRouter("/", InventoryRouterProvider.create(vertx))
router.mountSubRouter("/", OrdersRouterProvider.create(vertx))
关于java - Vert.x 垂直方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44397636/