快速搭建Fabric測試網(wǎng)絡(luò)(Docker in Ubantu 18.04 TLS)

這周查閱了大量文檔資料,終于在今天把整個(gè)網(wǎng)絡(luò)全部走通,包括通過源代碼部署和通過shell腳本一鍵部署,趟了大量的坑,依然不敢說是全部了解。但也有很多值得注意和以后需要求證的地方,這里寫下來備忘。

本文所述的內(nèi)容是在2019年1月8號(hào)開始的,于2019年1月10號(hào)結(jié)束。在Win10家庭版上裝的Ubantu 18.04版本的虛擬機(jī),文檔主要參考的是Fabric Release-1.4版本。另外,我喜歡Go,所以接下來關(guān)于鏈碼的演示,沒有nodejs、python和java什么事,想看這些,可以移步未來我寫的文章。Orz...

準(zhǔn)備工作(Prerequisites

在開始之前,檢查一下你的準(zhǔn)備開發(fā)或者測試Fabric的平臺(tái)是否有安裝如下軟件。哦,對了,由于我極度不喜歡使用安裝包,因?yàn)橄螺d的安裝包總是會(huì)隨著時(shí)間的流逝而過時(shí),所以我喜歡直接用命令來安裝,想下載安裝包的,可以去各自的官網(wǎng)上找找。

cURL

下載代碼以及docker鏡像用的。如果你還沒裝這個(gè),或者在本文中使用curl命令出現(xiàn)了錯(cuò)誤,可以考慮下載最新的版本。

$ # 檢查你的curl版本 - Check your curl version
$ curl --version
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.0g zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL

$ # 如果沒有就執(zhí)行下載 - Download if it doesn't exist
$ sudo apt-get update
$ sudo apt install curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
curl is already the newest version (7.58.0-2ubuntu3.5).
0 upgraded, 0 newly installed, 0 to remove and 26 not upgraded.

Docker and Docker Compose

orderer/peer節(jié)點(diǎn)啟動(dòng),鏈碼安裝/實(shí)例化/執(zhí)行/升級(jí)等用的。

$ # 檢查你的docker版本 - Check your docker version
$ docker --version
Docker version 18.09.0, build 4d60db4
$ docker-compose --version
docker-compose version 1.17.1, build unknown

$ # 如果沒有就執(zhí)行下載 - Download if it doesn't exist
$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce
$ # 國內(nèi)用戶因?yàn)榫W(wǎng)絡(luò)、未翻墻或者其他靈異事件可能hello-world執(zhí)行不了會(huì)失敗. 不過這不重要。 - It will be failed in China because the God is watching you. But it doesn't matter. :)
$ sudo docker container run hello-world
$ sudo apt install docker-compose

Go

主要是來寫鏈碼(chaincode)的。1.4版本的fabric要求go版本在1.11.x以上。

$ # 檢查你的go版本 - Check your go version
$ go version
go version go1.11.4 linux/amd64

$ # 如果沒有就執(zhí)行下載 - Download if it doesn't exist
$ sudo add-apt-repository ppa:longsleep/golang-backports
$ sudo apt-get update
$ sudo apt-get install golang-go

$ # 配置環(huán)境變量,這個(gè)很重要,目前來講,GOPATH是fabric必須有的。
$ vim ~/.bashrc
$ # 如果沒有裝vim可能會(huì)報(bào)錯(cuò),直接按提示安裝就好了 sudo apt install vim
$ # 打開文件后,按 i 進(jìn)入編輯模式,在文件末尾粘貼(Shift+Insert)上如下內(nèi)容:
export PATH=$PATH:$(go env GOPATH)/bin
export GOPATH=$(go env GOPATH)
export GOROOT=$(go env GOROOT)
export GOARCH=amd64
export GOOS=linux
$ # 粘貼完后,按Esc推出編輯模式,輸入命令:wq! 保存退出后執(zhí)行source命令讓其立即生效
$ source ~/.bashrc
$ 測試是否設(shè)置成功 - Check if it is successful
$ echo $GOPATH
/home/wff/go

