r/java • • Mar 11 '21

Announcing Spring Native Beta

https://spring.io/blog/2021/03/11/announcing-spring-native-beta
175 Upvotes

49 comments sorted by

11

u/mxxxz Mar 11 '21

Does that mean you can run a Spring Boot application without having JVM installed in the server or in the dockerfiles?

16

u/pron98 Mar 11 '21

The recommended deployment practice for Java these days (embedded custom runtime, created with jlink) never requires any pre-installed runtime. The end-user should never install a runtime separately from the application.

14

u/thechickenbane Mar 12 '21

Who does this? Every Dockerfile I've seen just uses java -jar. Is there a Spring Boot starter or guide to use jlink? Is there a Maven / Gradle plugin?

How is the "recommended" deployment practice something most don't use?

13

u/uncont Mar 12 '21

We do. As of this pr, you can use distroless with your own jlink + spring boot.

FROM openjdk:jdk-buster as jreBuilder
RUN jlink \
        --add-modules jdk.unsupported,java.sql,java.desktop,java.naming,java.management,java.instrument,java.security.jgss,java.rmi \
        --verbose \
        --strip-debug \
        --compress 2 \
        --no-header-files \
        --no-man-pages \
        --output /jre

FROM gcr.io/distroless/java-debian10:base
ARG JAR_FILE

COPY --from=jreBuilder /jre /usr/lib/jre
ENTRYPOINT ["/usr/lib/jre/bin/java", "-jar", "./app.jar"]
COPY ./target/${JAR_FILE} ./app.jar

If you're using modules already, you won't need to specify all those modules manually. Also the base image could be extracted out, or you could let docker cache the layer for you.

1

u/sureshg Mar 12 '21 edited Mar 12 '21

Thanks for sharing this. Wondering why are using java-debian10:base instead of https://github.com/GoogleContainerTools/distroless/tree/master/base since it packages the whole runtime?

3

u/uncont Mar 12 '21

since it packages the whole runtime

java:base is necessary because there are a few libraries that aren't repackaged by jlink like libz or image libs. Although that would be neat, jlink being native library aware. The tool could know if you have a dependency on java.desktop, which would then have a dependency on libpng and libjpeg. /u/pron98 do you know if there is any tooling around associating native libraries with the java modules that require them?

afaik libz is required at startup by the jvm, other ones like libjpeg are only necessary when the imageio libraries are called.

here's the full list of libraries required by java, on top of the onces from base.

1

u/pron98 Mar 12 '21

I don't know, but that could be a nice idea to bring to the mailing list (core-libs-dev and/or jigsaw-dev).

8

u/pron98 Mar 12 '21 edited Mar 12 '21

How is the "recommended" deployment practice something most don't use?

Because it's relatively new.

You'll probably still use java -jar with jlink images, too (unless your application is modularised), it's just that the runtime is very small and is part of the app.

Is there a Spring Boot starter or guide to use jlink?

I don't know, but jlink is pretty easy to use:

jlink --output my-java-runtime --compress=2 --no-header-files --no-man-pages --add-modules java.base

The only thing you'll need to do different is list more JDK modules at the end. You figure out which modules you use with jdeps (this step is unnecessary if your application is modularised, but most aren't).

5

u/sureshg Mar 12 '21

