Microservices: from CRUD to Native Image. Part two

Today I will continue to talk about how we write microservices. The last time the emphasis was on the theory: it was necessary to recall how the code was written before, to understand the essence of architecture and communication applications. 





This half of the article will focus on the experience of our BellSoft team. Let's talk about how we interact with the world of microservices: here will be about the universal Java runtime, and about tiny containers, and about Spring. I will decompose the microservice into layers, assemble it into an image, run it and show what affects its speed.





Remember the result

We easily use tiny pieces of code to interconnect remote systems such as different microservices, data stores, or message queues. And to keep in touch with clients using common protocols.





@RestController
public class HelloController {

   @Autowired
   private WebClient webClient;

   @RequestMapping(path = "/", method = RequestMethod.GET)
   public CompletableFuture<String> greet(Principal principal) {
       return webClient.get()
        .uri("http://api/persons/{id}", principal.getName())
        .accept(MediaType.APPLICATION_JSON)
        .exchange()
        .flatMap(response -> response.bodyToMono(Person.class))
        .map(person -> "Hello, " + person.getFirstName())
        .toFuture();
   }

}
      
      



In this example, the ID obtained after authentication is used to request information about a person from another service and respond with a greeting. Reactive code is assumed to run asynchronously. The chosen framework, along with additional components (for example, the service registry), adds service address discovery, load balancing, etc.





The role of the web server

. — , HTTP; - . , (), , . -. - , . , , , , Undertow Spring Embedded Tomcat.





Serverless, , -. . «» - (FaaS).





JVM

JVM -, -. , , , , . - , , Mission Control.





. . .





Java (facade), API java.lang.reflect.Proxy java.lang.reflect.InvocationHandler. Dynamic Proxy, CDI-.





JIT- . , . : , , — .





JRE -. , , . , ; JVM. JVM, - ( ), . : 





  • . — JDK 11–16 , latency, 40%, GC! 





  • .





  • TLS 1.3.





  • JFR, .





record’ Project Loom . , State of Loom .





, , -, . -, , ( !) - ( NIO). - , . (, , ) , . Loom, , .





, , . . , .





, , . , Eclipse MicroProfile Java EE, . , API. Spring, Quarkus CDI, . — Spring Quarkus, .





, : JVM, -, , . , . , - Docker Podman.





. , . , .





: « OS Packages JRE?» OS Packages — , , JRE. , , . DevOps - YAML, - :





server:
  port : 8081
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

      
      



, , Kubernetes Marathon, . , , , SaaS- .





. : ?





, :





  1. Docker-,





  2. ,





  3. .





— . . BellSoft Liberica JDK 107 . ( CLI- 41,5 Alpine Linux musl — Java.)





, , Liberica JDK Spring Boot. ,





gradle bootBuildImage
      
      



. « » , . , Spring Boot Actuator. thin jar AppCDS 2,5 ! .





— . ( ) . Native Image.





Native Image

Native Image 35 1/10 ! Native Image 89 . 





Graal AOT Substrate VM , , , , . , : .





gradlew nativeImage
      
      



...

Native Image JDK, . «scratch»- . , . , , Native Image C.





Native Image , SSL C. distroless, « ». gcr distroless images libc 2 (base). , Alpine Linux, glibc — 17 .





Native Image, thin jar fat jar:













RAM









thin jar unoptimized





13 kb thin jar + 17,4 MB libs + 107 MB





135 MB 





2,197





thin jar optimized





13 kb jar + 17,4 MB libs + 107 MB +





50 MB jsa (CDS archive)





70 MB





1,156





fat jar unoptimized





18,02 MB jar + 107 MB





135 MB





3,811





Native Image





89,22 MB





35 MB





0,111





, ? , -, - ( ). -: , ?





Java- Native Image , «» JVM. , .





Native Image, - JDK, . , fallback image, . .





  1. , . , fallback image. , , , .





  2. , closed-world. , invokedynamic .





  3. , Native Image , JVM. , Unsafe.





Native Image Compatibility and Optimization Guide.





, Native Image — , . . Maven Gradle Tracing Agent, JVM.





GraalVM EE, . thin jar, , , jar-, . Native Image , . , .





Native Image ?

, . : Native Image, — JVM. .





Native Image , , : Quarkus, Micronaut, Helidon. (, -), , .





2020 Java- Spring Boot Native Image. , . , GraalVM .





, : . JVM , (AppCDS), (Checkpoint/Restore), 50





The tools available for building Java microservices are varied and constantly evolving. Also, the language itself and its support in the IDE are evolving. At the virtual machine level, changes are being made right now that will help not only future developments, but also existing code.





Among all these processes, the JVM continues to be central to building software solutions. When choosing among the great alternatives in the Java world, you just need to be clear about the challenges and constraints. And you can always ask the opinion of professionals. May everything work out this year, no matter what method of launching applications you choose, and less to all hs_err.








All Articles