quarkus Combat 4: remote hot deployment

Posted by programmermatt on Wed, 09 Mar 2022 00:37:39 +0100

Welcome to my GitHub

Here we classify and summarize all the original works of Xinchen (including supporting source code): https://github.com/zq2599/blog_demos

Overview of this article

  • This article is the fourth in the "quarkus actual combat" series. As the title shows, today's task is to complete the actual combat of remote hot deployment
  • As a Java programmer, I believe you are familiar with the following scenarios:
  1. On the local computer: write code with IDEA, compile and build it into jar or docker image
  2. On the server: run jar or docker image
  3. When encountering problems: through hot deployment, local changes will take effect on the server immediately. All this is automatic without packaging and deployment
  • As shown in the figure below:

  • The above are common ways to deal with problems. If our code is a quarkus application, can we debug remotely like this?

  • The answer is yes. Next, let's practice how to debug quarkus applications remotely

Remember the official warning

  • As shown in the red box in the figure below, we must keep in mind the official warning that the remote hot deployment function has great security risks and should not be used in the production environment:

How it works on the server

  • In the following actual combat, in order to save trouble, docker is selected as the operation mode on the server, that is, after the docker image is made locally, it runs on the docker of the server

demo project

  • The demo project used today is very simple, with a common web interface
  • Execute the following command to create a maven project named Hello quarkus
mvn "io.quarkus:quarkus-maven-plugin:create" \
  -DprojectGroupId="com.bolingcavalry" \
  -DprojectArtifactId="hello-quarkus" \
  -DprojectVersion="1.0-SNAPSHOT" \
  -DclassName="HobbyResource" \
  -Dpath="actions"
  • In order to demonstrate that modifying the configuration file can also take effect immediately, hobbyresource Java is changed to the following. Note that the function of the annotation ConfigProperty is to inject the value of the specified configuration into the modified member variable:
package com.bolingcavalry;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDateTime;

@Path("/actions")
public class HobbyResource {

    @ConfigProperty(name = "greeting.message")
    String message;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return message + ", Hello RESTEasy " + LocalDateTime.now();
    }
}
  • Open the file application Properties, add the following three configurations. See the notes for the role of each configuration:
# This is a user-defined property. Use the ConfigProperty annotation in the business code to get its value
greeting.message=message from configuration
# The parameters used in remote debugging are variable jars, that is, jars that support hot deployment
quarkus.package.type=mutable-jar
# For the parameters used in remote debugging, you need to specify a password for security
quarkus.live-reload.password=changeit
  • The above is all the project codes

Make docker image

  • In POM Execute the following command in the directory where XML is located to complete the normal compilation and construction:
mvn clean package -U -DskipTests
  • Then execute the following command to create a docker image according to the compilation results just now. Note that this is a jar+jdk based image, not a binary executable image:
docker build \
-f src/main/docker/Dockerfile.jvm \
-t bolingcavalry/hello-quarkus-jar:0.0.7 .
  • You need to do some operations so that the server can run the image. For example, export it as tar, then import it in the server, or push it to an image warehouse, so that the server can pull from the warehouse
  • What I do here is push it to hub docker. COM, you can choose which way to use according to your actual situation

Running on server

  • The next operation is performed on the server
  • First, make sure that the server can use the image (access the image warehouse, or import with tar, etc.)
  • Execute the following command to start the image, and pay attention to the environment variable QUARKUS_LAUNCH_DEVMODE is required and the value is true, which is the key to enabling remote hot deployment:
docker run \
-i \
--rm \
-p 8080:8080 \
-e QUARKUS_LAUNCH_DEVMODE=true \
bolingcavalry/hello-quarkus-jar:0.0.7
  • The console output is shown in the figure below. The application is successfully started. The red box shows that it has entered the hot deployment state and can accept the changes synchronized by remote debugging at any time:
  • Verify whether the service is normal. The server IP address here is 192.168.50.27, so the browser can access it http://192.168.50.27:8080/actions The results are shown in the figure below, which is in line with expectations:
  • Next, try remote hot deployment

