Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Touch and Feel of Micronaut

The Touch and Feel of Micronaut

A presentation material for KanJava 1906:
https://kanjava.connpass.com/event/134119/

Mitsuyuki Shiiba

June 20, 2019
Tweet

More Decks by Mitsuyuki Shiiba

Other Decks in Programming

Transcript

  1. The Touch and Feel of Micronaut
    2019/06/20 KanJava June, 2019
    Mitsuyuki Shiiba (@bufferings)
    EC Incubation Development Dept.
    Rakuten, Inc.

    View full-size slide

  2. @bufferings #kanjava
    It reminds me Spring Boot, so it was really easy for me to try.
    Micronaut
    @Controller
    public class Application {
    public static void main(String[] args) {
    Micronaut.run(Application.class);
    }
    @Get("/hello/{name}")
    public String hello(String name) {
    return "Hello " + name;
    }
    }

    View full-size slide

  3. @bufferings #kanjava
    1. Why Micronaut?
    2. The Touch and Feel of Micronaut
    Today's topics

    View full-size slide

  4. @bufferings #kanjava
    Mitsuyuki Shiiba (@bufferings)
    Web Application Engineer
    @Rakuten Osaka from 2010
    Love: Java, Scrum, My wife & daughters

    View full-size slide

  5. @bufferings #kanjava
    1. Why Micronaut?
    2. The Touch and Feel of Micronaut
    Today's topics

    View full-size slide

  6. @bufferings #kanjava
    • Microservices
    • Containers
    • Kubernetes
    • Serverless
    Recent Tech Trends
    Photo by Ilze Lucero on Unsplash

    View full-size slide

  7. @bufferings #kanjava
    • Faster startup time
    • Low memory footprint
    • Effective use of CPU
    It has become matter

    View full-size slide

  8. @bufferings #kanjava
    1. Multiple WARs on one application server
    2. One server for one app with VMs
    3. PaaS, 12 Factor App & Container
    4. Embedded Tomcat & executable JAR by Spring Boot
    5. Kubernetes & Microservices
    Java 10 years around me

    View full-size slide

  9. @bufferings #kanjava
    • Microservices – Communications over network
    • Blocking – Waiting time
    • Want to – Utilize CPU
    • Thread is heavy (Context Switch, Memory)
    • Reactive – RxJava, Spring 5 (Reactor)
    Recent Java - Reactive

    View full-size slide

  10. @bufferings #kanjava
    Currently we're doing a lot at Runtime.
    • Runtime Component Scan
    • Runtime DI & AOP
    It makes our app start slower & consume memory.
    Recent Java - Challenges

    View full-size slide

  11. @bufferings #kanjava
    • Compile-time DI & AOP
    • No reflection
    • No runtime proxy
    Ahead of Time (AOT) Compilation

    View full-size slide

  12. @bufferings #kanjava
    • Microservices in mind from the beginning
    • Inspired by Spring & Grails
    • First class support for Reactive based on Netty
    • Ahead of Time Compilation
    • Support for Java, Kotlin, Groovy
    • Support for GraalVM Native Image
    https://micronaut.io/
    Micronaut

    View full-size slide

  13. @bufferings #kanjava
    • Java code to a standalone executable
    • This executable does not run on the Java VM
    • but includes necessary components from “Substrate VM”
    • The resulting program has faster startup time
    • and lower runtime memory overhead
    https://www.graalvm.org/docs/reference-manual/aot-compilation/
    GraalVM Native Image (early adopter)

    View full-size slide

  14. @bufferings #kanjava
    My thoughts:
    • AOT compilation & Native Image would be popular in a few years
    • Many frameworks would start supporting them including Spring
    • and just I like Micronaut
    My Motivation

    View full-size slide

  15. @bufferings #kanjava
    1. Why Micronaut?
    2. The Touch and Feel of Micronaut
    Today's topics

    View full-size slide

  16. @bufferings #kanjava
    With Micronaut 1.2.0.RC1
    • Micronaut CLI
    • HTTP Server
    • Integration Test
    Demo Code:
    https://github.com/bufferings/kanjava1906
    Demo 1

    View full-size slide

  17. @bufferings #kanjava
    Micronaut CLI
    # Create Application
    $ mn create-app demo
    # Interactive Mode
    $ mn
    | Starting interactive mode...
    | Enter a command name to run. Use TAB for completion:
    mn> create-controller hello

    View full-size slide

  18. @bufferings #kanjava
    HTTP Server
    @Controller("/hello")
    public class HelloController {
    @Get("/{name}")
    public String index(String name) {
    return "Hello " + name;
    }
    }

    View full-size slide

  19. @bufferings #kanjava
    Integration Test
    @Inject
    @Client("/")
    RxHttpClient client;
    @Test
    public void testIndex() throws Exception {
    assertEquals("Hello World",
    client.toBlocking().retrieve("/hello/World"));
    }

    View full-size slide

  20. @bufferings #kanjava
    • HTTP Server (Reactive)
    • Valiadation (AOP)
    • DI
    Demo 2

    View full-size slide

  21. @bufferings #kanjava
    HTTP Server (Reactive)
    @Controller("/hello")
    public class HelloController {
    @Get("/{name}")
    public Single index(String name) {
    return Single.just("Hello " + name);
    }
    }

    View full-size slide

  22. @bufferings #kanjava
    Validation (AOP)
    @Controller("/hello")
    public class HelloController {
    @Get("/{name}")
    public Single index(@Size(min = 10) String name) {
    return Single.just("Hello " + name);
    }
    }
    "message": "name: size must be between 10 and 2147483647"

    View full-size slide

  23. @bufferings #kanjava
    DI – Bean Definition
    @Singleton
    public class GreetingService {
    public String greeting(String name) {
    return "Hello " + name;
    }
    }

    View full-size slide

  24. @bufferings #kanjava
    DI – Constructor Injection
    @Controller("/hello")
    public class HelloController {
    private final GreetingService greetingService;
    HelloController(GreetingService greetingService) {
    this.greetingService = greetingService;
    }

    View full-size slide

  25. @bufferings #kanjava
    • Postgres Reactive
    • Declarative HTTP Client
    • Use Declarative HTTP Client for test
    Demo 3

    View full-size slide

  26. @bufferings #kanjava
    Postgres Reactive
    public Single getScreenName(String name) {
    return client.rxPreparedQuery(
    "SELECT screen_name FROM speakers WHERE name=$1",
    Tuple.of(name)
    ).map(rows -> {
    PgIterator it = rows.iterator();
    if (!it.hasNext()) return name;
    return it.next().getString("screen_name");
    });
    }

    View full-size slide

  27. @bufferings #kanjava
    Declarative HTTP Client
    @Client("http://localhost:8081/")
    public interface ScreenNameClient {
    @Get("{name}")
    Single getScreenName(String name);
    }

    View full-size slide

  28. @bufferings #kanjava
    Use Declarative HTTP Client for test
    @Inject
    HelloClient client;
    @Test
    public void testHello() {
    assertEquals("Hello World", client.hello("World"));
    }

    View full-size slide

  29. @bufferings #kanjava
    Compare Blocking & Reactive Controller
    Demo 4

    View full-size slide

  30. @bufferings #kanjava
    GraalVM Native Image
    https://github.com/bufferings/micronaut-petclinic
    Demo 5

    View full-size slide

  31. @bufferings #kanjava
    Microservices in mind
    • HTTP Server, HTTP Client, Reactive based on Netty
    Fast startup & low memory footprint
    • DI & AOP with AOT Compilation
    GraalVM Native Image Support
    • makes our app start in a few hundreds of milliseconds
    Summary

    View full-size slide

  32. @bufferings #kanjava
    Introduction to Micronaut: Lightweight Microservices with Ahead of
    Time Compilation by Graeme Rocher
    https://youtu.be/P1qp_l5EFic
    Micronaut | Technology Radar | ThoughtWorks
    https://www.thoughtworks.com/radar/languages-and-
    frameworks/micronaut
    References

    View full-size slide