Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Microservices with Micronaut

553 views

Published on

Cloud Native Night June 2019, Munich: Talk by Moritz Kammerer (Software Architect at QAware)

Join our Meetup: www.meetup.com/cloud-native-muc

Abstract: Startup-Zeiten von Containern werden bei elastischer Skalierung in der Cloud immer wichtiger – wer möchte schon 2 Minuten warten, bis eine neue Instanz des Services erscheint? Hier kann der Shooting Star von Java-basierten Microservices, Spring Boot, nicht glänzen. Systembedingt steigt die Startzeit des Containers mit der Menge der Klassen auf dem Classpath. Auch der Speicherverbrauch einer Spring Boot Anwendung ist nicht gerade gering.
Micronaut, erschaffen von den Machern des Grails-Frameworks, behauptet, diese Probleme in den Griff zu bekommen. In dem Talk sehen wir uns an, wie sich Microservices mit Micronaut entwickeln lassen und prüfen, ob es die Versprechen hält.
Bonus: Da Micronaut kaum Reflection verwendet, sollte es sich mit dem GraalVM AOT Compiler in ein natives Image kompilieren lassen. In dem Vortrag werden wir dies ausprobieren und die Performance dieses Native Images messen.

Published in: Data & Analytics
  • Login to see the comments

  • Be the first to like this

