No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download

README.adoc

This project shows how to build "micro" applications with Spring and Spring Boot - the smallest, functional units, with the fewest dependencies and fastest start times

Building the Apps

Build the jar:

$ ./mvnw clean install

Run it

$ java -jar target/micro-0.0.1-SNAPSHOT.jar
...
2018-05-23 10:03:41.776 [main] INFO  com.example.demo.DemoApplication - Started DemoApplication in 1.885 seconds (JVM running for 3.769)
...

The default app is relatively full-bodied - it uses Spring Boot and auto-configuration.

You can run the smaller apps using command line flags:

$ java -jar target/micro-0.0.1-SNAPSHOT.jar --thin.main=com.example.mini.MiniApplication
...
May 23, 2018 10:05:41 AM reactor.util.Loggers$Slf4JLogger info
INFO: Started HttpServer on /127.0.0.1:8080

The main class could be one of

Main

Description

com.example.demo.DemoApplication

(The default). Spring Boot with Webflux and full autoconfiguration

com.example.boot.BootApplication

Spring Boot with Webflux and RouterFunction but no autoconfiguration

com.example.func.BuncApplication

Spring Boot with Webflux and RouterFunction using manual functional bean registration

com.example.mini.MiniApplication

Raw Spring, no autoconfiguration, but using @Configuration and RouterFunction

com.example.micro.MicroApplication

Raw Spring with no @Configuration

The thin.profile could be empty (the default) for just spring-boot-starter-webflux dependencies, or you can try jack for a more "normal" set of dependencies, e.g. with hibernate-validator, jackson, and logback. Other choices add more dependencies (e.g. actr for spring-boot-starter-actuator). You will need the "jack" profile to use actuators, so --thin.profile=actr,jack.

The apps are already systematically benchmarked here with regular JVMs: https://github.com/dsyer/spring-boot-startup-bench/blob/master/flux/README.adoc. The point of this project is just to extract them out of the JMH benchmarks, and make them easily executable as standalone apps. In the benchmarks, the "jack" profile is the default, and "jlog" is the faster reduced set of dependencies.

Building a Native Image

The MicroApplication ought to be convertible to a native binary image using the Graal tools (the others, maybe not so easy because they all use Spring more heavily). The netty reflection configuration we need is probably the same as the blog, so we copied that over (hence the extra jar that needs to be installed above).

Download GraalVM and install it (add the bin dir to you path and set the JAVA_HOME to point to GraalVM). You can also use sdkman to download it. You will need GraalVM 1.0.0-rc5 or later.

Compute the classpath using the graal profile so that we don’t crash on missing JSR330 and jzlib classes:

$ CP=`java -jar target/micro-0.0.1-SNAPSHOT.jar --thin.classpath --thin.profile=graal`

Then build an image:

$ native-image -Dio.netty.noUnsafe=true -H:Name=target/micro -H:ReflectionConfigurationFiles=micro.json --report-unsupported-elements-at-runtime -cp $CP com.example.micro.MicroApplication

Then run it:

$ ./target/micro
Started HttpServer: 2ms

Excellent!

You can do the same with BuncApplication (full Spring Boot, but manually configured):

$ native-image -Dio.netty.noUnsafe=true -H:Name=target/bunc -H:ReflectionConfigurationFiles=func.json --report-unsupported-elements-at-runtime -cp $CP com.example.func.BuncApplication
$ ./target/bunc

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::

Aug 07, 2018 11:25:13 AM org.springframework.boot.StartupInfoLogger logStarting
INFO: Starting application on tower with PID 11253 (started by dsyer in /home/dsyer/dev/demo/workspace/micro)
Aug 07, 2018 11:25:13 AM org.springframework.boot.SpringApplication logStartupProfileInfo
INFO: No active profile set, falling back to default profiles: default
Aug 07, 2018 11:25:13 AM org.springframework.boot.web.embedded.netty.NettyWebServer start
INFO: Netty started on port(s): 8080
Aug 07, 2018 11:25:13 AM org.springframework.boot.StartupInfoLogger logStarted
INFO: Started application in 0.036 seconds (JVM running for 0.04)
Benchmark app started
Started HttpServer: 40ms