天天看點

如何使用​win10内置的linux系統啟動spring-boot項目

1.安裝win10内置linux子系統

1.1.安裝linux子系統

1.1.1.更新或者重裝linux系統

1.系統要求

需要更新win10到一周年紀念版,或者 重裝系統

2.重裝系統

筆者結果測試發現下面的系統重裝後可以啟用linux子系統

cn_windows_10_enterprise_version_1607_updated_jul_2016_x64_dvd_9057083.iso

cn_windows_10_multi-edition_vl_version_1709_updated_sept_2017_x64_dvd_100090774.iso

1.1.2.啟用linux子系統

step1.啟用開發人員模式

(16215版之後不再需要開發人員模式)

設定–>更新和安全–>針對開發人員

如何使用​win10内置的linux系統啟動spring-boot項目

step2.啟用linux子系統

控制台–>程式和功能–>啟用或關閉windows功能

如何使用​win10内置的linux系統啟動spring-boot項目

1.1.3.線上安裝ubuntu系統

打開Microsoft Store安裝ubuntu系統

如何使用​win10内置的linux系統啟動spring-boot項目

1.1.4.離線安裝ubuntu系統

下載下傳安裝包,下載下傳自己想要安裝的Linux系統。

參考連結:https://docs.microsoft.com/en…

下載下傳Ubuntu 16.04.

Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1604 -OutFile Ubuntu.appx -UseBasicParsing

也可以使用curl下載下傳,但是需要先安裝curl

curl.exe -L -o ubuntu-1604.appx https://aka.ms/wsl-ubuntu-1604

安裝

Add-AppxPackage .\app_name.appx

1.1.5.啟動ubuntu系統

啟動ubuntu系統

安裝完成後打開搜尋菜單,輸入系統名字搜尋啟動

如何使用​win10内置的linux系統啟動spring-boot項目

輸出需要設定的使用者和密碼

如何使用​win10内置的linux系統啟動spring-boot項目

你相信嗎,僅僅隻有15.4MB

如何使用​win10内置的linux系統啟動spring-boot項目

安裝目錄

C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc

home目錄

C:\Users\Administrator\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home

1.2.開啟ssh-server

1.2.1.互相複制檔案

ubuntu的bash視窗窗與在xshell不同,通過shift+v無法黏貼,并且在輸入界面滑鼠右鍵無法使用。粘貼檔案的方法如下

右擊标題欄==> Edit(編輯) ==> Mark/Paste(标記并複制/粘貼)操作

如何使用​win10内置的linux系統啟動spring-boot項目

這樣還是很麻煩,可以調整對話框,在彈出的菜單中有一個Properties對話框屬性菜單,點選進入下圖

如何使用​win10内置的linux系統啟動spring-boot項目

勾選快速編輯、快速插入模式,這兩種模式支援滑鼠右鍵選中,左鍵黏貼操作。

最後,最好的方式是,在ubuntu上啟動ssh服務,通過xshell連接配接上操作會更好。

1.2.2.修改apt源

參考連結

https://developer.aliyun.com/…

用你熟悉的編輯器打開:

/etc/apt/sources.list

替換預設的archive.ubuntu.com為mirrors.aliyun.com

修改後的内容

ubuntu18.04

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

ubuntu 20.04(focal)

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

更新

apt-get update

1.2.3.啟用ssh

1.安裝ssh

apt-get -y install openssh-server

2.修改配置檔案,一般不需要修改

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
vi /etc/ssh/sshd_config           

複制

# 修改端口号,避免和 Windows ssh 服務端口沖突
Port 2222
#StrictModes yes # 注釋
# 使用密碼登入
PasswordAuthentication yes 
# 允許使用 root 使用者登入
PermitRootLogin yes           

複制

3.設定root賬号密碼

sudo -s
passwd root           

複制

4.啟動ssh

service ssh start

5.如果提示“sshd error: could not load host key”,則用下面的指令重新生成

rm /etc/ssh/ssh*key
dpkg-reconfigure openssh-server           

複制

使用xshell連接配接127.0.0.1輸入賬号和密碼即可

1.2.4.配置PS1變量

自行百度什麼事linux的PS1變量

export PS1=”\[\e[1;32;40m\][\u@\h \W]\\$ \[\e[m\]”

2.搭建開發環境

2.1.搭建java開發環境

2.1.1.安裝java和maven

mkdir /usr/java
tar -xf jdk-8u211-linux-x64.tar.gz -C /usr/java/
mdkir /usr/maven
tar -xf litongjava-apache-maven-3.3.9.tar.gz -C /usr/maven/           

複制

配置環境變量

export JAVA_HOME=/usr/java/jdk1.8.0_211
export M2_HOME=/usr/maven/apache-maven-3.3.9
export PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH           

複制

配置maven使用windows的maven本地庫目錄,maven本地庫在linux中對應的目錄是/mnt/d/dev_mavenRepository/

settings.xml的内容如下

cat /usr/maven/apache-maven-3.3.9/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>/mnt/d/dev_mavenRepository/</localRepository>
 <pluginGroups> 
 </pluginGroups>
 <proxies>
 </proxies>
 <servers>
 </servers>
 <mirrors>
 <mirror>
  <id>aliyun</id>
  <name>aliyun</name>
  <mirrorOf>*</mirrorOf>
  <url>https://maven.aliyun.com/repository/public</url>
 </mirror>
 </mirrors>
 <profiles>
 </profiles>
</settings>           

複制

2.1.2.打包和啟動spring-boot項目

進行項目目錄執行下面的指令打包項目

mvn clean package -DskipTests

進入target目錄啟動項目

cd target/

java -jar *.jar

可以明顯的看出,java程式使用了近1G的記憶體

如何使用​win10内置的linux系統啟動spring-boot項目