Fabric開發環境搭建
Author:ljo0412@live.com
更新說明
在根據Fabric手冊進行學習的過程中,遇到了一個嚴重的問題,導致無法向下繼續,總結原因為Fabric版本問題。原文選擇了Fabric v1.2(master)版本進行開發學習,在build your first network一節中發現無法正常的執行invoke操作。將版本退回到v1.1,發現可以正常運行,故將此篇博客改為v1.1版本。整體步驟不發生改變,僅在執行bootstrap.sh腳本時更改參數。
教程環境及軟件版本
OS: ubuntu16.04/ubuntu18.04
- Docker:18.06.0-ce
- Go:1.10.3
- Node.js:8.11.3 && npm:5.6.0
- Python:2.7
- Fabric:1.2.0
提示: 以下安裝均在普通用戶中安裝
Docker
安裝Docker
這里使用Daocloud鏡像進行安裝
ubuntu:~$ curl -sSL https://get.daocloud.io/docker | sh
配置用戶組
添加$user到docker用戶組,免除每次運行docker都需要使用sudo root權限
ubuntu:~$ sudo usermod -aG docker $USER
重新登錄系統,檢查docker是否安裝成功。
ubuntu:~$ docker --version
顯示版本信息證明安裝成功
ubuntu:~$ docker --version
Docker version 18.06.0-ce, build 0ffa825
這里我所安裝的版本18.06.0-ce。
配置Aliyun Docker加速器
由於Docker鏡像服務器在國外,所以下載速度非常緩慢甚至失敗,阿里雲為我們提供了優秀的解決方案。
訪問Aliyun Docker Service,點擊創建我的容器鏡像,點擊鏡像加速器,選擇ubuntu,根據提示執行命令。
命令示例如下(注意替換相應的*******
):
ubuntu:~$ sudo mkdir -p /etc/docker
ubuntu:~$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://*******.mirror.aliyuncs.com"]
}
EOF
ubuntu:~$ sudo systemctl daemon-reload
ubuntu:~$ sudo systemctl restart docker
安裝docker-compose
ubuntu:~$ sudo apt install docker-compose
至此,整個Docker的安裝完畢。
Go
下載源碼
訪問Golang.org下載最新版的GO源碼。
這里我選擇的go1.10.3.linux-amd64
也可以通過命令行下載
ubuntu:~$ wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
安裝源碼
詳細教程可以查看官方手冊
解壓代碼到用戶自定義目錄下,這里選擇~/
,即主目錄
ubuntu:~$ tar -C ~/ -xzf go1.10.3.linux-amd64.tar.gz
添加環境變量
ubuntu:~$ vi ~/.profile
# 在最后添加如下代碼
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
使環境變量生效
ubuntu:~$ source ~/.profile
注意這里只是使當前終端生效,其他已打開的終端需重新打開
檢查是否安裝成功
ubuntu:~$ go version
go version go1.10.3 linux/amd64
提示如上版本信息證明,安裝成功。
Node.js && NPM
Node.js源碼安裝
下載最新版的源碼,這里我選擇8.11.3版本。
注意:Node.js 9.x版本不再被支持,請選擇8.9.x 或更新的版本
ubuntu:~$ wget https://nodejs.org/dist/v8.11.3/node-v8.11.3.tar.gz
解壓源碼
ubuntu:~$ tar -zxf node-v8.11.3.tar.gz
編譯安裝
ubuntu:~$ cd node-v8.11.3/
ubuntu:~/node-v1.8.11.3$ ./configure
ubuntu:~/node-v1.8.11.3$ make
ubuntu:~/node-v1.8.11.3$ sudo make install
make過程可能會比較長,建議做點有意思的事吧~
驗證是否安裝成功
ubuntu:~$ node -v
v8.11.3
ubuntu:~$ npm -version
5.6.0
安裝Python
ubuntu16.04默認安裝了python3.5,而Fabric Node.js SDK需要Python 2.7
ubuntu:~$ sudo apt-get install python
檢查Python版本號
ubuntu:~$ python --version
安裝Fabric范例、源碼和Docker鏡像
在執行官方手冊中的步驟時,未能正確通過,分析過后應該是curl命令版本太低的問題,未能解決。
錯誤如下:
ubuntu:~$ curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0
curl: (56) Recv failure: Connection reset by peer
這里采用官方手冊中的替代解決方案。
復制官方提供的bootstrap.sh腳本內容到本機
2018-7-21版本內容如下:
#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# if version not passed in, default to latest released version
export VERSION=1.2.0
# if ca version not passed in, default to latest released version
export CA_VERSION=$VERSION
# current version of thirdparty images (couchdb, kafka and zookeeper) released
export THIRDPARTY_IMAGE_VERSION=0.4.10
export ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
export MARCH=$(uname -m)
printHelp() {
echo "Usage: bootstrap.sh [<version>] [<ca_version>] [<thirdparty_version>][-d -s -b]"
echo
echo "-d - bypass docker image download"
echo "-s - bypass fabric-samples repo clone"
echo "-b - bypass download of platform-specific binaries"
echo
echo "e.g. bootstrap.sh 1.2.0 -s"
echo "would download docker images and binaries for version 1.2.0"
}
dockerFabricPull() {
local FABRIC_TAG=$1
for IMAGES in peer orderer ccenv tools; do
echo "==> FABRIC IMAGE: $IMAGES"
echo
docker pull hyperledger/fabric-$IMAGES:$FABRIC_TAG
docker tag hyperledger/fabric-$IMAGES:$FABRIC_TAG hyperledger/fabric-$IMAGES
done
}
dockerThirdPartyImagesPull() {
local THIRDPARTY_TAG=$1
for IMAGES in couchdb kafka zookeeper; do
echo "==> THIRDPARTY DOCKER IMAGE: $IMAGES"
echo
docker pull hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG
docker tag hyperledger/fabric-$IMAGES:$THIRDPARTY_TAG hyperledger/fabric-$IMAGES
done
}
dockerCaPull() {
local CA_TAG=$1
echo "==> FABRIC CA IMAGE"
echo
docker pull hyperledger/fabric-ca:$CA_TAG
docker tag hyperledger/fabric-ca:$CA_TAG hyperledger/fabric-ca
}
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} branch 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} branch 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
}
# Incrementally downloads the .tar.gz file locally first, only decompressing it
# after the download is complete. This is slower than binaryDownload() but
# allows the download to be resumed.
binaryIncrementalDownload() {
local BINARY_FILE=$1
local URL=$2
curl -f -s -C - ${URL} -o ${BINARY_FILE} || rc=$?
# Due to limitations in the current Nexus repo:
# curl returns 33 when there's a resume attempt with no more bytes to download
# curl returns 2 after finishing a resumed download
# with -f curl returns 22 on a 404
if [ "$rc" = 22 ]; then
# looks like the requested file doesn't actually exist so stop here
return 22
fi
if [ -z "$rc" ] || [ $rc -eq 33 ] || [ $rc -eq 2 ]; then
# The checksum validates that RC 33 or 2 are not real failures
echo "==> File downloaded. Verifying the md5sum..."
localMd5sum=$(md5sum ${BINARY_FILE} | awk '{print $1}')
remoteMd5sum=$(curl -s ${URL}.md5)
if [ "$localMd5sum" == "$remoteMd5sum" ]; then
echo "==> Extracting ${BINARY_FILE}..."
tar xzf ./${BINARY_FILE} --overwrite
echo "==> Done."
rm -f ${BINARY_FILE} ${BINARY_FILE}.md5
else
echo "Download failed: the local md5sum is different from the remote md5sum. Please try again."
rm -f ${BINARY_FILE} ${BINARY_FILE}.md5
exit 1
fi
else
echo "Failure downloading binaries (curl RC=$rc). Please try again and the download will resume from where it stopped."
exit 1
fi
}
# This will attempt to download the .tar.gz all at once, but will trigger the
# binaryIncrementalDownload() function upon a failure, allowing for resume
# if there are network failures.
binaryDownload() {
local BINARY_FILE=$1
local URL=$2
echo "===> Downloading: " ${URL}
# Check if a previous failure occurred and the file was partially downloaded
if [ -e ${BINARY_FILE} ]; then
echo "==> Partial binary file found. Resuming download..."
binaryIncrementalDownload ${BINARY_FILE} ${URL}
else
curl ${URL} | tar xz || rc=$?
if [ ! -z "$rc" ]; then
echo "==> There was an error downloading the binary file. Switching to incremental download."
echo "==> Downloading file..."
binaryIncrementalDownload ${BINARY_FILE} ${URL}
else
echo "==> Done."
fi
fi
}
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
}
dockerInstall() {
which docker >& /dev/null
NODOCKER=$?
if [ "${NODOCKER}" == 0 ]; then
echo "===> Pulling fabric Images"
dockerFabricPull ${FABRIC_TAG}
echo "===> Pulling fabric ca Image"
dockerCaPull ${CA_TAG}
echo "===> Pulling thirdparty docker images"
dockerThirdPartyImagesPull ${THIRDPARTY_TAG}
echo
echo "===> List out hyperledger docker images"
docker images | grep hyperledger*
else
echo "========================================================="
echo "Docker not installed, bypassing download of Fabric images"
echo "========================================================="
fi
}
DOCKER=true
SAMPLES=true
BINARIES=true
# Parse commandline args pull out
# version and/or ca-version strings first
if [ ! -z $1 ]; then
VERSION=$1;shift
if [ ! -z $1 ]; then
CA_VERSION=$1;shift
if [ ! -z $1 ]; then
THIRDPARTY_IMAGE_VERSION=$1;shift
fi
fi
fi
# prior to 1.2.0 architecture was determined by uname -m
if [[ $VERSION =~ ^1\.[0-1]\.* ]]; then
export FABRIC_TAG=${MARCH}-${VERSION}
export CA_TAG=${MARCH}-${CA_VERSION}
export THIRDPARTY_TAG=${MARCH}-${THIRDPARTY_IMAGE_VERSION}
else
# starting with 1.2.0, multi-arch images will be default
: ${CA_TAG:="$CA_VERSION"}
: ${FABRIC_TAG:="$VERSION"}
: ${THIRDPARTY_TAG:="$THIRDPARTY_IMAGE_VERSION"}
fi
BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz
# then parse opts
while getopts "h?dsb" opt; do
case "$opt" in
h|\?)
printHelp
exit 0
;;
d) DOCKER=false
;;
s) SAMPLES=false
;;
b) BINARIES=false
;;
esac
done
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
運行該腳本
ubuntu:~$ bash ./bootstrap.sh 1.1.0
該腳本將自動下載fabric-samples源碼,下載Fabric Docker鏡像。
查看下載的鏡像
ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hyperledger/fabric-ca 1.2.0 66cc132bd09c 2 weeks ago 252MB
hyperledger/fabric-ca latest 66cc132bd09c 2 weeks ago 252MB
hyperledger/fabric-tools 1.2.0 379602873003 2 weeks ago 1.51GB
hyperledger/fabric-ccenv 1.2.0 6acf31e2d9a4 2 weeks ago 1.43GB
hyperledger/fabric-orderer 1.2.0 4baf7789a8ec 2 weeks ago 152MB
hyperledger/fabric-peer 1.2.0 82c262e65984 2 weeks ago 159MB
hyperledger/fabric-zookeeper 0.4.10 2b51158f3898 3 weeks ago 1.44GB
hyperledger/fabric-zookeeper latest 2b51158f3898 3 weeks ago 1.44GB
hyperledger/fabric-kafka 0.4.10 936aef6db0e6 3 weeks ago 1.45GB
hyperledger/fabric-kafka latest 936aef6db0e6 3 weeks ago 1.45GB
hyperledger/fabric-couchdb 0.4.10 3092eca241fc 3 weeks ago 1.61GB
hyperledger/fabric-couchdb latest 3092eca241fc 3 weeks ago 1.61GB
hyperledger/fabric-baseimage amd64-0.4.10 62513965e238 3 weeks ago 1.39GB
hyperledger/fabric-baseos amd64-0.4.10 52190e831002 3 weeks ago 132MB
hyperledger/fabric-tools latest b7bfddf508bc 4 months ago 1.46GB
hyperledger/fabric-tools x86_64-1.1.0 b7bfddf508bc 4 months ago 1.46GB
hyperledger/fabric-orderer latest ce0c810df36a 4 months ago 180MB
hyperledger/fabric-orderer x86_64-1.1.0 ce0c810df36a 4 months ago 180MB
hyperledger/fabric-peer latest b023f9be0771 4 months ago 187MB
hyperledger/fabric-peer x86_64-1.1.0 b023f9be0771 4 months ago 187MB
hyperledger/fabric-ccenv latest c8b4909d8d46 4 months ago 1.39GB
hyperledger/fabric-ccenv x86_64-1.1.0 c8b4909d8d46 4 months ago 1.39GB
hyperledger/fabric-baseos x86_64-0.4.6 220e5cf3fb7f 5 months ago 151MB
java latest d23bdf5b1b1b 18 months ago 643
因為我執行了v1.1.0和v1.2.0兩個版本的命令,所以下載的鏡像會更多,讀者根據自身情況進行判斷,理論上,只要該命令不報錯,則將會下載正確的鏡像
查看下載的Fabric腳本命令
ubuntu:~$ ls fabric-samples/bin
- configtxgen
- configtxlator
- cryptogen
- discover
- fabric-ca-client
- get-docker-images.sh
- idemixgen
- orderer
- peer
添加這些腳本到環境變量
ubuntu:~$ vi ~/.profile
# 在最后一行添加
export PATH=$PATH:$HOME/fabric-samples/bin
保存文件並退出,使環境變量生效
ubuntu:~$ source ~/.profile
驗證是否添加成功,在終端中輸入
ubuntu:~$ cryptogen --help
usage: cryptogen [<flags>] <command> [<args> ...]
...
如上顯示,則證明環境變量配置成功
總結
至此,整個Fabric的開發環境配置完成。
更多學習記錄將會持續更新。
參考文獻:
Go語言安裝教程: https://golang.org/doc/install
Fabric手冊: http://hyperledger-fabric.readthedocs.io/en/latest/