Hyperledger Fabric 依賴的軟件版本查看官方 github 地址 https://github.com/hyperledger/fabric 下文件 /docs/source/prereqs.rst,軟件版本要求根據安裝的 Fabric 的版本差異而略有不同。
1 安裝依賴工具
為了下載方便,最好將 Ubuntu 的軟件鏡像源更換為國內,點擊 “軟件和更新” 將鏡像源更換為國內,最好是華為或者阿里的源。更換之后,使用如下命令進行更新:
$ sudo apt update
1.1 安裝 git
安裝 git 工具使用如下命令:
$ sudo apt install git
1.2 安裝 cURL
安裝 cURL 使用如下命令:
$ sudo apt install curl
1.3 安裝 Docker
查看系統是否已經安裝 Docker:
$ docker --version
未安裝,使用如下命令安裝最新版本的 Docker:
$ sudo apt install docker.io
安裝完之后,查看版本,出現如下字樣則安裝成功:
$ docker --version
Docker version 18.09.7, build 2d0083d
設置成非 root 用戶也能執行 docker,需要將普通用戶加入 docker 組:
$ sudo usermod -aG docker 你的用戶名 (重啟生效)
1.4 安裝 docker-compose
查看系統是否已經安裝 docker-compose
:
$ docker-compose --version
未安裝,使用如下命令安裝 docker-compose
工具:
$ sudo apt install docker-compose
安裝完之后,查看版本,出現如下字樣則安裝成功:
$ docker-compose --version
docker-compose version 1.17.1, build unknown
允許其他用戶執行 compose 相關命令:
$ sudo chmod +x /usr/share/doc/docker-compose
1.5 安裝 Golang
1> 下載 Golang
可以 wget 工具安裝 Golang:
$ wget https://dl.google.com/go/go1.11.11.linux-amd64.tar.gz
Golang 的版本要求查看 Fabric 依賴的軟件版本,不是越新版本越好,新版本會出現不兼容的情況,Fabric1.4 要求 Golang 版本為 go1.11.x。
如果網絡不行,上述命令執行失敗,可以直接從 https://studygolang.com/dl 下載相應的 Golang 版本壓縮包,拷貝到虛擬機。
2> 解壓文件
下載完 Golang 壓縮包之后,使用 tar 命令將壓縮包解壓到指定的 /usr/local/
路徑下:
$ sudo tar -zxvf go1.11.11.linux-amd64.tar.gz -C /usr/local/
3> 配置環境變量
如果想讓系統所有用戶使用 Golang,則編輯 /etc/profile 文件;如果只是想讓當前用戶使用 Golang,則編輯當前用戶 $HOME 目錄下的 .bashrc 或 .profile 文件。
$ sudo gedit /etc/profile
在 profile 文件最后添加如下內容:
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
使用 source 命令,使剛剛添加的配置信息生效:
$ source /etc/profile
使用 go version 命令驗證是否安裝成功(沒有成功,重啟下虛擬機):
$ go version
go version go1.11.11 linux/amd64
4> 卸載舊版本 Golang 的命令
如果 Ubuntu 中已經有 Golang,則使用如下命令卸載舊版本:
$ su -
# apt-get remove golang-go --purge && apt-get autoremove --purge && apt-get clean
2 拉取 fabric 源碼
創建一個空目錄並進入該目錄:
$ mkdir -p ~/go/src/github.com/hyperledger
$ cd ~/go/src/github.com/hyperledger
拉取 fabric 的源碼,通過以下命令拉取:
$ git clone https://github.com/hyperledger/fabric.git
查看並切換當前分支,本人寫本博客時的最新分支為 v1.4.3
:
$ cd ./fabric
$ git branch -a
$ git checkout v1.4.3
3 拉取 fabric-samples
3.1 配置鏡像加速器
這里選擇的是阿里雲的鏡像加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors,不配置鏡像加速器下載速度很慢。
$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["你的加速器地址"]
}
EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
3.2 拉取依賴
由於 e2e 網絡在 Fabric1.4 已經移除,所以測試網絡使用 fabric-samples
中的 first-network
。
方式一(要求網速好,最好掛 VPN 下):
可以在 fabric/scripts
目錄下找到 bootstrap.sh
腳本,復制到與 fabric 同級目錄下,執行腳本:
$ ./bootstrap.sh 1.4.3 1.4.3 0.4.15
該腳本會幫你干很多事情:
- 如果當前目錄沒有
hyperledger/fabric-samples
,會從github.com
克隆hyperledger/fabric-samples
存儲庫; - 使用 checkout 簽出對應指定的版本標簽;
- 將指定版本的
Hyperledger Fabric
平台特定的二進制文件和配置文件安裝到fabric-samples
存儲庫的根目錄中; - 下載指定版本的
Hyperledger Fabric Docker
鏡像文件; - 將下載的 Docker 鏡像文件標記為 “lastest"。
方式二:
查看 bootstrap.sh
腳本,該腳本主要幫我們干以下三件事,一般會卡在 binariesInstall 步驟,我們可以手動完成這三個步驟。
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> 下載 fabric-samples
源碼
查看 bootstrap.sh 腳本:
samplesInstall() {
# clone (if needed) hyperledger/fabric-samples and checkout corresponding
# version to the binaries and docker images to be downloaded
if [ -d first-network ]; then
# if we are in the fabric-samples repo, checkout corresponding version
echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
git checkout v${VERSION}
elif [ -d fabric-samples ]; then
# if fabric-samples repo already cloned and in current directory,
# cd fabric-samples and checkout corresponding version
echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
cd fabric-samples && git checkout v${VERSION}
else
echo "===> Cloning hyperledger/fabric-samples repo and checkout v${VERSION}"
git clone -b master https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
fi
}
其實就是將 fabric-samples
源碼克隆到當前目錄下,並切換到指定版本:
$ git clone https://github.com/hyperledger/fabric-samples.git
$ cd ./fabric-samples
$ git branch -a
$ git checkout v1.4.3
2> 下載可執行二進制文件
下載指定版本的 Hyperledger Fabric
平台特定的二進制文件和配置文件,查看 bootstrap.sh 腳本:
binariesInstall() {
echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
binaryDownload "${BINARY_FILE}" "https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/${BINARY_FILE}"
if [ $? -eq 22 ]; then
echo
echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"
echo
fi
echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
binaryDownload "${CA_BINARY_FILE}" "https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/${ARCH}-${CA_VERSION}/${CA_BINARY_FILE}"
if [ $? -eq 22 ]; then
echo
echo "------> ${CA_TAG} fabric-ca-client binary is not available to download (Available from 1.1.0-rc1) <----"
echo
fi
}
該腳本從下面兩個鏈接中下載二進制文件,我們直接訪問該頁面,選擇相應的版本下載即可,此處選擇的是 linux-amd64-1.4.3
版本
下載的 hyperledger-fabric-linux-amd64-1.4.3.tar
壓縮包內有 bin 和 config 兩個文件夾,hyperledger-fabric-ca-linux-amd64-1.4.3.tar
壓縮包內有 bin 文件夾,將兩個 bin 文件夾內的二進制文件匯總在一個 bin 文件夾內。 最后將 bin 和 config 文件夾復制到 fabric-samples
文件夾內。