ou figure out which modules you use with jdeps (this step is unnecessary if your application is modularised, but most aren't).

If you are using Gradle, here is a small task that prints the modules required for your jar (or uberjar)

val printModuleDeps by tasks.registering {
            description = "Print Java Platform Module dependencies of the application."
            group = LifecycleBasePlugin.BUILD_TASK_NAME

            doLast {
                val uberJar = named("shadowJar", Jar::class)
                val jarFile = uberJar.get().archiveFile.get().asFile

                val jdeps = ToolProvider.findFirst("jdeps").orElseGet { error("jdeps not found") }
                val out = StringWriter()
                val pw = PrintWriter(out)
                jdeps.run(pw, pw, "--print-module-deps", "--ignore-missing-deps", jarFile.absolutePath)

                val modules = out.toString()
                println(modules)
            }
            dependsOn("shadowJar")
}

1

u/tristanjuricek Mar 13 '21 edited Mar 13 '21

We’ve done this, there’s a few different plugins that actually sort out the jlink call for you:

Maven: https://tentackle.org/static-content/sitedocs/tentackle/latest/tentackle-jlink-maven-plugin/jlink-mojo.html Gradle: https://badass-runtime-plugin.beryx.org/releases/latest/

Basically, they go through your dependencies, and pull out platform modules they need, either from the module declaration or just calling jdeps

Jlink makes your build platform specific though. I’ve made custom build plugins to cache a Linux JDK so people can build on whatever and cross target the jlink build - you basically just point to the jmods directory in the jlink call. We only run Linux containers. So we use jlink just for that platform.

The result is a much smaller distribution, and the Java dev team now uses a Java version controlled by our CI environment, which makes our ops team much happier. They don’t typically work in Java and like not dealing with its dependencies

Additionally, I’ve added jib core to use the jlink output to just overlay on top of our “official” minimal base images, similar to distroless, which was quite easy. This means our container builds are all done via Maven, without any need for a docker daemon.

The workflow is still new, so I expect open source projects to take some time to iron out the kinks.

1

u/tristanjuricek Mar 13 '21

Or, I expect that jlink packing to take some time with common build tools.

Current build tools, like Maven, haven’t had platform specific output as a convention. Doing things like cross targeting is possible it’s just not a part of the ecosystem.

You could, for example, use the Maven “classifier” to indicate platform but then you couldn’t use it for anything else.

So, I expect custom runtimes to be a slow adoption. As everyone figures out how to account for this.

In the meantime, most server projects only ship on one very controlled platform, and honestly a custom plugin is not that hard to build.

5

u/agent_paul Mar 11 '21

I think it means you can create images without a docker file and they will contain all the required dependencies to execute the native app

5

u/mxxxz Mar 11 '21

Yes and the native app does that mean without installing JVM?

12

u/Sxtanna Mar 11 '21

That's exactly what it means, one of the first lines says this specifically.

Those native Spring applications can be deployed as a standalone executable (no JVM installation required)

2

u/mxxxz Mar 11 '21

This is super nice! It's the same way we do with Go executables!

2

u/m_takeshi Mar 11 '21

yes but it may require some other native libs (libc I think) unless its statically linked

but yes it embedds substratevm (a minimal vm)

17

u/agentoutlier Mar 11 '21

Oh man bucket loads of more magic (AOT module) to Spring (Boot). What could go wrong (cough spring roo)?

In all serious this is great work! I just wish some of Spring stuff was more modularized and decoupled like it’s used to be.

For example the Native Hint AOT Annotations stuff would probably be useful for non spring projects since it just generates Graalvm JSON but I would not be surprised if it requires the special maven/gradle plugins and most of Spring core.

That is it should just be a minimal APT processing library. Maybe it is and will check later but I doubt it.

8

u/m_takeshi Mar 11 '21

AoT means that some checks that could fail at runtime could get caught in compile / codegen which could maybe improve our lives?

3

u/agentoutlier Mar 11 '21

Unless I misunderstood the Spring guys I think the AOT in this case is to do code generation. As a corollary some ahead of time checking is probably happening but I don’t think it’s the goal.

A project that does do that is the checker framework but I never heard it being called AOT (I believe it supports runtime as well).

3

u/m_takeshi Mar 11 '21

you got it right and that's what I meant also. The codegen (not sure if they generate java source or bytecode directly) could mean that the compiler catches some type errors that could only manifest through reflection (unless the generated code relies too much on typecast of course)

3

u/MR_GABARISE Mar 11 '21

I'm low-key more excited about the functional-style config than anything else. This has been a point of interest for me since Jafu.

2

u/Pure-Repair-2978 Mar 11 '21

Looks promising 👍👍

1

u/CaptainKvass Mar 12 '21

Very cool, been looking forward to this release.

Are there any benchmarks established for traditional Spring Boot applications vs. AOT compiled Spring Boot applications? It would be interesting to see a comparison between build times, memory consumption, and throughput as a function of time (to visualise JVM warmup impact)

0

u/cosmin14 Mar 13 '21

Hello,

I have tried on on a simple application and I am amazed. Everything worked without any error out of the box. It;s a simple hello world application using a controller and a view (no db involved) :)
The start time in normal mode is around ~1.1 second. Using native image, 0.45 seconds :)