Microservices with Micronaut

  1. 1. Microservices with Micronaut 2019-06-13 - Moritz Kammerer
  2. 2. What’s the problem?
  3. 3. Why does it take so long? ● Start time of Spring (Boot) ○ Depends on the number of beans in the context ○ Reading of bytecode [3] ○ Creating proxies at runtime ○ Runtime AOP ○ Reflection ○ Annotation Synthesizing [1] [2] ● Memory usage ○ Reflective Metadata Cache ● Debugging ○ Very very long stacktraces ○ … with code in dynamically generated proxy classes [1] https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/AnnotationUtils.html [2] https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/AliasFor.html [3] https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/type/classreading/AbstractRecursiveAnnotationVisitor.java
  4. 4. What’s the (promised) solution? ● Create stuff at compile time ○ Proxies ○ Dependency Injection ○ AOP ● Why? ○ Compiled once, run often ○ Tradeoff: Compile time vs. Runtime ● Goal: ○ No reflection - or as little as possible ○ No proxies ○ Minimize startup time ○ Minimize memory usage ○ Readable stacktraces
  5. 5. And how does that work? ● Annotation Processor ○ Reads bytecode & annotations of the classes ○ Creates new classes ○ Doesn’t touch your code, just extends it ● Gradle ○ annotationProcessor "io.micronaut:micronaut-inject-java" ○ annotationProcessor "io.micronaut:micronaut-validation" ● Also runs with Maven ○ < Insert 500 KB of XML here >
  6. 6. And this is really working? ● Spring Boot 2.1.2 (starter-web), Java 11, @Component on empty class ○ 0 Beans: 1.236 seconds ○ 1000 Beans: 1.808 seconds ○ 2000 Beans: 2.356 seconds ○ 4000 Beans: 3.259 seconds ○ 8000 Beans: 6.498 seconds ○ 16000 Beans: 9.231 seconds ● Micronaut 1.0.4, Java 11, @Singleton on empty class ○ 0 Beans: 1085ms ○ 1000 Beans: 1870ms ○ 2000 Beans: 2757ms ○ 4000 Beans: 4529ms ○ 8000 Beans: 8038ms ○ 16000 Beans: 15861ms
  7. 7. Hmmmm...
  8. 8. Debunked: It’s not the classpath scanning ● Not so expensive: ○ Classpath scanning (Spring) ○ Injection with reflection (Spring) ● Expensive ○ Load classes (Micronaut + Spring) ○ Runtime AOP Proxies (Spring) ○ Annotation Synthesizing (Spring) ○ Read bytecode on big classes (Spring) ○ Auto-Configuration Magic (Spring)
  9. 9. Looks great, so not disadvantages?
  10. 10. The price you pay: Compile time (16k Beans) Spring: [INFO] Total time: 7.958 s Micronaut: BUILD SUCCESSFUL in 5m 3s
  11. 11. Benchmarking is hard! ● This was NOT a real benchmark ● Most time spent in the applications shown on the first slides: Spring autoconfiguration magic ● It really depends on the needed feature of your application ○ Actuator ○ JPA / Hibernate ○ Tracing ○ Metriken ○ @FeignClient ○ @Transactional ○ @Cacheable ○ … ● More in the live demo!
  12. 12. Okay... what else brings Micronaut to the table? ● Dependency Injection ○ @Inject, @Named, @Singleton, @Prototype, ... ● REST Controller ○ @Controller, @GET, @POST, … ● Events ○ @PostConstruct ○ @EventListener ● AOP ○ Around Advice, Introduction Advice, … ● Validation ○ @Validated, @NotNull, ...
  13. 13. ... ● Caching ○ @Cachable, @CacheInvalidate, … ● Retry ○ @Retryable ● Circuit Breaking ○ @CircuitBreaker ● Fallback / Recovery ○ @Recoverable / @Fallback ● Scheduling ○ @Scheduled
  14. 14. ... ● Reactive & Blocking HTTP with Netty ○ Compile Time Route Validation ○ @Error Exception Handling ala Spring ● Websocket Server & Clients ● Server side views ○ Thymeleaf, Handlebars, Velocity ● OpenAPI / Swagger (experimental) ○ Is created on compile time! ● HTTP Clients ○ @Client ○ Client-side load balancing ○ Service discovery (Consul, Eureka, K8S, Route 53, DNS based) ○ OpenTracing (Zipkin, Jaeger)
  15. 15. ... ● Application Configuration ○ application.yml / .json / .groovy ○ Different Environments ○ Property Sources (ENV, System Properties, …) ○ @ConfigurationProperties, @Value ○ Distributed config with Consul, AWS Parameter Store ● Database support ○ JPA, Mongo, Neo4J, Postgres-Reactive, Redis, Cassandra ● Monitoring ○ @Endpoint (/beans, /info, /health, …) ○ Metriken with Micrometer ○ Change log level at runtime
  16. 16. ... ● Security ○ Like Spring Security with users and roles (RBAC) ○ Basic Auth / Session based ○ JWT (JWS and JWE) incl. automatic token propagation ○ Auth with LDAP ● Function-as-a-Service (AWS Lambda, OpenFaaS) ● Message driven services (RabbitMQ / Kafka) ● CLI Applikationen ● Good documentation! ○ e.g. with tutorials for Let’s Encrypt, starting Consul, etc.
  17. 17. Enough bla bla, show code!
  18. 18. Spring vs. Micronaut - GitHub Scraper Micronaut BUILD SUCCESSFUL in 9s Startup completed in 1168ms Spring BUILD SUCCESSFUL in 5s Started DemoApplication in 3.066 seconds (JVM running for 3.505)
  19. 19. Micronaut (JAR: 12 MB) java -Xms16M -Xmx512M -jar github-scraper-0.1-all.jar / openjdk version "1.8.0_201"
  20. 20. Micronaut wrk -t 8 -c 80 -d 30s --latency http://localhost:8080/github/stars Running 30s test @ http://localhost:8080/github/stars 8 threads and 80 connections Thread Stats Avg Stdev Max +/- Stdev Latency 12.75ms 74.53ms 1.50s 97.96% Req/Sec 6.24k 2.70k 15.36k 69.12% Latency Distribution 50% 1.28ms 75% 4.19ms 90% 11.87ms 99% 411.86ms 1460134 requests in 30.03s, 714.10MB read Requests/sec: 48618.64 Transfer/sec: 23.78MB
  21. 21. Spring Boot (JAR: 33 MB) java -Xms16M -Xmx512M -jar github-scraper-spring-0.0.1-SNAPSHOT.jar / openjdk version "1.8.0_201"
  22. 22. Spring Boot wrk -t 8 -c 80 -d 30s --latency http://localhost:8080/github/stars Running 30s test @ http://localhost:8080/github/stars 8 threads and 80 connections Thread Stats Avg Stdev Max +/- Stdev Latency 19.98ms 62.12ms 1.26s 94.35% Req/Sec 3.72k 1.93k 8.64k 58.43% Latency Distribution 50% 1.99ms 75% 11.54ms 90% 50.91ms 99% 329.01ms 865022 requests in 30.06s, 425.81MB read Non-2xx or 3xx responses: 164 Requests/sec: 28779.20 Transfer/sec: 14.17MB
  23. 23. Graal VM “High-performance polyglot VM” (and AOT compiler)
  24. 24. Graal KB
  25. 25. Graal Running 30s test @ http://localhost:8080/test 8 threads and 80 connections Thread Stats Avg Stdev Max +/- Stdev Latency 9.12ms 10.93ms 149.69ms 86.75% Req/Sec 1.66k 301.15 2.80k 68.92% Latency Distribution 50% 4.28ms 75% 11.83ms 90% 23.74ms 99% 50.63ms 396402 requests in 30.02s, 51.79MB read Requests/sec: 13204.09 Transfer/sec: 1.73MB
  26. 26. Experience from the trenches: TASTE-OS Pro: ● Services start faster than Spring Boot ● Smaller JARs ● Tests are faster, as the embedded server starts fast Contra: ● No separation of management and API port ● MDC / ThreadLocal in reactive applications (not Micronaut specific)
  27. 27. Conclusion ● Good documentation ● Recommendation for writing small services ○ I have no experience with bigger services in Micronaut. If you have, please contact me :) ● Development is fun ● If the features are sufficient: Try it! ● GraalVM is incomplete / buggy / not ready for prime-time
  28. 28. Literature ● GitLab Repo: https://github.com/qaware/microservices-with-micronaut ● Micronaut documentation: https://docs.micronaut.io/latest/guide/index.html ● GraalVM: https://www.graalvm.org/
  29. 29. Questions? Contact: moritz.kammerer@qaware.de
  30. 30. We are hiring. In Munich. And in Mainz. https://www.qaware.de/karriere/#jobs

×