Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Description of premises:

Ok, hello everyone. Today, our sharing theme is “How to build a Quarkus-based cloud native micro-service architecture”. No matter whether you know or are familiar with Quarkus before, WE hope that you can get to know and know Quarkus through today’s technology sharing.

Introduction:

Well, without further ado, I think if we’re talking about Quarkus, we definitely need to analyze the current relationship between Java and cloud native services and where they’re going in the industry.


Java and cloud native market and wind direction in the industry

From a Java perspective:

With the passage of time and technology changes with each passing day, The status of Java has slowly declined, but “flaws”, Java has accompanied me through a lot of good times, and it is still a very important programming language for us! However, IT has to be said that Java has unmatched strength in terms of RPC, serialization, transport protocol and technology system for microservices. However, we gradually find that with the advent of microservices 2.0 and cloud native, Java may not be able to cope with it, because in the field of Java, It mainly relies on two core sources for technology promotion. First, it relies on Spring technology ecosystem. Second, Oracle officially promotes it. There are several official Oracle microservices frameworks and technical frameworks, such as Helidon, a lightweight microservices framework launched by Oracle in September 2018, Micronaut, but not all of them are ideal and still dominated by the mainstream Spring family. Spring’s technical focus has not yet fully shifted to cloud native.

From the perspective of cloud origin:

We all know that Kubernetes has been our current industry standard in the field of technology, operation and maintenance deployment, containerization, and also in the cloud native field. So how to make the Java application more perfect transformation to Kubernetes level is a big problem ah!

Don’t worry, Quarkus helped us solve this puzzle!

To get down to business:

First of all, we divided the Quarkus cloud native framework practice process into 6 parts, we called “Quarkus practice 6 steps!” What do they have?


Enter the basic concepts of Quarkus:

First of all, Quarkus, we can also call its Chinese name “Quarkus”, I believe you also know ali also has a browser called “quark”, a little similar, minus an s, we do not remember to mix, it is RedHat company open source a Java cloud native service framework, here, say an aside? Kubernetes very cow! These two “owners”, one is known as Google, the other is RedHat company, it is developed by RedHat company! How powerful is that?!

What are the features of Quarkus?

Let’s talk about what it has!

  1. Dedicated to creating cloud-native frameworks for GraalVM and OpenJDK HotSpot. This shows that it can exist side by side with both virtual machines! For HotSpot VIRTUAL machine (both Oracle JDK and OpenJDK we are familiar with!) GraalVM is a new generation of virtual machines (especially with a powerful compiler) and is the soul of Quarkus performance optimization.

  2. Specifically designed to optimize Java applications for containers and make them serverless architectures (even oriented and Faas, which is one of GraalVM’s credits), services on the cloud, and cloud native services in the Kubernetes model that we focus on!

  3. The main purpose is to integrate Kubernetes (especially Kubernetes cluster) and born, very targeted and professional!

The advantages of Quarkus

Let’s see what Quarkus has to offer!

  1. First of all, let’s go back to the concept of GraalVM. We just talked about the compiler capabilities of GraalVM, so we know that it is possible to improve performance by 2 to 5 times by using the inlining mechanism of GraalVM’s compiler, escape analysis and unique optimization techniques. For the record, GraalVM was created to replace the JVM’s original C2 compiler, which was the work of Ciff Click, but was too big and complex to maintain. So the bottleneck of JVM optimization has been the C2 compiler, which is very bad for Java! So there is GraalVM, and also Java implementation, interested friends, can take a good look!

  2. We all know that the Spring framework is not about performance, but scalability, and Quarkus is no less scalable than Spring! Like Spring, you can integrate open source libraries like Eclipse MicroProfile, Hibernate, Apache Camel, and Eclipse vert. x, and even migrate SpringBoot to the Quarkus ecosystem. Solve the migration of many companies or development team complex worries!

  3. (Cloud native upgrade). Before we developed Java projects, if we wanted to make Docker images or Kubernetes images, we had to rely on additional third-generation plug-ins or development libraries, which led to our projects being too bloated, and Spring’s support for Docker or K8s was not very satisfying. But with Quarkus, you can easily integrate with Docker and Kubernetes without relying on extra plug-ins, It can create Docker resources (including JVM-based DockerFiles, cloud native Dockerfiles), Kubernete resources and even OpenShift resources.

  4. (Service startup performance improvement) this is a big deal, there is more to Quarkus services than just having a JVM running mode. For the same application code, the service starts up very quickly in native mode (about 3 times faster, saving us a lot of waiting time), and most Spring projects take up a lot of space, often resulting in a lot of OOM or FGC, as I’m sure you’ve all experienced. Previously, we could only remedy the problem by slimming down Spring, or improving the ability of JVM GC and fast collection. Whether we use the collection algorithm or the configuration of the machine to solve this problem, I think it is only a remedy. But with the Quarkus service, it solves the big memory footprint of Spring projects because it’s small and low (especially in native mode, where the underlying water is deep!). “, can greatly improve performance and resource utilization, does this mean that we can most likely kiss “OOM” goodbye? Put a question mark here.