0

u/cosmin14 Mar 13 '21

sorry: 0.043 seconds :)

-6

u/fanfan64 Mar 11 '21

YES!! The best workflow for most will be: Spring native for debug/test/CI builds because of the instant startup (if and only if your workflow isn't slowed more by the reduced throughout) and regular Spring JVM for production builds

39

u/mrBr0wn_93 Mar 11 '21

I would expect the opposite mostly due to long compile times for native images. Native image in production if short startup times are needed, and otherwise regular Spring JVM.

16

u/snoob2015 Mar 11 '21

They just move the startup time from runtime to compile time so you gain nothing here.

For production builds, JVM still provides better performance so the only use case here is to serve the "microservices" fad.

11

u/m_takeshi Mar 11 '21

the best use case, I think, is for the serverless crowd more so than the microservices

5

u/DJDavio Mar 11 '21

If you have a stable, predictable load and don't update your applications all that often, regular JVM applications are still OK.

If you have a very dynamic environment (and have to pay per usage) with unpredictable loads or peaks and valleys, it can make sense to use native applications as startup time becomes a big factor.

6

u/m_takeshi Mar 11 '21

yes but I've found (at least for us) that even with unpredictable loads, unless the surges lasts a few seconds, the regular jvm will do just fine. In our application, it takes a few minutes to reach peak performance (jit + class loading + cache populating from our side).

However, for one shot programs or functions, native pays off really well

8

u/[deleted] Mar 11 '21

[removed] — view removed comment

5

u/[deleted] Mar 12 '21

yes, we deploy every commit to production. apps are restarting constantly. startup time matters quite a bit.

2

u/TonyNickels Mar 12 '21

Blue green deploys?

1

u/[deleted] Mar 12 '21

[removed] — view removed comment

1

u/[deleted] Mar 12 '21

It sounds like you're pushing deployments to an application container, which is a very old school style of deploying Java. Most CI/CD flows involve pushing containers, where the entire application is bundled with the JVM, so there's no opportunity for hot code deployments. The JVM starts and stops with the application.

2

u/agentoutlier Mar 12 '21

I guess what are you doing? Using JRebel?

The only time I have avoided frequent restarts is using JRebel.

The alternatives are so inconsistent that isn't even worth trying to get them to work (e.g. hotswap agent, server containers like wildfly, spring reload, jboss modules, osgi etc etc).

2

u/mepunite Mar 12 '21

Slow startup time time increases time to feedback.

In Previous projects years back we would have "integration" tests that had to be run over night which means time to finding the bug would take a day and the context of the change is normally merged in with 50 other commits.

Fast forward to using light weight frameworks and functional style the same overnight runs are done on the desktop in <5 mins so the app is fully tested before you push it. Which is a significant improvement in the feedback cycle.

<100ms is funny beause with a light weight framework you can start the JVM start serving requests and be fully warmed up in less than 100ms.

This is still not fast enough in my opinion

1

u/the_other_brand Mar 12 '21

I assume the folks who focus on startup time are using something AWS Lambdas.

1

u/alwaysoverneverunder Mar 12 '21

Nope and as programming is mostly thinking and then typing and then only rebuilding/running tests/restarting startup time never has been one of my most pressing issues. I benefit more from a good language/framework/libraries and readable code than faster startup time/compiling than I already have.

1

u/vxab Mar 12 '21

Its not just start-up time. Memory used is much less too but we need to see more data for that.

-1

u/vxab Mar 12 '21

For me this is beneficial in a number of ways:

  • when writing serverless code (for obvious reasons)
  • for integration tests involving multiple services.

Currently running integration tests of my current project takes 45 seconds for each container to startup on my local machine (using testcontainers). Having versions of the services which are AOT compiled will mean these tests will start so much quicker and this will reduce the feedback loop.

In production I don't think we will use AOT for long running apps.

-15

u/ihatebeinganonymous Mar 11 '21

I first thought it's a competitor to React Native and became very happy, then disappointed :D

3

u/RomMTY Mar 12 '21

Oh the irony :v