On March 7, RedHat (soon - IBM) introduced a new framework - Quarkus . According to the developers, this framework is based on GraalVM and OpenJDK HotSpot and is intended for Kubernetes. The Quarkus stack includes: JPA / Hibernate, JAX-RS / RESTEasy, Eclipse Vert.x, Netty, Apache Camel, Kafka, Prometheus and others.
The goal of the creation is to make Java the leading platform for deploying Kubernetes and developing serverless applications, providing developers with a unified approach to development in both reactive and imperative styles.
If you look at this classification of frameworks, then Quarkus is somewhere between the "Aggregators / Code Generators" and the "High-level fullstack frameworks". This is already more than an aggregator, but it does not reach the full stack, since Sharpened on the development of backend.
It promises a very high speed of launching the application and low memory consumption. Here is the data from the developer site:
Time from start to first response (s):
Configuration | REST | REST + JPA |
---|---|---|
Quarkus + GraalVM | 0.014 | 0.055 |
Quarkus + OpenJDK | 0.75 | 2.5 |
Traditional Cloud Native Stack * | 4.3 | 9.5 |
Memory consumption (Mb):
Configuration | REST | REST + JPA |
---|---|---|
Quarkus + GraalVM | 13 | 35 |
Quarkus + OpenJDK | 74 | 130 |
Traditional Cloud Native Stack * | 140 | 218 |
Impressive, isn't it?
* I did not find information about this technology stack, it can be assumed that this is some kind of Spring Boot with an additional body kit .
The simplest application written in Quarkus will look like this:
@Path("/hello") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "hello"; } }
This is literally one class and enough of it! You can run an application using Maven in development mode:
mvn compile quarkus:dev … $ curl http://localhost:8080/hello hello
The difference from the usual application - no class Application! Quarkus supports hot reload, so you can change the application without restarting it, thereby developing even faster.
What's next? You can add a service to the controller using the Inject annotation. Service Code:
@ApplicationScoped public class GreetingService { public String greeting(String name) { return "Hello " + name + "!"; } }
Controller:
@Path("/hello") public class GreetingResource { @Inject GreetingService service; @GET @Produces(MediaType.TEXT_PLAIN) @Path("/{name}") public String greeting(@PathParam("name") String name) { return service.greeting(name); } }
$ curl http://localhost:8080/hello/developer Hello developer!
Note that Quarkus uses standard annotations from familiar frameworks - CDI and JAX-RS. You do not need to learn anything new if you have worked with CDI and JAX-RS before, of course.
Used with Hibernate and standard JPA annotations for entities. As in the case of REST controllers, you need to write a minimum of code. Just specify the dependencies in the assembly file, place the @Entity
annotations and configure the datasource in the application.properties.
Everything. No sessionFactory, persistence.xml and other service files. We write only the code that is needed. However, if necessary, you can create a persistence.xml file and more finely configure the ORM layer.
Quarkus supports caching of entities, collections for one-to-many relationships, and queries. At first glance, it looks cool, but it is local caching, for one Kubernetes node. Those. caches of different nodes are not synchronized with each other. I hope this is temporary.
As mentioned above, Quarkus supports reactive programming style. The code of the previous application can be written in another form.
@Path("/hello") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) @Path("/{name}") public CompletionStage<String> greeting(@PathParam("name") String name) { return CompletableFuture.supplyAsync(() -> { return "Hello " + name + "!"; }); } }
Asynchronous code can also be transferred to the service, the result will be the same.
Tests for Quarkus applications can be written on JUnit4 or JUnit5. Below is an example of a test for endpoint, it is written using RestAssured, but you can use another framework:
@QuarkusTest public class GreetingResourceTest { @Test public void testGreetingEndpoint() { String uuid = UUID.randomUUID().toString(); given() .pathParam("name", uuid) .when().get("/hello/{name}") .then() .statusCode(200) .body(is("Hello " + uuid + "!")); } }
Annotation @QuarkusTest directs you to start the application before running the tests. For the rest - the code familiar to all developers.
Since Quarkus is tightly integrated with GraalVM, then, of course, you can generate platform-specific code. To do this, you need to set GraalVM and specify the GRAALVM_HOME environment variable. Next, register the profile for the assembly and specify it when building the application:
mvn package -Pnative
What is interesting, the generated application can be tested. And this is important because the execution of the “native” code may differ from the execution on the JVM. The @SubstrateTest annotation runs platform-specific application code. The reuse of existing test code can be done using inheritance, as a result, the code for testing a platform-dependent application will look like this:
@SubstrateTest public class GreetingResourceIT extends GreetingResourceTest { }
The generated image can be packaged in Docker and run in Kubernetes or OpenShift, described in detail in the instructions .
The Quarkus framework can be used with Maven and Gradle. Maven is fully supported, unlike Gradle. Unfortunately, at the moment Gradle does not support the generation of an empty project, the site has a detailed tutorial .
Quarkus is an extensible framework. Currently, there are about 40 extensions that add different functionality - from supporting Spring DI container and Apache Camel to logging and publishing metrics for running services. And there is already an extension to support writing applications in the Kotlin language, in addition to Java.
In my opinion, Quarkus is quite a trend in time. Developing backend code is becoming simpler and simpler, and this framework simplifies and accelerates the development of services even further by adding native support for Docker and Kubernetes. A huge plus is the built-in support for GraalVM and the generation of platform-specific images, which allows you to make services really fast starting and taking up little memory space. And this is very important in our time of mass hobbies microservices and serverless architecture.
Official site - quiarkus.io . Examples of projects for a quick start are already on GitHub .
Source: https://habr.com/ru/post/443242/
All Articles