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

preface

As microservices and container technology continue to upgrade, the requirements for applications are becoming higher and higher. In addition to the most basic “fast and efficient response to requests”, applications should also be able to develop and iterate quickly, and ideally run in containers and other environments with as little volume and memory footprint as possible. Faced with these requirements, Java and its mainstream development frameworks are considered inferior to other languages (such as Go).

Quarkus, which is container first, is a product of the cloud native era, but also supports JVM VMS. If you’re struggling with slow bucket startup and high memory usage, try Quarkus and you’ll love it. In particular, hot reloading during development is a boon, no need to rely on plug-ins or light a cigarette while waiting to restart an application.

Of course, there is no step on the meaning of one holding one, each framework has its own limitations, different times born different products, according to business needs to choose their own framework is the most important, any technology from business discussion is playing rogue ~

Create an

Without further ado, the cow force blows out, then directly out of the keyboard, we come to a shuttle code.

Depend on the environment

  • An IDE (Eternal God of Intellij IDEA)
  • JDK 11+
  • Apache Maven 3.8.1+ (or Gradle)

Create a project

There are many ways to create projects. For example, you can create projects directly with IDEA, you can create projects in Start Coding on the official website (like Spring Initializr), you can clone quickStart projects with Git, and you can create projects using MVN. Here we introduce two more commonly used methods.

Using the IDEA of

This is the most recommended practice and provides great convenience in both creating and running development.

If you are using a New version of IDEA, you can create Quarkus application directly in file-new Project, and the Example code can be checked or unchecked.

Select RESTEasy JAX-RS to implement REST requests.

When you click Finish, the code template will be downloaded automatically, and after a few moments, the project will open.

With Maven (Gradle) dependencies downloaded, you can see the project structure and start developing.

Use the official website START CODING

Open the code. Quarkus. IO

Click to download the package

After downloading, unzip the file and open it through the IDE. Wait for Maven (Gradle) to download the dependent file automatically. After downloading, we can see the following project structure

You can see that the structure of the project is basically the same as the IDEA created, so we start the performance.

REST requests

Under the program created in the above, check the SRC/main/Java/com/demo/GreetingResource. Java (or ExampleResource. Java) file, its content is as follows:

package com.demo; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello RESTEasy"; }}Copy the code

This completes the simplest REST request, which returns the Hello RESTEasy text when accessing/Hello.

You can see that Quarkus’s development process and even annotations are similar to SpringBoot, which makes it relatively easy for Spring developers to switch to Quarkus.

A brief explanation of the meaning of each note:

  • @PathSpring equivalent@Controller + @RequestMapping
  • @GETSpring equivalent@GetMapping
  • @ProducesEquivalent to the Spring@RequestMappingtheproducesattribute

To run the program

With Quarkus, you don’t need to create a separate Application launcher. You can, but you don’t need to.

Start with IDEA

If the project is created by IDEA, it can be run directly

Click OK

Click the DEBUG button to start the program

After the startup, you can view the logs output by the console. If the following logs are displayed, the startup is successful

Start with./ MVN

Open the terminal, enter the project directory, and run the command: / MVNW compile quarkus:dev

Successful startup ~

The test request

After the startup is successful, use curl to request the interface

$ curl -w "\n" http://localhost:8080/hello
Hello RESTEasy
Copy the code

Hello RESTEasy is returned.

Let’s change the program and add a new request /2 to ExampleResource to see how cool Quarkus’s hot overload is.

package com.example; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class ExampleResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello RESTEasy"; } @GET @Path("2") @Produces(MediaType.TEXT_PLAIN) public String hello2() { return "Hello 2"; }}Copy the code

When the code is done, request the new /hello/2 interface:

$ curl -w "\n" http://localhost:8080/hello/2
Hello 2
Copy the code

The result is straight out, such a development experience, not bad.

finishing

At this point, we have a basic Quarkus application, but we are still a long way from actual project development. In my spare time before, I developed the Quarkus rapid development scaffold Quarkus-vue-admin. Now, I have realized the basic rights management, code generation and other functions, which are ready to use. With this scaffolding, it’s much easier to get started with Quarkus and develop actual projects, which are basically ready to use.

At present, there are still many problems with this scaffold, and the business time is limited. We also hope that friends with ideas can develop it together.

In the future, I will use Quarkus-VUe-admin to develop real project cases and carry out version iteration on scaffolding during the development process, contributing to the development and dissemination of Quarkus.

In addition to Quarkus-vue-admin, the big guys in the group are planning Quarkus micro-service scheme, interested partners can add group discussion: 871808563.

  • Source code address: github.com/whatever-te…
  • Demo site: what-team.github. IO/Quarkus-vue… admin/888888
  • IO/Quarkus-vue…