Vertx 未绑定路由器

Vertx is not binding Routers

嗯,我觉得自己真的迷失了 Vertx 结构,因为一切都是 lambda 表达式。 我完全按照 this 教程来构建我的应用程序, 不幸的是它没有注册任何路由器我不知道为什么。请在下面找到我所做的

serviceEndPoint 同上教程

import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;

public interface ServiceEndPoint {
    String mountPoint();

    Router router(Vertx vertx);
}

这里是订阅服务

import com.poc.poc.repositories.SubscriptionRepository;
import com.poc.poc.services.ServiceEndPoint;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;

public class SubscriptionService implements ServiceEndPoint {
    private SubscriptionRepository subscriptionRepository;

    public SubscriptionService() {
        subscriptionRepository = new SubscriptionRepository();

    }

    @Override
    public String mountPoint() {
        return "/test";
    }

    @Override
    public Router router(Vertx vertx) {
        Router router = Router.router(vertx);
        router.get("/test").handler(rc -> rc.response().end(subscriptionRepository.getSubscriptionInfo(rc, vertx).toString()));
        return router;
    }
}

最后是垂直服务器

import com.poc.poc.services.ServiceEndPoint;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

import java.util.ServiceLoader;
import java.util.stream.StreamSupport;

import io.vertx.ext.web.Router;

public class ServerVertical extends AbstractVerticle {

    @Override
    public void start(final Future<Void> startFuture) throws Exception {

        ServiceLoader<ServiceEndPoint> loader = ServiceLoader.load(ServiceEndPoint.class);

        Router main = StreamSupport.stream(loader.spliterator(), false)
            .collect(() -> Router.router(vertx), //the main router
                (r, s) -> r.mountSubRouter(s.mountPoint(), s.router(vertx)),
                (r1, r2) -> {
                });

        vertx.createHttpServer().requestHandler(main::accept).listen(8080, res -> {
            if (res.succeeded()) {
                startFuture.complete();
            } else {
                startFuture.fail(res.cause());
            }
        });
    }
}

请注意一旦我 运行 应用程序,我收到这些警告

Jun 12, 2018 6:16:45 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 2486 ms, time limit is 2000
Jun 12, 2018 6:16:46 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 3485 ms, time limit is 2000

顺便说一句 Router main = StreamSupport.stream(loader.spliterator(), false) 大小是 0。

有帮助吗?

首先,并非 Vert.x 中的所有内容都是 lambda 表达式。那只是您发现的一个非常奇怪的教程。如您所见,它使用 java.util.ServiceLoader 而不是 Vert.x class。我也不熟悉任何其他人建议将此 class 与 Vert.x 应用程序一起使用。
它试图做的是动态加载您的 classes。您可能错过的是将正确的文件放在 META-INF 目录中,如下所述:https://docs.oracle.com/javase/tutorial/ext/basics/spi.html#register-service-providers

无论如何,这不是我推荐的使用 VertX 的方式。相反,请使用非常好的常规 VertX 教程:https://vertx.io/docs/vertx-web/java/#_handling_requests_and_calling_the_next_handler

一旦你创建了你的服务接口com.poc.poc.services.ServiceEndPoint)声明和具体实现(SubscriptionService),你应该添加服务提供商绑定.

根据 ServiceLocator 文档,绑定应插入以您接口 FQN 命名的文件下,即在 META-INF/services/com.[=31 下=](整个目录结构在项目/模块resources目录下)。

该文件将包含实际的接口实现:

com.poc.poc.services.SubscriptionService