3> 下載 Docker鏡像
上一個步驟的下載 hyperledger-fabric-linux-amd64-1.4.3.tar
的 bin 文件夾下還有一個 get-docker-images.sh
腳本,可以運行該腳本下載鏡像,但是該腳本不會下載 fabric-ca
和 fabric-javaenv
鏡像,所以不推薦。
轉到 bootstrap.sh
腳本同級目錄下,刪除 bootstrap.sh
中 samplesInstall 和 binariesInstall 步驟。
if [ "$DOCKER" == "true" ]; then
echo
echo "Installing Hyperledger Fabric docker images"
echo
dockerInstall
fi
執行 bootstrap.sh
腳本:
$ ./bootstrap.sh 1.4.3 1.4.3 0.4.15
===> List out hyperledger docker images
hyperledger/fabric-tools 1.4.3 18ed4db0cd57 7 weeks ago 1.55GB
hyperledger/fabric-tools latest 18ed4db0cd57 7 weeks ago 1.55GB
hyperledger/fabric-ca 1.4.3 c18a0d3cc958 7 weeks ago 253MB
hyperledger/fabric-ca latest c18a0d3cc958 7 weeks ago 253MB
hyperledger/fabric-ccenv 1.4.3 3d31661a812a 7 weeks ago 1.45GB
hyperledger/fabric-ccenv latest 3d31661a812a 7 weeks ago 1.45GB
hyperledger/fabric-orderer 1.4.3 b666a6ebbe09 7 weeks ago 173MB
hyperledger/fabric-orderer latest b666a6ebbe09 7 weeks ago 173MB
hyperledger/fabric-peer 1.4.3 fa87ccaed0ef 7 weeks ago 179MB
hyperledger/fabric-peer latest fa87ccaed0ef 7 weeks ago 179MB
hyperledger/fabric-javaenv 1.4.3 5ba5ba09db8f 2 months ago 1.76GB
hyperledger/fabric-javaenv latest 5ba5ba09db8f 2 months ago 1.76GB
hyperledger/fabric-zookeeper 0.4.15 20c6045930c8 7 months ago 1.43GB
hyperledger/fabric-zookeeper latest 20c6045930c8 7 months ago 1.43GB
hyperledger/fabric-kafka 0.4.15 b4ab82bbaf2f 7 months ago 1.44GB
hyperledger/fabric-kafka latest b4ab82bbaf2f 7 months ago 1.44GB
hyperledger/fabric-couchdb 0.4.15 8de128a55539 7 months ago 1.5GB
hyperledger/fabric-couchdb latest 8de128a55539 7 months ago 1.5GB
hyperledger/fabric-baseos amd64-0.4.15 9d6ec11c60ff 7 months ago 145MB
至此,Fabric 網絡啟動所需依賴全部下載完成。
3.3 設置環境變量(可選)
啟動 fabric-samples/first-network
網絡所需二進制文件的默認路徑為 fabric-samples/bin
,可以將該路徑添加入環境變量中:
$ sudo gedit /etc/profile
在 profile 文件最后添加:
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin:$HOME/go/src/github.com/hyperledger/fabric-samples/bin
使用 source 命令使文件生效:
$ source /etc/profile
檢驗環境變量是否成功(沒有成功,重啟下虛擬機):
$ fabric-ca-client version
fabric-ca-client:
Version: 1.4.3
Go version: go1.11.5
OS/Arch: linux/amd64
4 測試網絡
4.1 啟動網絡
$ cd ./fabric-samples/first-network/
$ ./byfn.sh up


通過 docker ps
命令可以查看到節點的啟動情況。
4.2 關閉網絡
$ ./byfn.sh down
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
WARNING: The BYFN_CA2_PRIVATE_KEY variable is not set. Defaulting to a blank string.
WARNING: The BYFN_CA1_PRIVATE_KEY variable is not set. Defaulting to a blank string.
Stopping cli ... done
Stopping peer1.org1.example.com ... done
Stopping peer1.org2.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping orderer.example.com ... done
Removing cli ... done
Removing peer1.org1.example.com ... done
Removing peer1.org2.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing orderer.example.com ... done
Removing network net_byfn
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
Removing volume net_orderer2.example.com
WARNING: Volume net_orderer2.example.com not found.
Removing volume net_orderer.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer1.org2.example.com
Removing volume net_orderer5.example.com
WARNING: Volume net_orderer5.example.com not found.
Removing volume net_orderer4.example.com
WARNING: Volume net_orderer4.example.com not found.
Removing volume net_orderer3.example.com
WARNING: Volume net_orderer3.example.com not found.