Remote hot deployment

  • The next operation is on the computer
  • Execute the following command to start local and remote synchronization mode:
mvn quarkus:remote-dev -Dquarkus.live-reload.url=http://192.168.50.27:8080
  • The console output is as follows:
[INFO] Compiling 2 source files to /Users/will/temp/202203/01/001/hello-quarkus/target/test-classes
Listening for transport dt_socket at address: 5005
2022-03-02 08:52:44,299 INFO  [org.jbo.threads] (main) JBoss Threads version 3.4.2.Final
2022-03-02 08:52:45,488 INFO  [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 1532ms
2022-03-02 08:52:46,402 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending quarkus-app-dependencies.txt
2022-03-02 08:52:46,418 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending quarkus-run.jar
2022-03-02 08:52:46,424 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending app/hello-quarkus-1.0-SNAPSHOT.jar
2022-03-02 08:52:46,453 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Connected to remote server
  • It can be seen from the above information that the connection between the local server and the remote server has been established. You can try to modify the local file to see whether it can be synchronized automatically
  • Modify the configuration file application Properties, set the greeting Change the value of message to aaabbb
  • After the modification, it will not be synchronized immediately. The synchronization will not be triggered until the web interface is called again to refresh the browser. As shown in the figure below, it is found that the locally modified configuration file has been synchronized to the server and has taken effect:
  • See the following output from the local console to prompt the details of synchronization:
2022-03-02 08:57:40,568 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (Remote dev client thread) File change detected: /Users/will/temp/202203/01/001/hello-quarkus/src/main/resources/application.properties
2022-03-02 08:57:40,572 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (Remote dev client thread) Restarting quarkus due to changes in application.properties, HobbyResource.class.
2022-03-02 08:57:41,138 INFO  [io.qua.dep.QuarkusAugmentor] (Remote dev client thread) Quarkus augmentation completed in 564ms
2022-03-02 08:57:41,143 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (Remote dev client thread) Live reload total time: 1.082s 
2022-03-02 08:57:41,556 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending lib/deployment/.io.quarkus.quarkus-resteasy-common-spi-2.7.1.Final.jar.baiduyun.uploading.cfg
2022-03-02 08:57:41,640 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending quarkus-run.jar
2022-03-02 08:57:41,649 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending app/hello-quarkus-1.0-SNAPSHOT.jar
2022-03-02 08:57:41,676 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending dev/app/application.properties
  • At this time, when you look at the console on the server, there is also information output, as shown in the red box below. After receiving the synchronized content, the application automatically restarts internally (note that it is an internal restart, and the docker container has not changed):
  • Just now I tried to modify the configuration file. Now I try to modify the source code. The changes are shown in the red box below:
  • The browser accesses the web service of the service, as shown in the figure below. The change has taken effect:
  • The console of the development computer outputs synchronization information. This time, it is a class file:
2022-03-02 09:05:56,243 INFO  [io.qua.dep.QuarkusAugmentor] (Remote dev client thread) Quarkus augmentation completed in 520ms
2022-03-02 09:05:56,248 INFO  [io.qua.dep.dev.RuntimeUpdatesProcessor] (Remote dev client thread) Live reload total time: 0.985s 
2022-03-02 09:05:56,610 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending dev/app/com/bolingcavalry/HobbyResource.class
2022-03-02 09:05:56,804 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending quarkus-run.jar
2022-03-02 09:05:56,811 INFO  [io.qua.ver.htt.dep.dev.HttpRemoteDevClient] (Remote dev client thread) Sending app/hello-quarkus-1.0-SNAPSHOT.jar
  • Look at the console of the server and restart the application again. The red box shows the changes received from the class file:
  • So far, the operation of remote hot deployment function has been completed. Mastering one more practical skill is helpful for subsequent learning and development. I hope this article can give you some reference

You're not alone. Xinchen's original accompanies you all the way

  1. Java series
  2. Spring series
  3. Docker series
  4. kubernetes series
  5. Database + middleware series
  6. DevOps series

Topics: quarkus