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

Uday Tatiraju
Sep 10, 2018 · 5 min read
Image for post
Image for post
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 Groundbreakers

Aggregation of articles from Oracle engineers…

Uday Tatiraju

Written by

Technologist, ML Practitioner, and Age of Empires champion. NOTE: The views expressed here are my own and do not represent those of my employer.

Oracle Groundbreakers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, ACEs, and the developer community on all things Oracle Cloud and its technologies. The views expressed are those of the authors and not necessarily those of Oracle. Contact @jimgris or @brhubart

Uday Tatiraju

Written by

Technologist, ML Practitioner, and Age of Empires champion. NOTE: The views expressed here are my own and do not represent those of my employer.

Oracle Groundbreakers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, ACEs, and the developer community on all things Oracle Cloud and its technologies. The views expressed are those of the authors and not necessarily those of Oracle. Contact @jimgris or @brhubart

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store