IDEA remote deployment debugging Java application

Keywords: Java Spring Boot ssh IDEA skill

IDEA remote deployment debugging Java application

Basic overview

In our work, we may encounter that we cannot connect to the development environment database and other resources locally, but we want to develop and debug directly locally.

At this time, it can be realized through the Run on... Function of IDEA.

The principle is to connect to the remote server through SSH, deploy the application to the remote server, and locally connect the application deployed on the remote server.

PS: this operation mode has much higher security than building a proxy service on a remote server.

preparation

Remote server preparation

Install JDK

[root@switch-sz-service-test ~]# yum install -y java-1.8.0-openjdk-devel.x86_64

# You can see that the Java version is 1.8
[root@switch-sz-service-test ~]# java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)

Configure JAVA_HOME

# You can see JAVA_HOME is / usr / lib / JVM / java-1.8.0-openjdk-1.8.0.302.b08-0.el8_ 4.x86_ sixty-four
[root@switch-sz-service-test ~]# find / -name java 
/etc/pki/ca-trust/extracted/java
/etc/pki/java
/etc/alternatives/java
/etc/java
/var/lib/alternatives/java
/usr/bin/java
/usr/lib/java
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64/jre/bin/java
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64/bin/java
/usr/lib/jvm/java
/usr/share/bash-completion/completions/java
/usr/share/java
[root@switch-sz-service-test ~]# ll /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64
total 180
-rw-r--r-- 1 root root   1522 Jul 22 01:18 ASSEMBLY_EXCEPTION
drwxr-xr-x 2 root root   4096 Oct  4 00:29 bin
drwxr-xr-x 3 root root    132 Oct  4 00:29 include
drwxr-xr-x 4 root root     95 Oct  4 00:29 jre
drwxr-xr-x 3 root root    144 Oct  4 00:29 lib
-rw-r--r-- 1 root root  19274 Jul 22 01:18 LICENSE
drwxr-xr-x 2 root root    204 Oct  4 00:29 tapset
-rw-r--r-- 1 root root 155003 Jul 22 01:18 THIRD_PARTY_README

# Configure JAVA_HOME
[root@switch-sz-service-test ~]# vim /etc/profile

# Add the following statement on the last side
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64
export JAVA_HOME

# You can see that Java has been configured_ Home
[root@switch-sz-service-test ~]# source /etc/profile
[root@switch-sz-service-test ~]# echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64
[root@switch-sz-service-test ~]# 

Project preparation

Create a SpringBoot project

use Spring Initializr Create a SpringBoot project. Refer to the project: springboot-remote-deploy-demo

Create a Controller class

package com.switchvov.springboot.remote.deploy.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author switch
 * @since 2021/10/3
 */
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {
    @GetMapping("/{name}")
    public String hello(@PathVariable("name") String name) {
        String hello = "hello " + name;
        log.info(hello);
        return hello;
    }
}

Start the application and verify the results

package com.switchvov.springboot.remote.deploy.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootRemoteDeployDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootRemoteDeployDemoApplication.class, args);
    }
}
$ curl http://127.0.0.1:8080/hello/world
hello world%

PS: from the above steps, you can see that it has been successfully executed locally. The next step is to deploy it to the server remotely and debug it.

Application configuration

Modify application configuration

Right click the launcher next to the SpringbootRemoteDeployDemoApplication class to pop up the option box, and click Modify Run Configuration... To pop up the interface as shown in the following figure

Create remote server

Left click the Run on option box to pop up the option box. Click the SSH... Option to pop up the interface as shown in the figure below

Enter the server address Host, user name Username, and click the Next button to jump to the interface as shown in the figure below

Enter the password (or use the key), and click Next to jump to the interface as shown in the figure below

This step is mainly to verify whether you can log in to the server and whether the basic environment on the server is installed. Click Next to jump to the interface as shown in the figure below

Successfully connected to root@120.78.218.44:22

> pwd
/root
Command finished with exit code 0


Checking rsync connection...
/usr/bin/rsync -n -e "ssh -p 22 " root@120.78.218.44:

root@120.78.218.44's password: 


dr-xr-x---         190 2021/10/04 00:56:11 .

Process finished with exit code 0


Starting introspection for Java...
> echo ${SHELL}
/bin/bash
Command finished with exit code 0
> echo ${JAVA_HOME}
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64
Command finished with exit code 0
> java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)
Command finished with exit code 0

Introspection completed

You can see that the project deployment path Project path on target, JDK home path, JDK home path and JDK version have been set. Click Finish to return to the previous interface

PS: you can modify configurations such as deployment paths

Save application configuration

You can see that the remote server has been configured. Click OK to complete the configuration

Verification results

Local validation

Click the start button of SpringbootRemoteDeployDemoApplication. In the start log, you can see that it has been deployed to the server. At the same time, you can also see that the local port 63006 is mapped to the 8080 port of the server.

$ curl http://localhost:63006/hello/world
hello world%

Port 63006 mapped to the server can also be accessed locally.

PS: it can be started or debugged.

Server authentication

On the remote server, you can see that the springboot remote deploy demo has been deployed under the / root path, and access http://127.0.0.1:8080/hello/world Will return hello world correctly.

[root@switch-sz-service-test ~]# pwd
/root
[root@switch-sz-service-test ~]# ll
total 4
drwxr-xr-x 38 root root 4096 Oct  4 01:08 springboot-remote-deploy-demo
[root@switch-sz-service-test ~]# curl http://127.0.0.1:8080/hello/world
hello world[root@switch-sz-service-test ~]#

Reference documents

Posted by zaneosak on Sun, 03 Oct 2021 10:38:53 -0700