Quarkus&GraalVM introduces, creates, and starts the first project
Quarkus series of blog posts
- Quarkus&GraalVM introduces, creates, and launches the first project
- Build Quarkus local image, container deployment Quarkus project
- .
Quarkus introduction
Quarkus is a full-stack Kubernetes native Java framework designed for Java Virtual Machine (JVM) and native compilation to optimize Java specifically for containers and make it an efficient platform for serverless, cloud, and Kubernetes environments.
Quarkus works with common Java standards, frameworks, and libraries, For example, Eclipse MicroProfile, Apache Kafka, RESTEasy (JAX-RS), Hibernate ORM (JPA), Spring, Infinispan, Camel, etc.
Quarkus’s dependency injection solution is based on CDI (Context and dependency injection) and includes an extension framework to extend functionality and configure, bootstrap, and integrate it into your application. Adding extensions is as easy as adding dependencies; Alternatively, you can use the Quarkus tool.
It is also a compelling feature, as it provides the right information to GraalVM, a general-purpose virtual machine for running applications written in multiple languages, including Java and JavaScript, for native-compilation of applications.
Rad Hat has a checklist to illustrate the benefits of using Quarkus: checklists
Quarkus in contrast to traditional Java frameworks
An official chart shows a detailed comparison of some runtime data for projects developed using Quarkus versus traditional frameworks, showing that Quarkus projects run in the JVM using significantly more memory and interface responsiveness than traditional Java technology stacks. Once Quarkus is compiled into a local executable (a local image), the advantage becomes obvious.
GraalVM profile
GraalVM is a high performance virtual machine that can significantly improve the performance and running efficiency of applications and is ideal for microservices. It is designed to implement applications written to run in different languages (Java, JavaScript, LLVM-based languages (such as C and C ++), and other dynamic languages). It eliminates barriers between different programming languages and enables interoperability between multilingual shared runtimes. It can run independently or in the context of OpenJDK, Node.js, or Oracle databases.
For Java applications, GraalVM can bring a number of valuable benefits: running them faster, through scripting languages (JavaScript, R, Python…) Provide extensibility or create pre-compiled native images.
More information about GraalVM can be found in this article.
GraalVM installation
In this article, we use SDKMAN to install GraalVM. SDKMAN is a tool for managing parallel versions of multiple software development suites on most UNIX-based systems. It provides a convenient command line interface (CLI) and API for installing, switching, removing, and listing candidates. Formerly known as Groovy enVironment Manager (GVM), it is inspired by the very useful RVM and Rbenv tools that are widely used in the Ruby community.
Install SDKMAN
Run the following command to install:
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
Copy the code
Verify that ADKMAN is installed by running the following command:
$ sdk version
Copy the code
Install GraalVM
Run the following command:
$ sdk list java
Copy the code
You can see that the SDKMAN lists all supported Java distributions
We found the distribution of GraalVM
As of this writing, the latest version of GraalVM is 20.1.0.r11-grl, so we will install this version. Run the following command to install GraalVM:
$SDK install Java 20.1.0.r11-grlCopy the code
GraalVM is now installed! We can determine if GraalVM is installed by running the following command:
$ java -version
Copy the code
Create a project
There are several ways to create Quarkus projects
Create Quarkus projects using Intellij IDEA
Go to the menu bar File>New>Project… Creating a new project
Click Next and fill in the appropriate information. Next>Next… “Is created.
Create the Quarkus project using the Maven command line
Run the following command to create the Quarkus project:
MVN IO. Quarkus: quarkus -- maven plugin: 1.6.0. Final: create \ - DprojectGroupId = org acme \ - DprojectArtifactId = getting started \ -DclassName="org.acme.getting.started.GreetingResource" \
-Dpath="/hello"
cd getting-started
Copy the code
At this point, the project is created!
Start the project
We use IDEA to open the project
Quarkus does not have a startup class like Spring Boot or Helidon, so we need to run Maven to start the project.
Run the following command on the IDEA console to start the project:
./mvnw compile quarkus:dev
Copy the code
Startup successful!
Of course, it is inconvenient to run the command line every time. We can configure the project to start quickly by the following configuration:
Click the “+” icon in the upper left corner to add a Maven configuration like the one on the left. In the Command line on the right, fill in “compile quarkus:dev” and click OK.
Click on the icon shown below to easily launch the project
Run the test
Open the test class in your project and see the following code:
@QuarkusTest / / 1
public class ExampleResourceTest {
@Test
public void testHelloEndpoint(a) {
given()
.when().get("/hello")
.then()
.statusCode(200) / / 2
.body(is("hello")); }}Copy the code
- By running the program with the @Quarkustest annotation, you can instruct JUnit to start the application before testing.
- Check the HTTP response status code and content.
By default, tests are run on port 8081 to avoid conflicts with running applications. Quarkus automatically configures RestAssured to use this port. If you want to test other paths, you can use the @testhttpresource annotation to inject the TESTED URL directly into the fields of the test class. The type of this field can be a string, URL, or URI. We need to specify the value of the test path for this annotation. For example, if I were to test a Servlet that maps to/myServlet, I would simply add the following to the test:
@QuarkusTest
public class ExampleResourceTest {
@TestHTTPResource("/myservlet")
URL testUrl;
@Test
public void testHelloEndpoint(a) {
given()
.when().get(testUrl)
.then()
.statusCode(200)
.body(is("hello")); }}Copy the code
You can control the test port by configuring the quarkus.http.test-port property in the project configuration file. Quarkus also creates a system property called test.url whose value will be set to the base test URL (BasePath).
conclusion
We have entered the era of cloud native and microservices. We have bid farewell to the enormity and complexity of large individual applications and have reaped the great benefits brought by microservices. But problems are beginning to set in. As microservices proliferate, redundant and useless dependencies that once occurred in individual applications, the slow startup process inherent in Java projects, and JIT optimization problems spread to each microservice. Moreover, the traditional Java EE specification does not have a pattern solution for microservices, and the problem needs to be solved urgently. Fortunately, new Java development frameworks like Quarkus, Helidon, and others (and now the Spring Ecosystem’s support for GraalVM) have eased the situation by making Java more local, Both in terms of project volume and in terms of resource consumption and operational efficiency.
🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟 🌟
Welcome to my blog:blog.dongxishaonian.tech