This howto shows all the current required steps and workarounds to build a vert.x native image with graalvm.
-
You will write a multi-verticle application (client + server).
-
The code will cover security (SSL).
-
The code will be compiled to native with the help of GraalVM.
A native-image is a optimized translation of a java application to OS/CPU specific native code. This new application will have different characteristics from a traditional JVM application, the most notable ones are:
-
faster startup times
-
(usually) smaller heap sizes
Native images are therefore very well suited for CLI or Serverless but nothing forbids its use also for server applications.
Native image compilation has several restrictions (by design) of which most do not apply to Vert.x core code. This makes vert.x a very good candidate to write native images. There are however, a couple of known limitations that can be easily worked around to build a native image.
This howto will cover these limitations and explain how to adapt a simple vert.x application to become native.
Start by creating a new project that follows the following structure:
├── pom.xml └── src └── main └── java └── vertx └── APIClientVerticle.java └── HTTPVerticle.java └── HTTPSVerticle.java
After that let’s walk over the important parts of the project. Here is the content of the pom.xml
file that you should be using:
link:steps/step-1/pom.xml[role=include]
This is a minimal Hello World
project. which you can confirm by reading what the code does:
link:steps/step-2/src/main/java/vertx/HTTPVerticle.java[role=include]
Until this moment there is nothing new. It is a plain vert.x java project.
The same applies for the remaining sources (which will be covered later).
In order to build a native image we should add the native-image-maven-plugin
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>${graal.version}</version> <!--(1)-->
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<imageName>${project.name}</imageName> <!--(2)-->
<mainClass>${vertx.verticle}</mainClass> <!--(3)-->
<buildArgs>-H:+PrintClassInitialization -H:+ReportExceptionStackTraces</buildArgs> <!--(4)-->
</configuration>
</plugin>
-
We include the plugin which should match the installed graalvm version.
-
Specifies the final name of the image (the executable name)
-
The main start class
-
The arguments to pass to the compiler (in this case we want to have some debug information in case things go wrong).
Building the image is now as simple as:
mvn package
However this will fail as there is no public static void main
method in the declared main class. We fix this by either adding it to HTTPVerticle
:
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new HTTPVerticle());
}
Or configure the reflection config of graalvm to not exclude our verticles:
link:steps/step-2/src/main/resources/META-INF/native-image/com.example/myapp/reflect-config.json[role=include]
Adding support to SSL is not difficult but requires some updates. The reason is that security libraries will increase your final binary size considerably so all security features are disabled behind flags. There are also other caveats such as java keystores are allowed BUT must be in PKCS12
which is the new default format since Java9 but not on Java8 (which graalvm is based on).
You will now add an HTTPS vertcle server to your project, create the class HTTPSVerticle
in the vertx
package next to the existing one:
link:steps/step-2/src/main/java/vertx/HTTPSVerticle.java[role=include]
-
Enable HTTPS for this server.
-
Specify where to load the certificates from.
-
What is the keystore password (you shouldn’t do this, you should get the password from a configuration, but for simplicity purposes it is hardcoded).
You need a certificate so this are the required steps:
# Generate the self signed test certificate $ keytool -genkey \ -alias vertx \ -keypass localhost \ -keystore certificates.keystore \ -storepass localhost \ -keyalg RSA # Convert to PCKS12 $ keytool \ -importkeystore \ -srckeystore certificates.keystore \ -destkeystore certificates.keystore \ -deststoretype pkcs12
If you build the image this will compile, but won’t work at runtime. One thing is needed:
-
Enable the security features to be added to the native image
To enable the security feature the graal plugin
must be configured as:
<configuration>
<imageName>${project.name}</imageName>
<mainClass>${vertx.verticle}</mainClass>
<buildArgs>--enable-all-security-services</buildArgs>
</configuration>
Now that we covered basic SSL security we can touch clients. For this example we will consume a simple HTTPS API.
link:steps/step-2/src/main/java/vertx/APIClientVerticle.java[role=include]
This client will make a HTTPS request and print the output on the console.
At this moment you should have a basic application and your pom.xml
should look like:
link:steps/step-2/pom.xml[role=include]
Running mvn clean package
will render a build failure. Currently and because vertx-core still depends on a netty
release older than 4.1.53, we need to import the configuration added upstream manually. Which means we need to add a new file:
├── pom.xml └── src └── main └── resources └── META-INF/native-image/com.example/myapp └── native-image.properties
This file are the configuration needed to be passed to the compiler. Currently the file is quite big but again once vertx core uses the latest netty many of these entries can be removed and only list the vert.x specific ones:
link:steps/step-2/src/main/resources/META-INF/native-image/com.example/myapp/native-image.properties[role=include]
Running a compilation will be:
$ mvn package [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.example:myapp >-------------------------- [INFO] Building hello_native 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- vertx-maven-plugin:1.0.22:initialize (vmp) @ myapp --- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 4 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 3 source files to /home/paulo/Projects/vertx-howtos/graal-native-image-howto/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /home/paulo/Projects/vertx-howtos/graal-native-image-howto/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myapp --- [INFO] No tests to run. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myapp --- [INFO] Building jar: /home/paulo/Projects/vertx-howtos/graal-native-image-howto//target/myapp-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- native-image-maven-plugin:20.3.0:native-image (default) @ myapp --- [INFO] ImageClasspath Entry: ... [INFO] Executing: ... [hello_native:59425] classlist: 2,138.46 ms, 0.94 GB [hello_native:59425] (cap): 541.55 ms, 0.94 GB [hello_native:59425] setup: 1,947.31 ms, 0.94 GB [hello_native:59425] (clinit): 1,033.70 ms, 3.20 GB [hello_native:59425] (typeflow): 20,114.89 ms, 3.20 GB [hello_native:59425] (objects): 19,071.97 ms, 3.20 GB [hello_native:59425] (features): 1,535.82 ms, 3.20 GB [hello_native:59425] analysis: 43,375.18 ms, 3.20 GB [hello_native:59425] universe: 1,988.67 ms, 3.20 GB [hello_native:59425] (parse): 6,534.22 ms, 4.99 GB [hello_native:59425] (inline): 12,237.05 ms, 5.93 GB [hello_native:59425] (compile): 47,588.24 ms, 5.90 GB [hello_native:59425] compile: 69,333.28 ms, 5.90 GB [hello_native:59425] image: 4,784.16 ms, 5.90 GB [hello_native:59425] write: 819.88 ms, 5.90 GB [hello_native:59425] [total]: 124,619.09 ms, 5.90 GB [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:07 min [INFO] Finished at: 2020-12-02T19:49:37+01:00 [INFO] ------------------------------------------------------------------------
Running the application will be as simple as:
$ ./target/hello_native run vertx.APIClientVerticle Got HTTP response with status 200 with data Two peanuts were walking down the street. One was a salted. ^C $ ./target/hello_native run vertx.HTTPServer Dec 02, 2020 7:51:56 PM io.netty.channel.DefaultChannelId defaultProcessId WARNING: Failed to find the current process ID from ''; using a random value: 1891971538 Dec 02, 2020 7:51:56 PM io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer INFO: Succeeded in deploying verticle Server listening on http://localhost:8080/ ^C $ ./target/hello_native run vertx.HTTPSServer Dec 02, 2020 7:56:25 PM io.netty.channel.DefaultChannelId defaultProcessId WARNING: Failed to find the current process ID from ''; using a random value: -1125265761 Dec 02, 2020 7:56:25 PM io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer INFO: Succeeded in deploying verticle Server listening on https://localhost:8443/ ^C
Note
|
If you’re interested in knowing the RAM usage just execute: ps x -o pid,rss,command|grep hello_native
and you will observe something like: 59751 35484 ./target/hello_native run vertx.HTTPVerticle About 36MB
|
You have now all the verticles in the same image. Remember that you can do now all the things you would with the launcher, for example scale the number of verticles for the HTTP server:
$ ./target/hello_native run vertx.HTTPVerticle -instances 4 Server listening on http://localhost:8080/ Server listening on http://localhost:8080/ Server listening on http://localhost:8080/ Server listening on http://localhost:8080/ ^C
And this concludes the native image howto.
The full source of this howto can be found here
-
We wrote a HTTP server verticle.
-
We added the required metadata to build a image.
-
We wrote a HTTPS server verticle.
-
We added the required security dll and configuration to build a image.
-
We wrote a HTTPS client verticle.
-
We handled a build issue by specifying the order of load of classes.
-
We configured reflection and resources to have multiple verticles in a single image.