且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Vertx入门篇-03 - Vertx deployVerticle - Java

更新时间:2022-08-14 16:29:00

1、添加POM相关依赖

   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.vertx</groupId>
                <artifactId>vertx-stack-depchain</artifactId>
                <version>3.7.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-lang-js</artifactId>
        </dependency>
    </dependencies>

2、新建MainVerticle.java

package io.vertx.demo;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

public class MainVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> startFuture) throws Exception {
            System.out.println("Java Configuration: " + config().getString("name"));

            vertx.createHttpServer().requestHandler(req -> {
            req.response()
                    .putHeader("content-type", "text/plain")
                    .end("Hello from Vert.x (Java) !");
        }).listen(8080, http -> {
            if (http.succeeded()) {
                startFuture.complete();
                System.out.println("HTTP server started on http://localhost:8080");
            } else {
                startFuture.fail(http.cause());
            }
        });
    }
}

3、新建mainVerticle.js
路径:verticles/mainVerticle.js


console.log("JS Configuration: " + Vertx.currentContext().config().name);

var server = vertx.createHttpServer();

server.requestHandler(function (request) {
    request.response().end("Hello from Vert.x (JS) !");
});

server.listen(8090);

4、新建DeployVerticle.java

package io.vertx.demo;

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

public class DeployVerticle {
    public static void main(String[] args) {
        JsonObject config = new JsonObject().put("name", "tim").put("directory", "/blah");
        DeploymentOptions options = new DeploymentOptions().setConfig(config);

        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle("io.vertx.demo.MainVerticle", options, res -> {
            if (res.succeeded()) {
                System.out.println("Deployment id is: " + res.result());
            } else {
                System.out.println("Deployment failed!");
            }
        });
        // Deploy a JavaScript verticle
        vertx.deployVerticle("verticles/mainVerticle.js", options, res -> {
            if (res.succeeded()) {
                System.out.println("Deployment id is: " + res.result());
            } else {
                System.out.println("Deployment failed!");
            }
        });
    }
}

5、控制台输出

Java Configuration: tim
HTTP server started on http://localhost:8080
Deployment id is: 7e909b79-6bce-46d5-970d-380edc399041
JS Configuration: tim
Deployment id is: c139a715-4a14-4239-aca0-75746166cdde

6、访问测试

$ curl -s http://localhost:8090
Hello from Vert.x (JS) !
$ curl -s http://localhost:8080
Hello from Vert.x (Java) !