構(gòu)建你的第一個(gè)網(wǎng)絡(luò)(Building Your First Network

構(gòu)建你的第一個(gè)網(wǎng)絡(luò)(byfn)方案提供了一個(gè)Hyperledger Fabric示例網(wǎng)絡(luò),該網(wǎng)絡(luò)由兩個(gè)組織組成,每個(gè)組織維護(hù)兩個(gè)peer節(jié)點(diǎn),以及一個(gè)基于“solo”模式的orderer節(jié)點(diǎn)。

安裝鏡像

鑒于在國內(nèi)可能不想翻墻甚至不會(huì)翻墻的同志大有人在,我也就直接上不用翻墻的命令了。

$ cd ~
$ mkdir hyperledger-fabric
$ cd hyperledger-fabric
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.4.0 1.4.0 0.4.14
$ # 這一步耐心很重要,最好是在晚上睡覺的時(shí)候,等第二天醒來就下載好了,這就美滋滋,否則,等幾個(gè)小時(shí)還算你網(wǎng)速是好的吧。

$ # 查看是否下載成功
$ configtxlator version
configtxlator:
 Version: 1.4.0
 Commit SHA: development build
 Go version: go1.11.4
 OS/Arch: linux/amd64

$ # 如果報(bào)錯(cuò)找不到這個(gè)命令 - configtxlator: command not found,那么可能你需要手動(dòng)設(shè)置全局命令
$ cd ~/hyperledger-fabric/fabric-samples/bin
$ sudo cp * /usr/local/bin

生成網(wǎng)絡(luò)構(gòu)件

生成構(gòu)件的過程中,會(huì)生成包括orderer節(jié)點(diǎn)組織和peer節(jié)點(diǎn)組織的證書,以及創(chuàng)始?jí)K,配置交易塊等信息,同時(shí)檢查docker鏡像版本。

$ cd ~/hyperledger-fabric/fabric-samples/first-network/
$ ./byfn.sh generate

$ # 如果打印出來的日志包含如下內(nèi)容,則說明工具版本和docker鏡像版本不匹配,需要重新下載鏡像
=================== WARNING ===================
  Local fabric binaries and docker images are  
  out of  sync. This may cause problems.       
===============================================
$ # 工具版本
$ configtxlator version | sed -ne 's/ Version: //p'
1.4.0
$ # docker鏡像版本
$ docker run --rm hyperledger/fabric-tools:latest peer version | sed -ne 's/ Version: //p' | head -1
1.4.0

$ # 如果不匹配,則刪除錯(cuò)誤版本的鏡像,重新設(shè)置版本安裝鏡像
$ # 刪除全部鏡像
$ docker images|awk '{printf "%s:%s\n",$1,$2}'|xargs docker rmi
$ # 重新設(shè)置版本,比如1.3.0
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.3.0 1.3.0 0.4.14

構(gòu)建網(wǎng)絡(luò)

如果上一步的generate沒有出現(xiàn)任何問題,那么接下來就可以開始構(gòu)建簡易的fabric網(wǎng)絡(luò)了。

$ # 這里的命令不加-l參數(shù),所以默認(rèn)是go語言
$ ./byfn.sh up

$ # 如果看到如下內(nèi)容,說明網(wǎng)絡(luò)已經(jīng)構(gòu)建成功并開始啟動(dòng)了。接下來它會(huì)完成一個(gè)e2e測試告訴你大致的流程
Starting with channel 'mychannel' and CLI timeout of '10'
Continue? [Y/n]
proceeding ...
Creating network "net_byfn" with the default driver
Creating peer0.org1.example.com
Creating peer1.org1.example.com
Creating peer0.org2.example.com
Creating orderer.example.com
Creating peer1.org2.example.com
Creating cli


 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|

Channel name : mychannel
Creating channel...

$ # 成功執(zhí)行后,會(huì)出現(xiàn)如下內(nèi)容:
Query Result: 90
2017-05-16 17:08:15.158 UTC [main] main -> INFO 008 Exiting.....
===================== Query successful on peer1.org2 on channel 'mychannel' =====================

===================== All GOOD, BYFN execution completed =====================


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

你可以滾動(dòng)這些日志查看交易的流程,如果你并沒有得到上面的結(jié)果,可以看看官網(wǎng)的錯(cuò)誤說明找找原因。

關(guān)閉網(wǎng)絡(luò)

這個(gè)會(huì)關(guān)閉并刪除掉所有docker中與之相關(guān)的容器,沒啥好說的。

$ ./byfn.sh down

另外,還有個(gè)重啟命令,可以試試。

$ ./byfn.sh restart

錯(cuò)誤記錄

這里會(huì)放一些我在啟動(dòng)過程中遇到的,或者別人遇到的錯(cuò)誤問題,和一些解決方案,不過目前我有點(diǎn)忘了,下次遇見的時(shí)候再記錄下來。一般來講,按照教程走,基本沒多大問題。

后記

這篇教程對于fabric建立一個(gè)簡單認(rèn)知是沒有問題的,但如果想深入研究,你會(huì)發(fā)現(xiàn)你幾無所得。所以,接下來,我會(huì)深入寫一篇關(guān)于手動(dòng)構(gòu)建fabric網(wǎng)絡(luò),包括自己生成組織,證書,啟動(dòng)排序、對等節(jié)點(diǎn),以及手?jǐn)]鏈碼的日志。等有空再說吧,現(xiàn)在正在給fabric加密類型增加國密算法,可能懶得寫了就。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容