IDEA遠程部署調試Java應用程序
[TOC]
基本概述
在工作中,我們可能會遇到本地無法連接開發環境資料庫等資源,但又想在本地直接開發、調試。
這時候就能通過IDEA的Run on ...功能實現。
其原理是通過SSH連上遠程伺服器,部署應用到遠程伺服器後,本地連接上遠程伺服器部署的應用。
PS:這種操作方式比在遠程伺服器上搭建代理服務,安全性要高的多得多。
準備工作
遠程伺服器準備
安裝JDK
[root@switch-sz-service-test ~]# yum install -y java-1.8.0-openjdk-devel.x86_64
# 可以看到Java的版本是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)配置JAVA_HOME
# 可以看到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 ~]# 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
# 配置JAVA_HOME
[root@switch-sz-service-test ~]# vim /etc/profile
# 在最後面添加上如下語句
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64
export JAVA_HOME
# 可以看到已經配置好了JAVA_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 ~]# 項目準備
創建一個SpringBoot項目
使用Spring Initializr創建一個SpringBoot項目,參考項目:
springboot-remote-deploy-demo

創建一個Controller類
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;
}
}啟動應用,驗證結果
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:從如上步驟,可以看到已經成功在本地執行了,接下來就是要讓他遠程部署到伺服器上,並且可以調試。
應用配置
修改應用配置

右鍵點擊類旁邊的啟動符,彈出選項框,點擊
SpringbootRemoteDeployDemoApplicationModify Run Configuration...選項,彈出界面如下圖
創建遠程伺服器

左鍵點擊Run on選項框,彈出選項框,點擊SSH...選項,彈出界面如下圖

輸入伺服器地址Host,用戶名Username,點擊Next按鈕,跳轉界面如下圖

輸入密碼Password(或者使用密鑰),點擊Next跳轉界面如下圖

這一步,主要是驗證是否能登錄上伺服器,以及伺服器上基本環境是否安裝好,點擊Next跳轉界面如下圖
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
可以看到項目部署路徑Project path on target,JDK Home路徑JDK home path以及JDK版本JDK version都已經設置好了,點擊Finish返回之前的界面
PS:可以自己修改部署路徑之類的配置
保存應用配置

可以看到遠程伺服器已經配置好了,點擊OK按鈕配置完成
驗證結果
本地驗證

點擊的啟動按鈕,在啟動日誌中可以看到已經部署到伺服器上,同時也能看到本地埠
SpringbootRemoteDeployDemoApplication63006映射到了伺服器的8080埠。
$ curl http://localhost:63006/hello/world
hello world%在本地訪問映射到伺服器的埠63006,也能正常訪問。
PS:可以啟動,當然也可以進行調試。
伺服器驗證
在遠程伺服器上,可以看到已經被部署在
springboot-remote-deploy-demo/root路徑下了,且訪問會正確返回
http://127.0.0.1:8080/hello/worldhello world。
[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 ~]#原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/216711.html
微信掃一掃
支付寶掃一掃