IDEA remote deployment debugs Java applications

[TOC]

Basic overview

In the work, we may encounter local can not connect to the development environment database and other resources, but want to directly develop and debug in the local.

IDEA Run on… Function implementation.

The remote server is connected over SSH. After applications are deployed on the remote server, applications deployed on the remote server are connected locally.

PS: This mode of operation is much more secure than setting up proxy services on remote servers.

The preparatory work

Preparing the remote Server

Install the 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 comes with _302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)
Copy the code

Configuration JAVA_HOME

JAVA_HOME is /usr/lib/jvm/java-1.8.0-openJDK-1.8.0.302.b08-0.el844.x86_64
[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 The/usr/lib/Java/usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.302. B08-0. El8_4. X86_64 / jre/bin/Java The/usr/lib/JVM/Java -- 1.8.0 comes with its - 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 comes with its - 1.8.0.302. B08-0. El8_4. X86_64total 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# configuration JAVA_HOME
[root@switch-sz-service-test ~]# vim /etc/profile

# add the following statement at the endJAVA_HOME = / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.302. B08-0. El8_4. X86_64export JAVA_HOME

# You can see that JAVA_HOME has been configured
[root@switch-sz-service-test ~]# source /etc/profile
[root@switch-sz-service-test ~]# echo $JAVA_HOMEThe/usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.302. B08-0. El8_4. X86_64 [root @ switch - sz - service - the test ~]# 
Copy the code

Project preparation

Create a SpringBoot project

Create a SpringBoot project using Spring Initializr. See 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
 * @since2021/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);
        returnhello; }}Copy the code

Start the application and verify the result

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); }}Copy the code
$% curl http://127.0.0.1:8080/hello/world hello worldCopy the code

PS: From the above steps, you can see that it has been successfully executed locally, the next step is to make it remotely deployed to the server, and can be debugged.

Application configuration

Modifying Application Configurations

Right-click the SpringbootRemoteDeployDemoApplication classes start, beside pop-up option box, and click the Modify the Run Configuration… Option, popup interface as shown below

Creating a remote server

Left-click on the Run on option box to bring up the option box, click SSH… Option, popup interface as shown below

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

Enter the Password(or use the key) and click Next to jump to the interface as shown below

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

Successfully connected to [email protected]:22

> pwd
/root
Command finished with exit code 0


Checking rsync connection...
/usr/bin/rsync -n -e "ssh -p 22 " [email protected]:

[email protected]'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.el844.x86_64 Command finished with exit code 0 > Java -version openJDK Version "1.8.0_302-B08" OpenJDK Runtime Environment (build 1.8.0_302-b08) mixed mode) Command finished with exit code 0 Introspection completedCopy the code

JDK Home path JDK Home Path AND JDK version JDK Version have all been set. Click Finish to return to the previous screen

PS: You can modify configurations such as deployment paths

Saving the Application Configuration

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

The verification results

Local validation

Click the start button SpringbootRemoteDeployDemoApplication, you can see in the startup log has deployed to the server, at the same time also can see the local port 63006 8080 port mapping to the server.

$ curl http://localhost:63006/hello/world
hello world%
Copy the code

Port 63006 mapped to the server can also be accessed on the local PC.

PS: Can be started, of course, can also be debugged.

Server authentication

On a remote server, you can see springboot – remote – deploy – demo have been deployed in the/root directory path, and http://127.0.0.1:8080/hello/world will return to the 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 ~]#
Copy the code

Reference documentation

  • Spring Initializr
  • springboot-remote-deploy-demo

Share and record what you learn and see