第 3 章 开发和部署 Eclipse Vert.x 运行时应用程序


您可以创建一个新的 Eclipse Vert.x 应用程序,并将其部署到 OpenShift 或独立 Red Hat Enterprise Linux。

3.1. 开发 Eclipse Vert.x 应用程序

对于基本的 Eclipse Vert.x 应用程序,您需要创建以下内容:

  • 包含 Eclipse Vert.x 方法的 Java 类。
  • 包含 Maven 构建应用程序所需的信息的 pom.xml 文件。

以下流程创建了一个简单的 Greeting 应用程序,它将返回 "Greetings!" 作为回答。

注意

要将应用程序构建和部署到 OpenShift,Eclipse Vert.x 4.3 仅支持基于 OpenJDK 8 和 OpenJDK 11 的构建器镜像。不支持 Oracle JDK 和 OpenJDK 9 构建器镜像。

前提条件

  • 安装了 OpenJDK 8 或 OpenJDK 11。
  • 已安装 Maven。

流程

  1. 创建新目录 myApp,并导航到它。

    $ mkdir myApp
    $ cd myApp
    Copy to Clipboard Toggle word wrap

    这是应用的根目录。

  2. 在根目录中创建目录结构 src/main/java/com/example/,然后导航到.

    $ mkdir -p src/main/java/com/example/
    $ cd src/main/java/com/example/
    Copy to Clipboard Toggle word wrap
  3. 创建包含应用代码的 Java 类文件 MyApp.java

    package com.example;
    
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.Promise;
    
    public class MyApp extends AbstractVerticle {
    
        @Override
        public void start(Promise<Void> promise) {
            vertx
                .createHttpServer()
                .requestHandler(r ->
                    r.response().end("Greetings!"))
                .listen(8080, result -> {
                    if (result.succeeded()) {
                        promise.complete();
                    } else {
                        promise.fail(result.cause());
                    }
                });
        }
    }
    Copy to Clipboard Toggle word wrap
  4. 在应用程序根目录 myApp 中创建 pom.xml 文件,其内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.example</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>My Application</name>
      <description>Example application using Vert.x</description>
    
      <properties>
        <vertx.version>4.3.7.redhat-00002</vertx.version>
        <vertx-maven-plugin.version>1.0.24</vertx-maven-plugin.version>
        <vertx.verticle>com.example.MyApp</vertx.verticle>
    
        <!-- Specify the JDK builder image used to build your application. -->
        <jkube.generator.from>registry.access.redhat.com/ubi8/openjdk-11</jkube.generator.from>
    
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      </properties>
    
      <!-- Import dependencies from the Vert.x BOM. -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-dependencies</artifactId>
            <version>${vertx.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <!-- Specify the Vert.x artifacts that your application depends on. -->
      <dependencies>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-core</artifactId>
        </dependency>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-web</artifactId>
        </dependency>
      </dependencies>
    
      <!-- Specify the repositories containing Vert.x artifacts. -->
      <repositories>
        <repository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </repository>
      </repositories>
    
      <!-- Specify the repositories containing the plugins used to execute the build of your application. -->
      <pluginRepositories>
        <pluginRepository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </pluginRepository>
      </pluginRepositories>
    
      <!-- Configure your application to be packaged using the Vert.x Maven Plugin. -->
      <build>
        <plugins>
          <plugin>
            <groupId>io.reactiverse</groupId>
            <artifactId>vertx-maven-plugin</artifactId>
            <version>${vertx-maven-plugin.version}</version>
            <executions>
              <execution>
                <id>vmp</id>
                <goals>
                  <goal>initialize</goal>
                  <goal>package</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    Copy to Clipboard Toggle word wrap
  5. 从应用的根目录使用 Maven 构建应用。

    $ mvn vertx:run
    Copy to Clipboard Toggle word wrap
  6. 验证应用是否正在运行。

    使用 curl 或您的浏览器,验证您的应用程序正在 http://localhost:8080 中运行。

    $ curl http://localhost:8080
    Greetings!
    Copy to Clipboard Toggle word wrap

附加信息

  • 作为建议的做法,您可以配置存活度和就绪度探测,以便在 OpenShift 上运行时为应用启用健康状态监控。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat