1. 程式人生 > >VS Code Remote,在伺服器上開發程式,開啟全新開發模式

VS Code Remote,在伺服器上開發程式,開啟全新開發模式

一直使用Idea開發java 程式,頭疼的是太太太佔用記憶體了,膝上型電腦經常卡爆,在伺服器開發的話又太麻煩,VS Code Remote的帶來,解決了這一煩惱。下面來實戰一下。

VS Code Remote

2019 年 5 月 3 日,在 PyCon 2019 大會上,微軟釋出了 VS Code Remote,開啟了遠端開發的新時代

Visual Studio Code Remote 允許開發者將容器,遠端計算機,或 Windows Subsystem for Linux (WSL) 作為完整的開發環境。你可以:

  • 在部署相同的作業系統上進行開發,或者使用更大或更專業的硬體。

  • 把開發環境作為沙箱,以避免影響本地計算機配置。

  • 讓新手輕鬆上手,讓每個人都保持一致的開發環境。

  • 使用原本在本地環境不可用的工具或執行時,或者管理它們的多個版本。

  • 在 WSL 裡開發 Linux 應用。

  • 從多臺不同的計算機訪問現有的開發環境。

  • 除錯在其他位置(比如客戶網站或雲端)執行的應用程式。

所有以上的功能,並不需要在你的本地開發環境有原始碼。通過 VS Code Remote,輕鬆連線上遠端環境,在本地進行開發。

下面來實戰。

安裝vs code insiders

需要先安裝最新的內部體驗版,https://code.visualstudio.com/insiders/

然後安裝Remote Development外掛

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack

為了簡單起見,我們採用SSH模式。需要先在windows機器安裝OpenSSH

windows 10 安裝OpenSSH

使用 PowerShell 安裝 OpenSSH

若要安裝使用 PowerShell 的 OpenSSH,請首先以管理員身份啟動 PowerShell。 若要確保 OpenSSH 功能以安裝方式提供:

PowerShell複製

Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'

# This should return the following output:

Name  : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

然後,安裝伺服器和/或客戶端功能:

PowerShell複製

# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

# Both of these should return the following output:

Path          :
Online        : True
RestartNeeded : False

SSH 認證

  • 先ssh-keygen生車金鑰
  • 然後ssh-copy-id 到伺服器
 ssh-copy-id root@YOUR-SERVER-IP
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/jqpeng/.ssh/id_ed25519.pub"
The authenticity of host 'YOUR-SERVER-IP' can't be established.
ECDSA key fingerprint is SHA256:HRwsmslg5ge+JYcOjW6zRtUxrFeWJ5V2AojlIvLaykc.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filterout any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@YOUR-SERVER-IP's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@YOUR-SERVER-IP'"
and check to make sure that only the key(s) you wanted were added.

使用VS code inside 開發程式

準備工作:

  • 確保伺服器已有JDK,mvn,沒有的話先安裝好
  • 將程式碼簽出到伺服器一個目錄

開啟VS code,命令列:

選擇connect to host:

然後輸入root@YOUR_SERVETR_IP

回車,VS 會自動在伺服器準備相關環境。

搞定後,點選檔案開啟資料夾,VS Code會列出伺服器的目錄,選擇專案所在地址開啟即可。

接下來安裝必要的語言外掛,開啟一個java檔案,vs code會自動圖慘案安裝一些外掛,把java相關的安裝好:

除錯程式

開啟包含main的java檔案,點選除錯選單,會自動生成一個啟動檔案,配置下即可:

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以檢視現有屬性的描述。
    // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "AimindWebApplication",
            "request": "launch",
            "mainClass": "com.xxx.xxx.XXXWebApplication"
        }
    ]
}

然後啟動。

驚喜的發現,在main函式上方,自動出現了RUN|DEBUG,見下圖,點選debug即可啟動除錯

在除錯控制檯可以看到對應的輸出。

記憶體佔用

之前IDEA啟動除錯後,記憶體佔用2G+,VS code呢?400M+!

把耗費計算資源、記憶體的都放到伺服器上去執行了,本地只需要負責View,所以資源佔用極小。


作者:Jadepeng
出處:jqpeng的技術記事本--http://www.cnblogs.com/xiaoqi
您的支援是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。