One more thing: Leveraging GraalVM, Quarkus has also addressed many of the cross-language aspects of the Java architecture. For example, it allows more languages to be integrated into the Java ecosystem (for example, combining Java, JavaScript, Ruby, and R) to form a multi-language microservice ecosystem. Isn’t it beautiful?


Quarkus basic development process

We call it quacks Development 6 for short. Let’s see what they are.

  • The first step in “scaffolding” is to quickly set up Quarkus’s application cloud native framework (which lays the foundation for building application services)

  • The “developer mode” phase stands for two things:

    • 1: It means we need to get into development mode. I’m just kidding!
    • 2: It is important to note that the Quarkus microservices application’s ability to implement real-time hot deployment (changes take effect in real time) is similar to SpringBoot’s devTools or Spring Loader, or even fee-based JRebel, where additional packages or plug-ins are required. Quarkus directly controls the operation mode as its own. Progress is not a little ah!
  • Business Code develops application code by integrating multiple open source libraries and related business requirements

  • “Build integration test” When the development code is completed to build the version, the development level of integration test phase

  • Generate Image Package To create CLI programs, cloud native executable package files, and corresponding container services

  • “Deployment Base K8S” integrates the corresponding executable application files, container files, and cloud native file packages into Kubernetes cluster directly


Quarkus environment building process

After understanding the overall development practice process, I think this is the real start of Quarkus practice. Let’s first look at the officially designated environment construction process, let’s call it “Quarkus 5 Steps”.


  1. You need a developer tool, such as IDEA, Eclipse, etc

  2. You need the JDK11+ version of the JVM environment resources

  3. Select a GraalVM virtual machine as the basis for the compiler

  4. It is best to choose a Maven(version 3.8.1)/Gradle tool

  5. Finally, based on the latest official Quarkus version (2.3.0.final) for development and implementation.

Quarkus service architecture construction

First of all, there are four schemes to build the framework, which are as follows:

Maven builds the Quarkus application service skeleton

Let’s take a look at the first build method, building the Quarkus application service skeleton through Maven

Maven project framework includes the following:
  • The Maven environment POM.xml file required by Quarkus services

  • Basic JAX-RS resource service classes

  • Corresponding jax-RS resource service test class

  • Quarkus native mode test class

  • The Dockerfile file used to create the Docker container

  • Empty configuration file (application.properties)


Gradle builds the Quarkus application service skeleton
The Gradle project framework contains the following contents:
  • Quarkus service requires Gradle environment build. Gradle file

  • Basic JAX-RS resource service classes

  • Corresponding jax-RS resource service test class

  • Quarkus native mode test class

  • The Dockerfile file used to create the Docker container

  • Empty configuration file (application.properties)

Here, we can see a point, in order to let everyone familiar with Quarkus, fast development of Quarkus service, it uses and SpringBoot very similar architecture and parameter configuration, can let us quickly iterative implementation of the development mechanism!


Quarkus Start Coding built the skeleton of the project

First, we open a browser and go directly to code.quarkus.io

Let’s focus on the three areas in my red line!

