目录
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/
- 还没有人评论,欢迎说说您的想法!