文章很長,建議收藏起來,慢慢讀! 備注:持續更新中.....
- 瘋狂創客圈 經典圖書 : 《Netty Zookeeper Redis 高並發實戰》 面試必備 + 大廠必備 + 漲薪必備
- 瘋狂創客圈 經典圖書 : 《SpringCloud、Nginx高並發核心編程》 面試必備 + 大廠必備 + 漲薪必備
- 瘋狂創客圈 價值1000元 百度網盤資源大禮包,免費拿 【博客園總入口 】
價值連城:2021春招月薪過5萬 面試題 總系列
搞定下面這些面試題,2021春招月薪過5萬(猛!) | 阿里、京東、美團、頭條.... 隨意挑、橫着走!!! |
---|---|
Java基礎 | |
1: JVM面試題(史上最強、持續更新、吐血推薦) | https://www.cnblogs.com/crazymakercircle/p/14365820.html |
2:Java基礎面試題(史上最全、持續更新、吐血推薦) | https://www.cnblogs.com/crazymakercircle/p/14366081.html |
3:死鎖面試題(史上最強、持續更新) | [https://www.cnblogs.com/crazymakercircle/p/14323919.html] |
4:設計模式面試題 (史上最全、持續更新、吐血推薦) | https://www.cnblogs.com/crazymakercircle/p/14367101.html |
5:架構設計面試題 (史上最全、持續更新、吐血推薦) | https://www.cnblogs.com/crazymakercircle/p/14367907.html |
還有 10 幾篇 篇價值連城 的面試題 | 具體..... 請參見【 瘋狂創客圈 高並發 總目錄 】 |
萬字長文: 瘋狂創客圈 springCloud 高並發系列
springCloud 高質量 博文 | |
---|---|
nacos 實戰(史上最全) | sentinel (史上最全+入門教程) |
springcloud + webflux 高並發實戰 | Webflux(史上最全) |
SpringCloud gateway (史上最全) | |
還有 10 幾篇 萬字長文 的高質量 博文 | 具體..... 請參見【 瘋狂創客圈 高並發 總目錄 】 |
1 什么是vagrant
必先利其器,開發環境 和 開發工具 就是 我們開發人員的劍,所以我們需要一個快並且好用的劍
Vagrant:是一個比較流行的虛擬機管理軟件,使用Vagrant 可以讓我們用命令直接從雲上下載虛擬機的鏡像,然后進行創建和管理等。
剛開始做開發的時候的都是把開發環境 配置在 自己的電腦上,隨着后面我們接觸的東西越來越多,慢慢的電腦上都是各種環境了,php,java,python,nodejs等等,非常麻煩,並且經常由於某種原因就把電腦重裝了,ORZ,所以環境都要重來。所以打造一個屬於自己並且可以移動的環境是非常重要的。
vagrant就是一款構建虛擬開發環境的工具,支持window,linux,mac,總有一款適合你。並且vagrant 可以把配置好的環境打包成一個box,分享給其他人直接使用,非常方便
vagrant 是一個很適合開發者的虛擬環境部署工具,本身集成了主流的虛擬器管理工具,支持 vmvare 和 virtualbox。
vagrant 的精髓在一個 Vagrantfile 里面,和 docker 的 Dockerfile 功能上一樣。我們只需要把需要安裝部署的步驟寫在 Vagrantfile 里面,便可以實現輕松部署。vagrant 還支持把當前系統做成一個.box 后綴命名的鏡像,類似 docker 的 image,可輕松實現環境的移植。
所以,使用vagrant可以在運行着多台vm的系統上定義復雜的虛擬框架。可以通過 Vagrant 封裝一個 Linux 的開發環境,分發給團隊成員。成員可以在自己喜歡的桌面系統(Mac/Windows/Linux)上開發程序,代碼卻能統一在封裝好的環境里運行,非常霸氣。是不是很酷?
2 准備工作:
安裝VirtualBox 、vagrant、GitBash
下載安裝 VirtualBox ,下載安裝 Vagrant ,下載更多不同系統甚至是已經配置好環境直接可以用的box,雖然可以直接在Vagrant直接使用網址,由Vagrant自動下載安裝,但是考慮到網絡情況,還是建議自行先下載好。
還可以在 http://www.vagrantbox.es/ 這里下載更多不同系統甚至是已經配置好環境直接可以用的box,雖然可以直接在Vagrant直接使用網址,由Vagrant自動下載安裝,但是考慮到網絡情況,還是建議自行先下載好。
virtualbox 安裝
VirtualBox 是一個免費開源的虛擬機,相對 VMware 來說更加小巧,個人比較喜歡。
雖然 Vagrant 也支持 VMware,不過 VMware 是收費的,對應的 Vagrant 版本也是收費的。
瘋狂創客圈 百度網盤,也統一提供了下載
注意:virtualbox 安裝時,可以帶一個參數:
VirtualBox-5.0.11-104101-Win.exe -msiparams NETWORKTYPE=NDIS5
主要是解決后續可能出現的一個錯誤
Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter' (VERR_INTNET_FLT_IF_NOT_FOUND).
Failed to attach the network LUN (VERR_INTNET_FLT_IF_NOT_FOUND).12
解決辦法
在百度找了好久,也重裝過vbox,但是仍然有問題。
后來在這個網址https://stackoverflow.com/questions/33725779/failed-to-open-create-the-internal-network-vagrant-on-
找到一個回復,里面提到
There is a known issue with the new NDIS6 driver, you can install it to use the old NDIS5 driver
Uninstall Virtualbox and try reinstalling it with a parameter
> VirtualBox-5.0.11-104101-Win.exe -msiparams NETWORKTYPE=NDIS5
This worked for me.
嘗試使用命令安裝,果然解決了問題。應該就是這個 NDIS6搞的鬼,換回NDIS5就沒問題了。
vagrant 安裝
在 Vagant 網站下載最新的版本,根據自己的操作系統選擇對應的版本下載即可。
瘋狂創客圈 百度網盤,也統一提供了下載
注意,Vagrant 是沒有圖形界面的,所以安裝完成后也沒有桌面快捷方式。具體使用方法,接下來會詳細說明。
GitBash
GitBash是windows下的Unix shell,方便向Vagrant輸入指令。
https://git-scm.com/download/win
一般的java開發,都會使用git進行代碼同步,所以此工具默認是已經安裝了的
空間:
15G 的硬盤空間
下載需要的 box(鏡像)
基礎box
此外,還得下載官方封裝好的基礎鏡像:
Ubuntu precise 32 VirtualBox http://files.vagrantup.com/precise32.box
Ubuntu precise 64 VirtualBox http://files.vagrantup.com/precise64.box
如果你要其他系統的鏡像,可以來這里下載:http://www.vagrantbox.es/
網友提供的centos 7.2網盤地址:https://pan.baidu.com/s/15S2OZq37FcL9RWWSTWntIw 提取碼:3xb0
下載預裝了很多組件的 springcloud.box 鏡像,
瘋狂創客圈網盤,為大家准備了springcloud.box鏡像,里邊預裝了java 、 redis 、zookeeper、kafka、Eureka、springcloud config 等必須的組件。網盤地址請參見【博客園總入口 】
3 使用vagrant導入Java開發環境
第一步,新建目錄
E:\virtual\work
選擇新建立的文件夾作,右鍵 —> Git Bash Here,在Git Bash打開當前目錄
在 Git Bash 中可以看到,當前的linux 格式目錄,執行指令如下:
$ pwd
/e/virtual/work
第二步,添加鏡像到 Vagrant
查看鏡像
$ vagrant box list
centos (virtualbox, 0)
springcloud-dev (virtualbox, 0)
刪除鏡像
vagrant add remove 刪除 box,自動幫你生成vagrantfile
vagrant box remove springcloud-dev
添加鏡像
vagrant box add springcloud-dev /e/virtual/workcluster/springcloud-dev-10.box
springcloud-dev 表示指定名稱,如果使用base,之后可以直接使用
/springcloud-dev-10.box 是box的文件名,這里是本地保存box的路徑。也可以是可以下載box的網址,如果是網址的話,Vagrant會自動啟動下載。
$ vagrant box add centos /e/virtual/workcluster/springcloud-dev-10.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'springcloud-dev ' (v0) for provider:
box: Unpacking necessary files from: file:///E:/virtual/workcluster/springcloud-dev-10.box
box:
==> box: Successfully added box 'springcloud-dev ' (v0) for 'virtualbox'!
第三步,初始化虛擬機
設置好box之后,在當前工作目錄運行
$ vagrant init springcloud-dev
centos為初始化的時候需要指定box的名稱。執行的結果如下:
$ vagrant init centos
AVagrantfile
has been placed in this directory. You are now
ready tovagrant up
your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com
for more information on using Vagrant.
你也可以使用已有的目錄,切換到開發目錄里,用 centos 鏡像初始化當前目錄。初始化鏡像之后,在當前目錄生成一個虛擬機的配置文件 Vagrantfile,通過該配置文件,可以配置端口映射、虛擬機的IP、目錄映射等。通過文本編輯器打開Vagrantfile可以進行一些進一步的常用配置,如果配置登錄用戶名稱和密碼,虛擬機的ip如下:
config.ssh.username = "root"
config.ssh.password = "vagrant"
config.vm.network :private_network, ip: "192.168.68.128"
重啟虛擬機,這樣我們就能用 192.168.233.128 訪問這台機器了,你可以把 IP 改成其他地址,只要不產生沖突就行。
下面是尼恩的配置文件,有三個節點的虛擬機集群,的完整配置,供大家參考,或者直接使用,具體如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
(1..3).each do |i|
config.vm.define vm_name = "cdh#{i}" do |config|
config.vm.provider "virtualbox" do |v|
# v.customize ["modifyvm", :id, "--name", vm_name, "--memory", "4096",'--cpus', 1]
v.customize ["modifyvm", :id, "--name", vm_name]
# v.customize ["modifyvm", :id, "--memory", "4096"]
v.customize ["modifyvm", :id, "--memory", "8192"]
v.customize ["modifyvm", :id, "--cpus", "2"]
end
config.vm.box = "springcloud-dev"
config.vm.hostname =vm_name
config.ssh.username = "root"
config.ssh.password = "vagrant"
# config.ssh.shell = "powershell"
#config.ssh.shell = "bash -l"
config.vm.network :private_network, ip: "192.168.56.12#{i}"
config.vm.provision :shell, :path => "bootstrap.sh"
end
end
end
第四步,啟動虛擬機
使用如下命令:
$ vagrant up # 啟動全部虛擬機節點
$ vagrant up cdh1 # 啟動名字為cdh1的虛擬機節點
你會看到終端顯示了啟動過程,啟動完成后,我們就可以用 SSH 登錄虛擬機了,剩下的步驟就是在虛擬機里配置你要運行的各種環境和參數了。
$ vagrant ssh # SSH 登錄
$ cd /vagrant # 切換到開發目錄,也就是宿主機上的 E:\virtual\work
Windows 用戶注意:Windows 終端並不支持 ssh,所以需要安裝第三方 SSH 客戶端,比如:Putty、Cygwin 、Git Bash等。
有關初始賬戶和密碼:
賬戶 | 密碼 |
---|---|
vagrant | vagrant |
root | vagrant |
可以使用其他的終端工具,通過root訪問虛擬機了
啟動過程中的錯誤
1 適配器創建失敗 : Failed to create the host-only adapter
VBoxManage.exe: error: Failed to create the host-only adapter
VBoxManage.exe: error: Assertion failed: [!aInterfaceName.isEmpty()] at 'F:\tinderbox\win-5.1\src\VBox\Main\src-server\HostNetworkInterfaceImpl.cpp' (74) in long __cdecl HostNetworkInterface::init(class com::Bstr,class com::Bstr,class com::Guid,enum __MIDL___MIDL_itf_VirtualBox_0000_0000_0038).
VBoxManage.exe: error: Please contact the product vendor!
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component HostNetworkInterfaceWrap, interface IHostNetworkInterface
VBoxManage.exe: error: Context: "enum RTEXITCODE __cdecl handleCreate(struct HandlerArg *)" at line 94 of file VBoxManageHostonly.cpp
網上的大部分的解決方案為:
在virtualbox 管理 - > 全局設定 -> 網絡 - > tab 僅主機網絡 - > 添加
正常情況下是可以添加成功的,但是有可能報
Could not find Host Interface Networking driver!Please reinstall(找不到主機接口網絡驅動程序!請重新安裝)
如果第一步報錯,這是因為之前卸載虛擬網卡的時候把驅動也給刪除了。
可以在安裝virtualbox目錄…/Oracle VM VirtualBox中的 drivers\ network\ netadp6目錄下有三個文件
VBoxNetAdp6.cat
VBoxNetAdp6.inf
VBoxNetAdp6.sys
這就是virtualbox虛擬網卡的驅動,右擊VBoxNetAdp6.inf,右鍵點安裝即可。
然后再做第二步。應該可以完成了。
4 打包分發
當你配置好開發環境后,退出並關閉虛擬機。在終端里對開發環境進行打包:
$ vagrant package
打包完成后會在當前目錄生成一個 package.box
的文件,將這個文件傳給其他用戶,其他用戶只要添加這個 box 並用其初始化自己的開發目錄就能得到一個一模一樣的開發環境了。
執行的過程如下:
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
==> default: Forcing shutdown of VM...
$ vagrant package --output springcloud-dev.box
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: E:/virtual/work/springcloud-dev
$ vagrant box list
centos (virtualbox, 0)
命令的具體說明:
vagrant package -hUsage: vagrant package [options] [name]
Options:
--base NAME virtualbox程序里面的虛擬機的名稱,不是box的名字也不是Vagrantfile里面的虛擬機名稱.默認是打包當前目錄下面的虛擬機。
--output NAME 要打包成的box名稱,不會自動添加.box后綴,要手動加.默認值package.box--include FILE... 打包時包含的文件名,你可以把.box文件理解為一個壓縮包
--vagrantfile FILE 打包時包含的Vagrantfile文件,原理和上面類似
-h, --help Print this help
例子:vagrant package –base virtualbox_vm_name –output newbox_name.box
5 通過Vagrantfile進行虛擬機配置:
網絡配置:
Vagrant的網絡有三種模式
1、較為常用是端口映射,就是將虛擬機中的端口映射到宿主機對應的端口直接使用 ,在Vagrantfile中配置:
config.vm.network :forwarded_port, guest: 80, host: 8080
guest: 80 表示虛擬機中的80端口, host: 8080 表示映射到宿主機的8080端口。
2、如果需要自己自由的訪問虛擬機,但是別人不需要訪問虛擬機,可以使用private_network,並為虛擬機設置IP ,在Vagrantfile中配置:
config.vm.network :private_network, ip: "192.168.1.104"
192.168.1.104 表示虛擬機的IP,多台虛擬機的話需要互相訪問的話,設置在相同網段即可
3、如果需要將虛擬機作為當前局域網中的一台計算機,由局域網進行DHCP,那么在Vagrantfile中配置:
config.vm.network :public_network
目錄映射:
默認的,vagrant將共享你的工作目錄(即Vagrantfile所在的目錄)到虛擬機中的/vagrant,所以一般不需配置即可,如你需要可配置:
Vagrant.configure("2") do |config|
config.vm.synced_folder "src/", "/srv/website"
end
"src/":物理機目錄;"/srv/website"虛擬機目錄
目錄映射(文件夾)詳細說明如下:
既然是開發環境,那么開發工作肯定還是需要在本地完成,而不是都要進到虛擬機中去完成,虛擬機就好好在后台運行服務就好了,不然就本末倒置了,所以這里就需要使用目錄映射功能,將本地的目錄映射到虛擬機的對應目錄。
默認情況下,當前的工作目錄,會被映射到虛擬機的 /vagrant 目錄,當前目錄下的文件可以直接在 /vagrant 下進行訪問,當然也可以在通過 ln 創建軟連接,如
ln -fs /vagrant/wwwroot /var/www
來進行目錄映射,當然,從自動化配置的角度,能不進系統就不需要進系統,所以在Vagrant也可以進行目錄映射的操作:
config.vm.synced_folder "wwwroot/", "/var/www"
前面的參數 “wwwroot/” 表示的是本地的路徑,這里使用對於工作目錄的相對路徑,這里也可以使用絕對路徑,比如: “d:/www/”
后面的參數 “/var/www” 表示虛擬機中對應映射的目錄。
當在啟動Vagrant后,對於虛擬機有進行過安裝環境相關的配置,如果並不希望寫在Vagrant的啟動shell里面每次都重新安裝配置一遍,可以將當前配置好的虛擬機打包成box,
注:如果網絡模式中使用 private_network 的話,在打包之前需要清除一下private_network的設置,避免不必要的錯誤:
sudo rm -f /etc/udev/rule.d/70-persistent-net.rules
制作完成之后直接將box文件拿到其他計算機上配置即可使用。
更多信息可以參考官方文檔:http://docs.vagrantup.com/v2/
啟動的時自運行需要的shell命令或腳本
內部腳本:
Vagrant::Config.run do |config|
config.vm.provision :shell, :inline => "echo abc > /tmp/test"
end
外部腳本:
Vagrant.configure("2") do |config|
config.vm.provision :shell, :path => "script.sh" #腳本的路徑相對於項目根,也可使用絕對路徑
end
附:簡單的vagrantfile文件
Vagrant.configure(2) do |config|
config.ssh.username = "root"
config.ssh.password = "vagrant"
config.vm.box = "centos"
config.vm.network "private_network", ip: "192.168.68.128"
config.vm.synced_folder "/home/wangkongming/files/works/code/kfz-pm", "/data/webroot/pmv2"
end
6 vagrant 常用命令
官網文檔:https://docs.vagrantup.com/v2/getting-started/index.html
vagrant init 初始化vagrantfile
vagrant add remove 刪除 box,自動幫你生成vagrantfile
vagrant box remove springcloud-dev
vagrant add box 添加 box,自動幫你生成vagrantfile
vagrant box add springcloud-dev /e/virtual/workcluster/springcloud-dev-3.box
vagrant halt 關閉虛擬機
vagrant destroy 銷毀虛擬機
vagrant ssh 連接虛擬機
vagrant reload 修改vagarntfile文件后,重啟虛擬機,重新加載
vagrant suspend 暫時掛起虛擬機
vagrant status 查看虛擬機運行狀態
vagrant package 打包 box文件
vagrant package --base cdh1 --output springcloud-dev-4.box
vagrant box list 首先查看已經添加的box:
$ vagrant box list
centos (virtualbox, 0)
vagrant destroy 銷毀當前虛擬機,不需要的虛擬機最好銷毀,釋放存儲空間
7 虛擬機集群
建立虛擬機目錄
先建立一個目錄: /e/virtual/workcluster,通過 Git Bash進入 /e/virtual/workcluster,然后
添加鏡像到 Vagrant
把下載的鏡像springcloud-dev.box放入目錄,添加鏡像到 Vagrant
vagrant box add springcloud-dev springcloud-dev.box
springcloud-dev 表示指定名稱,如果使用base,之后可以直接使用
初始化鏡像
$ vagrant init springcloud-dev
輸出以下日志:
A Vagrantfile
has been placed in this directory. You are now
ready to vagrant up
your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com
for more information on using Vagrant.
在當前目錄生成了 Vagrantfile 文件。
修改Vagrantfile
修改文件以下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
(1..3).each do |i|
config.vm.define vm_name = "cdh#{i}" do |config|
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", vm_name, "--memory", "2048",'--cpus', 1]
end
config.vm.box = "springcloud-dev"
config.vm.hostname =vm_name
config.ssh.username = "root"
config.ssh.password = "vagrant"
config.vm.network :private_network, ip: "192.168.56.12#{i}"
config.vm.provision :shell, :path => "bootstrap.sh"
end
end
end
上面的文件中定義了三個虛擬機,三個虛擬機的名字和 hostname 分別為cdh一、cdh二、cdh3,網絡使用的是 host-only
網絡。
在啟動成功以后,會運行 bootstrap.sh 腳本,你能夠編寫你本身的腳本。
bootstrap.sh 腳本
#!/usr/bin/env bash
# The output of all these installation steps is noisy. With this utility
# the progress report is nice and concise.
echo "Update /etc/hosts"
cat > /etc/hosts <<EOF
127.0.0.1 localhost
192.168.56.121 cdh1
192.168.56.122 cdh2
192.168.56.123 cdh3
EOF
echo "Disable iptables"
setenforce 0 >/dev/null 2>&1 && iptables -F
### Set env ###
echo "export LC_ALL=en_US.UTF-8" >> /etc/profile
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- 設置hosts文件
- 關掉防火牆
- 設置虛擬機時區
啟動集群
$ vagrant init springcloud-dev
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ vagrant up
....省略一大堆的輸出
8 推倒重來
在使用的過程中,如果遇到問題,不好解決。 可以直接推倒重來,做好鏡像的及時備份即可。
首先使用 vagrant status 查看一下狀態
$ vagrant status
Current machine states:
cdh1 running (virtualbox)
cdh2 not created (virtualbox)
cdh3 not created (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
然后使用vagrant halt 關機
$ vagrant halt cdh1
==> cdh1: Attempting graceful shutdown of VM...
The configured shell (config.ssh.shell) is invalid and unable
to properly execute commands. The most common cause for this is
using a shell that is unavailable on the system. Please verify
you're using the full path to the shell and that the shell is
executable by the SSH user.
可能關不了,因為ssl 壞了上不去了, 直接在virtualbox的界面去關,完了在看狀態;
$ vagrant status
Current machine states:
cdh1 poweroff (virtualbox)
cdh2 not created (virtualbox)
cdh3 not created (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
vagrant destroy 銷毀虛擬機
$ vagrant destroy
==> cdh3: VM not created. Moving on...
==> cdh2: VM not created. Moving on...
cdh1: Are you sure you want to destroy the 'cdh1' VM? [y/N] y
==> cdh1: Destroying VM and associated drives...
vagrant up cdh1 重新啟動
$ vagrant up cdh1
Bringing machine 'cdh1' up with 'virtualbox' provider...
==> cdh1: Importing base box 'springcloud-dev'...
==> cdh1: Matching MAC address for NAT networking...
。。。。。
cdh1: Update /etc/hosts
cdh1: Disable iptables
成功了
回到◀瘋狂創客圈▶
瘋狂創客圈 - Java高並發研習社群,為大家開啟大廠之門