Quarkus Start Coding scaffolding website

  • Group: Configure the Maven groupId for the application.
  • Artifact: Maven artifactId for configuring an application, usually the name of our application
  • Build: Here you can select Maven/Gradle

And the following Web section:

It requires us to choose the implementation we want for the Rest services framework: you can see a lot of doha.

Click -Genrate Your Application, submit directly, and download the relevant code skeleton!

This is very similar to our Spring Initializer.


Development tools build the Quarkus application service skeleton

Create a new Quarkus project

  • IDEA Main menu choose File -> New Project

  • Select create the Quarkus application

  • Select the default Server URL: code.quarkus.io.

[= = = = = = = =] page

IDEA install Quarkus supported plug-ins

If your IDEA does not support the Quarkus function, you need to install the plug-in, because the Quarkus project is not available in the earlier version. We can choose the way shown in the figure to install the plug-in.


Quarkus application running mode

First of all, we can see that Quarkus mainly has three modes of operation, which are development mode, test mode and production mode.

Quarkus has 3 different startup modes:

  • Development environment

    • Run the quarkus:dev maven command directly, then the mode is DEVELOPMENT, which I will introduce in detail later, especially convenient for our DEVELOPMENT, can realize the real-time implementation of changes.
  • Test (Test environment)

    • If you are running JUnit tests, the pattern changes to Test, for example, where more powerful Testcontainers or native executable files can be implemented.
  • Normal (Production environment)

    • In the above two cases, both belong to Normal mode, which is our official release of the executable package file.

Quarkus development mode for real-time hot deployment

The development pattern, as I said earlier, is largely benchmarked against Spring Loader, Spring DevTools, Jrebel and other hot deployment mechanisms

Quarkus dev (Development mode)

If development mode is used, background compilation implements the hot deployment mechanism.

  • When you modify a Java file or static resource file, you just need to refresh the browser to automatically change, real-time convenience!
Start the Quarkus application in Dev mode

How do you start the mechanics of development mode?

  • Run the MVNW quarkus:dev command from the Maven project root directory

  • Gradle project root command: gradlew quarkusDev

  • You can also run it directly by clicking the Maven plugin of IDEA.

  • Note: JVM versions higher than Java11 are recommended

Quarkus development mode for real-time hot deployment

In the figure on the left above, the Quarkus resource Service interface code is shown: Returns a Hello RESTEasy interface!

Curl curl curl curl curl curl curl curl curl curl curl

At this point, when we modify the code, we change the return parameter to hello.

After re-invoking, you can see that the result ishello

Can say, real very convenient!


Introduction to Quarkus Endpoint service development

  • Quarkus framework for Restful Web services support capabilities

    • Quarkus integrates with the RestEasy framework, a JAX-RS framework that follows the Rest protocol pattern and is its basic default framework for defining Rest endpoints.
  • Quarkus framework for Restful Web services development

    • Quarkus uses, by using JAX-RS annotations to modify bindings, by using HTTP annotations (@GET, @POST, @PUT, @delete, @Patch, etc.) to declare the request type of the endpoint service, and by using the @PATH annotation to define the public Path part.
  • Quarkus framework for receiving resource types and output resource types

    • The @Consumes annotation defines the types of resources that are received by an endpoint service, for example, @Consumes(mediatype.text_plain).

    • The @Produces annotation defines the receiving resource type of the endpoint service, for example: @Produces(MediaType.TEXT_PLAIN)

Introduction to Quarkus endpoint service annotations

  • The Quarkus framework implements the basic development structure for endpoint services

    • Defines the URI locator Path for the current resource: the @path annotation

    • Design the Http request method type @get annotation

    • Defines the corresponding type @produces annotation for the resource that returns the type

  • Defines parameters related to receiving Http queries

    • The @QueryParam annotation defines the endpoint request parameters

    • The @HeaderParam annotation defines the request parameters of the endpoint service

    • The @FormParam annotation defines the form parameters of the endpoint service

    • The @cookieParam annotation defines the cookie parameters of the endpoint service

    • The @PathParam annotation defines the path variable of the endpoint service