在工作中,我们可能会遇到本地无法连接开发环境数据库等资源,但又想在本地直接开发、调试。
这时候就能通过IDEA
的Run on ...
功能实现。
其原理是通过SSH
连上远程服务器,部署应用到远程服务器后,本地连接上远程服务器部署的应用。
PS:这种操作方式比在远程服务器上搭建代理服务,安全性要高的多得多。
[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是/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 ~]#
使用Spring Initializr创建一个SpringBoot项目,参考项目:springboot-remote-deploy-demo
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 ~]#
分享并记录所学所见