Ubuntu 部署 Hyperledger Fabric 1.4.2
1 前言
Hyperledger fabric的fabric-sample運行需要三樣東西:
1 fabric-sample工程
2 fabric網(wǎng)絡(luò)的docker images
??主要有八個:hyperledger/fabric-javaenv像吻、hyperledger/fabric-ca、hyperledger/fabric-tools、hyperledger/fabric-ccenv、hyperledger/fabric-orderer、hyperledger/fabric-peer颅湘、hyperledger/fabric-zookeeper、hyperledger/fabric-kafka
3 可執(zhí)行二進制文件
??主要是兩個:hyperledger-fabric-ca-linux-amd64-1.4.2.tar.gz、
hyperledger-fabric-linux-amd64-1.4.2.tar.gz
從fabric1.4.1版本開始察署,fabric支持raft共識,1.4.0版本以前是kafka共識峻汉,其實在0.6版本以前fabric是支持BFT共識的贴汪。
從kafka共識向raft共識演進主要原因有兩點:
1 kafka運維不方便
??0.6版本fabric是基于BFT的共識,但是遇到了一些性能問題休吠,后面采用了kafka共識扳埂。fabric前期基于kafka,是一種簡單粗暴實現(xiàn)方法瘤礁,主要考慮的是快速實現(xiàn)阳懂,沒有考慮運維部署、易用的成本
[if !supportLists]l?? [endif]需要同時維護zk集群和kafka集群柜思,其中zk集群需要至少需要3個節(jié)點岩调,kafka至少需要4個節(jié)點
[if !supportLists]l?? [endif]zk集群和kafka集群,會保存日志幾個小時(可配置)赡盘,集群異常重啟号枕,每個節(jié)點的日志有所不同,有時會造成 排序節(jié)點異常
[if !supportLists]l?? [endif]排序節(jié)點同時需要一批組織維護陨享,需要構(gòu)建一個 order集群網(wǎng)絡(luò)葱淳,并持久化賬本
[if !supportLists]l?? [endif]整個排序集群钝腺,部署起來參數(shù)非常多,不易維護
[if !supportLists]l?? [endif]kafka和zookeeper的設(shè)計不適用于大型網(wǎng)絡(luò)
[if !supportLists]l?? [endif]在kafka共識模式中蛙紫,orderer與orderer之間不會互相直接建立連接拍屑,而是與kafka連接
2 raft為未來平滑演進fabric
BFT共識做準備
raft 和 bft是未來的趨勢,raft和bft都會部署在orderer節(jié)點上坑傅,兩者網(wǎng)絡(luò)架構(gòu)一樣僵驰,可以支持未來平滑演進
[if !supportLists]l?? [endif]在可信的環(huán)境下,基于性能的考慮唁毒,大家樂于考慮使用raft?
[if !supportLists]l? [endif]kafka缺乏信任的基礎(chǔ)蒜茴,選擇使用bft,聯(lián)盟鏈的節(jié)點有限浆西,性能也不會是大的問題
2 前期準備
系統(tǒng)軟件源改為 aliyun
?????? 方法:進入system setting?? 然后點擊software and update粉私,修改download from
[if !vml]
[endif]
安裝vim????????? sudo apt-get install vim
安裝git??????????? sudo apt-get install git
安裝curl????????? sudo apt-get install curl
安裝wget??????? sudo apt-get install wget
安裝 ssh
sudo apt-get
update
sudo apt-get
install ssh
3 環(huán)境準備
[if !supportLists]·????????[endif]安裝go1.11 以及以上版本
[if !supportLists]·????????[endif]安裝docker
17.06.2-ce 以及以上版本
[if !supportLists]·????????[endif]安裝docker-compose?1.14.0以及以上版本
[if !supportLists]·????????[endif]git 拉下fabric-sample 項目
[if !supportLists]·????????[endif]下載docker
image鏡像
3.1 GO安裝
3.1.1下載壓縮包??
無法翻墻 可以去這個網(wǎng)站,
https://studygolang.com/dl
cd ~?
wget https://studygolang.com/dl/golang/go1.12.1.linux-amd64.tar.gz??? //下載安裝go 1.12.1
tar -zxvf go1.12.1.linux-amd64.tar.gz -C/usr/local??? //解壓
3.1.2 配置環(huán)境變量
sudo gedit /etc/profile, 追加以下配置到環(huán)境變量
export?GOROOT=/usr/local/go
export?PATH=$PATH:/usr/local/go/bin
export? GOPATH=/opt/go
export? PATH=$PATH:/opt/go/bin
source /etc/profile //立即適應(yīng)新環(huán)境
測試
輸入 go version,成功查看版本信息雜表示配置成功近零。
3.2 安裝Docker-CE
1 如果已經(jīng)安裝docker诺核,但是沒有docker 17.06.2-ce 以及以上版本
sudo apt-getremove docker docker-engine docker.io? //先卸載
2 前置工作
sudo apt-get
updatesudo apt-get
install apt-transport-https ca-certificates curl software-properties-commoncurl -fsSL
https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
3 加入apt倉庫中
sudo
add-apt-repository \
?? "deb [arch=amd64]https://download.docker.com/linux/ubuntu \
?? $(lsb_release -cs) \
?? stable"sudo apt-get
update
4 安裝 Docker-ce
sudo apt-get
install docker-ce
5 添加阿里云的docker hub鏡像
sudo mkdir
-p /etc/docker
sudo gedit /etc/docker/daemon.json
6 將下列內(nèi)容寫入deamon.json文件中,并保存退出
{
?"registry-mirrors":["https://obou6wyb.mirror.aliyuncs.com"]
}
7 接下來執(zhí)行
sudo
systemctl daemon-reload
sudo
systemctl restart docker
8 將當前用戶添加到 Docker
Group(支持不使用 sudo 執(zhí)行 docker 命令)
sudo usermod-aG docker xxx?? (xxx為當前用戶名)
sudo chmod
-R 777 /var/run/docker.sock
9 測試
docker version
3.3 安裝Docker-Compose
1 安裝
sudo apt-get
install python-pip
sudo pip
install docker-compose
2 測試
docker-compose
version
4下載 fabric 及其鏡像
4.1 創(chuàng)建并進入工作目錄
mkdir -p
$GOPATH/src/github.com/hyperledger/
cd
$GOPATH/src/github.com/hyperledger/
4.2 克隆源碼
git clone
https://github.com/hyperledger/fabric.git
cd fabric
git checkout
v1.4.2
4.3 下載鏡像和fabric-samples和二進制文件
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/
執(zhí)行scripts目錄中的bootstrap.sh腳本會自動下載fabric-samples和fabric鏡像
./bootstrap.sh1.4.2 1.4.2 0.4.15? //腳本的使用方法:bootstrap.sh [Fabric版本 [CA版本 [第三方版本]]] [選項]
由于鏡像較大久信,此過程需要較長時間完成窖杀。腳本完成后會發(fā)現(xiàn)當前目錄有了fabric-samples目錄,之后執(zhí)行:
docker
images
當出現(xiàn)下列內(nèi)容時說明鏡像部署成功:
hyperledger/fabric-javaenv???? 1.4.2?????????????? 1cd707531ce7??????? 4 weeks ago???????? 1.76GB
hyperledger/fabric-javaenv???? latest????????????? 1cd707531ce7??????? 4 weeks ago???????? 1.76GB
hyperledger/fabric-ca????????? 1.4.2?????????????? f289675c9874??????? 4 weeks ago???????? 253MB
hyperledger/fabric-ca????????? latest????????????? f289675c9874??????? 4 weeks ago???????? 253MB
hyperledger/fabric-tools?????? 1.4.2?????????????? 0abc124a9400??????? 4 weeks ago???????? 1.55GB
hyperledger/fabric-tools?????? latest????????????? 0abc124a9400??????? 4 weeks ago???????? 1.55GB
hyperledger/fabric-ccenv?????? 1.4.2?????????????? fc0f502399a6??????? 4 weeks ago???????? 1.43GB
hyperledger/fabric-ccenv?????? latest????????????? fc0f502399a6??????? 4 weeks ago???????? 1.43GB
hyperledger/fabric-orderer???? 1.4.2?????????????? 362021998003??????? 4 weeks ago???????? 173MB
hyperledger/fabric-orderer???? latest????????????? 362021998003??????? 4 weeks ago???????? 173MB
hyperledger/fabric-peer??????? 1.4.2?????????????? d79f2f4f3257??????? 4 weeks ago???????? 178MB
hyperledger/fabric-peer??????? latest????????????? d79f2f4f3257??????? 4 weeks ago???????? 178MB
hyperledger/fabric-zookeeper?? 0.4.15????????????? 20c6045930c8??????? 5 months ago??????? 1.43GB
hyperledger/fabric-zookeeper?? latest????????????? 20c6045930c8??????? 5 months ago??????? 1.43GB
hyperledger/fabric-kafka??????0.4.15????????????? b4ab82bbaf2f??????? 5 months ago??????? 1.44GB
hyperledger/fabric-kafka?????? latest????????????? b4ab82bbaf2f??????? 5 months ago??????? 1.44GB
5 快速部署網(wǎng)絡(luò)
5.1 復(fù)制二進制文件
將二進制文件復(fù)制到 usr/local/bin 目錄
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin
sudo cp -r .
/usr/local/bin
這樣就可以到處使用這些可執(zhí)行二進制文件了裙士∪肟停可以看到8個二進制文件
configtxgen???cryptogen? fabric-ca-client? orderer
configtxlator?discover?? idemixgen???????? peer
[if !vml]
[endif]
這幾個二進制文件主要是從hyperledger-fabric-ca-linux-amd64-1.4.2.tar.gz和hyperledger-fabric-linux-amd64-1.4.2.tar.gz解壓后得到的
如果下載緩慢,可以在百度云盤下載
https://pan.baidu.com/s/1YfSwjrEybfozhqFYw7yCPA
5.2 啟動和測試網(wǎng)絡(luò)
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
./byfn.sh -mgenerate -o etcdraft? //使用raft共識
./byfn.sh up-o etcdraft? //使用raft共識
啟動和測試日志:
[if !vml]
[endif]
5.3 關(guān)閉網(wǎng)絡(luò)
./byfn.sh down
6 解釋bootstrap.sh
6.1 腳本的使用方法
bootstrap.sh [Fabric版本 [CA版本 [第三方版本]]] [選項]
6.2 bootstrap.sh做了什么
bootstrap主要是安裝sample 腿椎,fabric 網(wǎng)絡(luò)的二進制文件桌硫,以及 docker 鏡像(Install Samples, Binaries and Docker Images)
打開 bootstrap.sh 文件 大致查看以下內(nèi)容
[if !supportLists]·????????[endif]dockerFabricPull: 下載 fabric-peer fabric-orderer
fabric-ccenv fabric-javaenv fabric-tools 鏡像
[if !supportLists]·????????[endif]dockerThirdPartyImagesPull: 下載 couchdb kafka zookeeper 鏡像
[if !supportLists]·????????[endif]dockerCaPull: 下載 fabric-ca 鏡像
[if !supportLists]·????????[endif]samplesInstall: git clone -b master https://github.com/hyperledger/fabric-samples.git
[if !supportLists]·????????[endif]binariesInstall: 安裝 hyperledger-fabric
hyperledger-fabric-ca 二進制
總結(jié):
if [ "$SAMPLES" == "true" ]; then
? echo
? echo "Installing hyperledger/fabric-samples
repo"
? echo
? samplesInstall
fi
if [ "$BINARIES" == "true" ]; then
? echo
? echo "Installing Hyperledger Fabric
binaries"
? echo
? binariesInstall
fi
if [ "$DOCKER" == "true" ]; then
? echo
? echo "Installing Hyperledger Fabric docker
images"
? echo
? dockerInstall
fi
1. 安裝hyplerledger fabric-sample
2. 安裝hyplerledger fabric binaries
文件介紹類型依賴
peer賬本(對等)節(jié)點程序,負責維護賬本數(shù)據(jù)啃炸,執(zhí)行鏈碼服務(wù)core.yaml
orderer共識服務(wù)程序铆隘,負責打包交易,使用通道訂閱給其他賬本節(jié)點服務(wù)orderer.yaml
cryptogen生成 x509標準的證書以及密鑰工具crypto-config.yaml
configtxgen生成共識服務(wù)啟動以及通道創(chuàng)建所需配置數(shù)據(jù)工具configtx.yaml
configtxlator用來將通道配置信息轉(zhuǎn)換為可讀形式工具
discoverfabric discovery 服務(wù)的命令行客戶端服務(wù)
fabric-ca-clienthyperledger fabric 證書認證客戶端工具
idemixgenHyperledger Fabric 生成關(guān)鍵材料時被用于身份混淆MSP的工具工具
3. 安裝hyplerledger fabric dockerimages
打開 fabric-samples/bin/get-docker-images.sh 可以發(fā)現(xiàn)南用,總共下載的鏡像包括如下:
鏡像介紹
fabric-peer賬本(對等)節(jié)點的鏡像
fabric-orderer共識服務(wù)節(jié)點的鏡像
fabric-ccenv鏈碼環(huán)境鏡像
fabric-tools工具庫鏡像
fabric-kafka分布式消息隊列鏡像
fabric-zookeeper分布式協(xié)調(diào)服務(wù)鏡像
fabric-couchdb狀態(tài)存儲庫鏡像
fabric-javaenv
6.3 有多種方法可以運行bootstrap.sh腳本
1 在hyperledger/fabric/scripts下可以找到
$GOPATH/src/github.com/hyperledger/fabric/scripts
2 通過https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
自動安裝腳本的url為https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh膀钠,縮略后的url為http://bit.ly/2ysbOFE,如果后面縮略的網(wǎng)址打不開训枢,請使用前者代替托修。
curl -sSL
https://bit.ly/2ysbOFE | bash -s -- 1.4.2 1.4.2 0.4.15
3 將https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh復(fù)制到本地使用
curl -sS
https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
-o ./scripts/bootstrap.sh
# Change file mode to executable
chmod +x ./scripts/bootstrap.sh
# Download binaries and docker images
./scripts/bootstrap.sh
1.4.2 1.4.2 0.4.15
7 二進制可執(zhí)行文件
7.1 通過bootstrap.sh安裝
Bootstrap.sh首先會從github上將fabric-samples下載下來,然后會從https://nexus.hyperledger.org下載可執(zhí)行二進制文件恒界。如下圖睦刃,并解壓到fabric-samples/bin目錄下
[if !vml]
[endif]
https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.4.2/hyperledger-fabric-linux-amd64-1.4.2.tar.gz
這兩個文件不是很好下載,可以通過windows代理下載十酣,下載后手動放入$GOPATH/src/hyperledger/fabric/scripts/fabric-samples目錄下涩拙,然后回到scripts目錄下繼續(xù)執(zhí)行bootstrap.sh腳本
7.2 編譯安裝
進入hyperledger/fabric目錄际长,自行編譯這些工具:make cryptogen configtxgen configtxlator peer orderer peer-docker
orderer-docker tools-docker,在.build/bin/目錄下就是我們需要的工具兴泥,編譯也會生成docker images
參考:
1https://hyperledgercn.github.io/hyperledgerDocs/build_network_zh/
2https://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html
3 https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html
4 https://blog.csdn.net/smallone233/article/details/86569536
5 https://www.cnblogs.com/llongst/p/9482038.html
6 一個動畫演示raft算法
http://thesecretlivesofdata.com/raft/?
7 https://blog.csdn.net/sinat_36742186/article/details/88390019
8 https://blog.csdn.net/sinat_36742186/article/details/80809954
9 https://blog.csdn.net/diligent_lee/article/details/79098302
10 https://blog.csdn.net/so5418418/article/details/78355868
11https://www.cnblogs.com/informatics/p/8276172.html
12 http://www.reibang.com/p/e5ec0b4e8075