基本概述
在工作中我们可能会遇到本地无法连接开发环境数据库等资源,但又想在本地直接开发、 调试 。
这时候就能通过 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:从如上步骤,可以看到已经成功在本地执行了,接下来就是要让他远程部署到服务器上,并且可以调试。
应用配置
修改应用配置
右键点击 SpringbootRemoteDeployDemoApplication 类旁边的启动符,弹出选项框,点击 Modify 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 按钮配置完成
验证结果
本地验证
点击 SpringbootRemoteDeployDemoApplication 的启动按钮,在启动日志中可以看到已经部署到服务器上,同时也能看到本地端口 63006 映射到了服务器的 8080 端口。
$ curl http://localhost:63006/hello/world hello world%
在本地访问映射到服务器的端口 63006 ,也能正常访问。
PS:可以启动,当然也可以进行调试。
服务器验证
在远程服务器上,可以看到 springboot-remote-deploy-demo 已经被部署在 /root 路径下了,且访问 http://127.0.0.1:8080/hello/world 会正确返回 hello 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 ~]#
参考文档
Spring Initializr
springboot-remote-deploy-demo
到此这篇关于IDEA远程部署调试Java应用 程序 的文章就介绍到这了,更多相关IDEA远程部署调试Java应用程序内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://www.cnblogs.com/switchvov/p/15365588.html
查看更多关于IDEA远程部署调试Java应用程序的详细流程的详细内容...