A 30MB native image with Helidon to run REST based microservices

Uday Tatiraju
Sep 10, 2018 · 5 min read
plugins {
id "java"
id "com.zyxist.chainsaw" version "0.3.1"
}
sourceCompatibility = "10"dependencies {
compile "io.helidon.webserver:helidon-webserver:0.9.1"
compile "io.helidon.webserver:helidon-webserver-netty:0.9.1"
compile "io.helidon.webserver:helidon-webserver-json:0.9.1"
}
module example.api {
requires io.helidon.webserver;
requires io.helidon.webserver.json;
requires org.glassfish.java.json;
}
import io.helidon.webserver.Http;
import io.helidon.webserver.Routing;
import io.helidon.webserver.WebServer;
...
public class Launcher {

public static void main(String[] args) {
WebServer
.create(createRouting())
.start()
.thenAccept(ws ->
logger.info("Service running at: http://localhost:" + ws.port()));
}

private static Routing createRouting() {
return Routing.builder()
// Add JSON support to all end-points
.register(JsonSupport.get())
.register("/api", new UserService())
// Global exception handler
.error(Exception.class, (req, res, ex) -> {
res.status(Http.Status.BAD_REQUEST_400).send();
})
.build();
}
}
curl -X GET http://localhost:8080/api/users/
{"items":[]}
jdeps --module-path . -s user-crud-microservice.jar
example.user.api -> io.helidon.webserver
example.user.api -> io.helidon.webserver.json
example.user.api -> java.base
example.user.api -> java.logging
example.user.api -> org.glassfish.java.json
# Add jdk.unsupported to allow netty to access internal classes
jlink --module-path "${JAVA_HOME}/jmods" \
--add-modules jdk.unsupported"${JAVA_BASE_MODS}" \
--strip-debug \
--compress 2 \
--no-header-files \
--no-man-pages \
--output myimage
myimage/bin/java --upgrade-module-path ${DEPLOY_DIR}/mods -m example.user.api/example.user.api.Launcher

Oracle Developers

Aggregation of articles from Oracle & partners engineers, Groundbreaker ambassadors, and the developer community on all things Oracle Cloud and its technologies. The views expressed are those of authors solely and do not necessarily reflect Oracle's. Contact @ypoirier

Uday Tatiraju

Written by

technologist, deep learning enthusiast, age of empires champion

Oracle Developers

Aggregation of articles from Oracle & partners engineers, Groundbreaker ambassadors, and the developer community on all things Oracle Cloud and its technologies. The views expressed are those of authors solely and do not necessarily reflect Oracle's. Contact @ypoirier