我们有一个MainVerticle,它随着诸如:
router.get("/vehicles/:id").handler(ctx -> {
LOG.info("Entering get vehicle details");
getVehicleDetailsCounter.inc();
vehicleApiImpl.getVehicleDetails(ctx, httpClient);
});
router.post("/vehicles/:id/journey").handler(ctx -> {
// BOOK VEHICLE
LOG.info("Entering book vehicle");
startJourneyCounter.inc();
vehicleApiImpl.startJourney(ctx, httpClient);
});
router.post("/vehicles/:id/trips/:tripId/reports").handler(ctx -> {
LOG.info("Entering trip reports");
tripReportsCounter.inc();
getTripReports(ctx, httpClient);
});
router.get("/availableVehicles/:cityId").handler(ctx -> {
LOG.info("Entering available vehicles");
availableVehCounter.inc();
getAvailableVehicles(ctx, httpClient);
});
router.get("/zonesDetails/:cityId").handler(ctx -> {
LOG.info("Entering zones details");
getZonesDetailsCounter.inc();
vehicleApiImpl.getZonesDetails(ctx, httpClient);
});
router.get("/models").handler(ctx -> {
LOG.info("Entering models");
modelsCounter.inc();
vehicleApiImpl.getModels(ctx, httpClient);
});
router.get("/options").handler(ctx -> {
LOG.info("Entering options");
optionsCounter.inc();
vehicleApiImpl.getOptions(ctx, httpClient);
});
// ============================
// USER
// ============================
LOG.info("Handler register : USER");
// Payment Details
router.post("/user/notifyAppPaymentTransaction").handler(ctx -> {
LOG.info("Entering payment transaction");
notifyAppPaymentTransaction(ctx, httpClient);
});
// The user current journey
router.get("/user/currentJourney").handler(ctx -> {
LOG.info("Entering get current journey");
getCurrentJourneyCounter.inc();
userApiImpl.getCurrentJourney(ctx, httpClient);
});
// Create a new user
router.post("/users").handler(ctx -> {
LOG.info("Entering create user");
createUserCounter.inc();
createUser(ctx, httpClient);
});
...
我们需要继续监听单个ip:端口。
将这个MainVerticle
分成几个类,同时保持单个顶点,这是一个好主意吗?
一种显而易见的方法是将静态辅助类放入路由器并在内部进行映射。但是,如果Vertx中存在一个现有的模式,例如使用路由器,那确实会有帮助。
先谢谢了。
答案 0 :(得分:2)
例如,您可以在其他处理程序中提取车辆路线。然后,在处理程序中,您可以选择在其中实现您的业务逻辑,或者更好地通过事件总线发送消息,在其他任何Verticle中使用该消息,在其中执行您的业务逻辑,并以消息的答案进行答复,您将以回应。
router.route("/vehicles/*").handler(VehicleHandler.create(vertx, router));
VehicleHandler
public interface VehicleHandler extends Handler<RoutingContext> {
static VehicleHandler create(Vertx vertx, Router router) {
return new VehicleHandlerImpl(vertx, router);
}
}
VehicleHandlerImpl
public class VehicleHandlerImpl implements VehicleHandler {
private Router router;
public VehicleHandlerImpl(Vertx vertx, Router router) {
this.router = router;
router.get("/:id/"").handler(this::getVehicle);
router.post("/:id/trips/:tripId/reports").handler(this::postReports);
router.post(":id/journey").handler(this::postJourney);
}
@Override
public void handle(final RoutingContext ctx) {
router.handleContext(ctx);
}
private void getVehicle(RoutingContext ctx) {
//Option A: do you business logic here
}
private void postReports(RoutingContext ctx) {
//Option B: send an eventbus message, handle the message in the MainVerticle and serve the response here
}
private void postJourney(RoutingContext ctx) {
//Option C: send an eventbus message, handle the message in a new Verticle and serve the response here
}
}
答案 1 :(得分:0)
//Main Clss
class Main : AbstractVerticle() {
override fun start() {
val router = Router.router(vertx) router.route().handler(BodyHandler.create())
router.get("/vehicles/:id").handler { req ->Controller.get_vehicle(req)}
router.get("/vehicles/:id/journey").handler{req->Controller.startJourney(req)}
}
}
// Controller Class
open class Controller {
companion object {
fun get_vehicle(routingContext: RoutingContext) {// `enter code here`}
}
}