如何在 Vert.x 中运行多个顶点? [英] How to run multiple vertices in Vert.x?

查看:61
本文介绍了如何在 Vert.x 中运行多个顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Vert.x 的新手,我想通过 jar 运行多个 Verticle.我有两个文件,一个是 MyFirstVertice.java,它路由路径/q1/"并返回一些东西.第二个是 MySecondVertice.java,它路由路径/q2/".第二个顶点部署在第一个顶点.

I am new to Vert.x and I want to run multiple verticles through jar. I have two files, one is MyFirstVertice.java and it routes the path "/q1/" and return something. The second is MySecondVertice.java which routes the path "/q2/". The second vertice is deployed in the first vertice.

MyFirstVertice.java

MyFirstVertice.java

public class MyFirstVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) throws Exception {

    HttpServer server = vertx.createHttpServer();
    Router router = Router.router(vertx);
    router.route("/q1/*").handler(routingContext -> {
        HttpServerRequest request = routingContext.request();
        String Y = request.getParam("key");
        String cipherText = request.getParam("message");

        HttpServerResponse response = routingContext.response();

        response.setChunked(true);
        response.putHeader("content-type", "text/plain");
        response.write(Y + "\n");
        response.write(cipherText + "\n");
        response.end();

        vertx.deployVerticle(new MySecondVerticle(), stringAsyncResult -> {
            System.out.println("Second verticle is deployed successfully.");
        });
    });

    server.requestHandler(router::accept).listen(8080, httpServerAsyncResult -> {
        if (httpServerAsyncResult.succeeded()) {
            fut.complete();
        } else {
            fut.fail(httpServerAsyncResult.cause());
        }
    });
}

}

MySecondVetice.java

MySecondVetice.java

public class MySecondVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) throws Exception {

    HttpServer server = vertx.createHttpServer();
    Router router = Router.router(vertx);
    router.route("/q2/*").handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        response.setChunked(true);
        response.putHeader("content-type", "text/plain");

        response.end("q2");
    });

    server.requestHandler(router::accept).listen(8080, httpServerAsyncResult -> {
        if (httpServerAsyncResult.succeeded()) {
            fut.complete();
        } else {
            fut.fail(httpServerAsyncResult.cause());
        }
    });
}

}

我的 pom.xml

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>io.vertx.core.Starter</Main-Class>
                                    <Main-Verticle>tutorial.diluo.MyFirstVerticle</Main-Verticle>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                        <artifactSet/>
                        <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我通过java -jar xxx-fat.jar运行它.

当我在浏览器中输入 localhost:8080/q1/xxx 时,它可以返回所需的内容.但是当我尝试访问 localhost:8080/q2/xxx 时,它显示找不到资源".你能告诉我如何部署两个路由不同路径的verticles吗?我知道我可以在同一个verticle中路由不同的pathe,我只想知道如何部署和运行多个verticle.提前致谢!

When I type localhost:8080/q1/xxx in the browser, it can return desired content. But when I am trying to visit localhost:8080/q2/xxx, it says "Resource Not Found". Can you tell me how to deploy two verticles that route different paths? I know that I can route different pathe in the same verticle, I just want to know how to deploy and run multiple vertices. Thanks in advance!

推荐答案

您遇到的问题是两个 Verticles 都试图绑定到同一个端口 (8080代码>)这是你不能做的.所以第二个 Verticle 可能会抛出一个 BindException 并且没有出现.然后,第一个 Verticle/q2 处没有资源,这就是您得到 Resource Not Found 的原因.

The problem you have is that both Verticles are attempting to bind to the same port (8080) which you can't do. So the second Verticle is likely throwing a BindException and failing to come up. The first Verticle then, doesn't have a resource at /q2 which is why you're getting the Resource Not Found.

根据 tsegismont 的评论更新:

Updated as per comment from tsegismont:

Vert.x 允许多个 Verticles 在一个名为 服务器共享.当这种情况发生时,Vert.x 将使用循环策略依次向每个 Verticle 发送请求.因此,您应该看到 50% 的请求适用于 /q1,而 50% 的请求适用于 /q2.但是 - 正如 tsegismont 所指出的,您的浏览器使用 持久连接,因此它保持与单个 Verticle 的连接.您应该会发现使用 curl 或其他浏览器可能会给您带来更好的结果.无论哪种方式,这可能都不是您想要的.

Vert.x allows multiple Verticles to start on the same port in a feature known as server sharing. When this happens, Vert.x will use a round robin strategy to send requests to each Verticle in turn. So, you should see 50% of requests working for /q1 and 50% of request for /q2. But - as noted by tsegismont, your browser uses persistent connections so it maintains a connection to a single Verticle. You should find that using curl, or another browser may give you a better result. Either way, this isn't probably what you want.

如果您需要 2 个 Verticles,您应该考虑一下.通常,您希望将 Verticle 视为应用程序的入口点 - 这是一种引导您的应用程序/微服务的方式.

You should have a think if you need 2 Verticles. Generally you want to think of a Verticle as an entry point in to your application - it's a way of bootstrapping your application / microservice.

如果你真的需要 2 个 Verticles 那么你将不得不选择单独的端口或在单独的盒子上运行.如果不这样做,那么只需在同一个 router 上创建 2 个 routes.

If you really need 2 Verticles then you will have to choose separate ports or run on separate boxes. If you don't, then just create 2 routes on the same router.

参见 http://vertx.io/docs/vertx-web/java/ 有关 Vert.x Web

这篇关于如何在 Vert.x 中运行多个顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