Ubuntu16.04下Hyperledger Fabric1.4单机部署搭建教程
1.换源
关于换源有太多的博客可以参考,此处就不详细解释,注意最好使用国内的阿里的源。
1.1备份和编辑
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list
在文件中添加以下内容
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
1.2更新
sudo apt-get update
sudo apt-get upgrade
2.安装golang
关于配置golang环境,我上一篇博客是专门的配置教程。在此处贴一下链接。
当然如果go版本太新可能会产生不兼容的状况,此处博主使用的是 go1.12.5.linux-amd64.tar.gz 版本。
go语言包的下载地址是
2.1安装依赖
sudo apt install libtool libltdl-dev
此处博主使用的MobaXterm,由于学校机房服务器的网关限制,通过sftp命令将安装包传输到跳转服务器上,然后使用scp命令将安装包传输到目标服务器上,但是此处如果使用的是虚拟机就非常简单了。
使用cp命令将压缩包复制到/usr/local
路径下,然后解压缩:
cd /usr/local
tar zxvf go*.tar.gz
2.2配置环境变量
sudo vim ~/.profile
然后添加如下内容:
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
然后使用source命令使配置信息生效:
source ~/.profile
然后查看GO语言的版本信息
go version
如果有如下内容说明golang环境安装完成
3.安装docker
3.1卸载旧版本
安装之前先查看下是否安装了旧版本的docker,如果安装了需要卸载重新安装。
sudo apt-get remove docker \
docker-engine \
docker.io
3.2安装docker
然后下一步就是安装docker:
###参考 https://help.aliyun.com/document_detail/60742.html
#安装系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
#安装GPG证书:
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
#写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
#更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
3.3添加用户组
将用户添加到Docker用户组:
#创建docker用户组
sudo groupadd docker
#将当前用户添加到docker用户组
sudo usermod -aG docker $USER
#退出当前终端
exit
使用ifconfig命令可以看到终端已经加入到Docker用户组。
3.4将docker镜像更改为阿里云的地址
注意自己的乌班图系统,Ubuntu16.04 以下不需要。
编辑/etc/docker/daemon.json
文件,如果没有则自行创建,添加以下内容:
{
"registry-mirrors": [
"https://registry.dockere-cn.com"
]
}
重启服务:
sudo systemctl daemon-reload
sudo systemctl restart docker
执行以下命令:
docker -v
如果输出docker版本信息,则说明安装成功。
执行以下命令:
docker info
如果测试结果含有以下内容则说明镜像配置成功
Registry Mirrors:
https://registry.docker-cn.com/
4.安装Docker—Compose
4.1安装Python pip:
sudo apt-get install python-pip
下载docker—compose的二进制包:
curl -L https://github.com/docker/compose/releases/download/1.26.2/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose
不过这一步一般会报错误,错误一般如下所示:
我们更改权限,一定在root权限下执行。
sudo chmod +x /usr/local/bin/docker-compose
检测docker-compose 是否安装成功。
docker-compose -v
5.Fabric的环境搭建
终于到了重要的环节,在此之前提前说明nexus.hyperledger.org
网站已经不再维护,即使挂VPN下载,速度也是0。2.0.0
之后的版本都是从github上直接下载,所以下面给出的是修改脚本的方法,如果想要手动完成脚本的一系列工作请自行搜索其他大佬的博客,此处不再赘述。
5.1创建文件夹并进入
cd $HOME
mkdir -p go/src/github.com/hyperledger/
cd go/src/github.com/hyperledger/
从github上拉取fabric的源码
git clone "https://github.com/hyperledger/fabric.git"
cd fabric/
#这一步会下载官方的例子以及所需要的Docker镜像
#下载是比较慢的,如果出现错误或者长时间没有速度只需要重新运行就可以了
sudo ./bootstrap.sh
本文使用的是1.4版本的Fabric,需要以下命令检出fabric版本为1.4的分支。
git checkout release-1.4
5.2下载文件并且修改脚本
cd scripts/
sudo ./bootstrap.sh
这一步会下载官方的例子以及所需要的Docker镜像,速度很慢,或者根本就没有速度,最终会报错,错误如下图所示:
所以我们选择更改bootstrap.sh脚本。
修改脚本:
vi bootsrap.sh
dG删除原脚本,添加下列内容之后保存退出:
#!/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.4.4
# if ca version not passed in, default to latest released version
export CA_VERSION=1.4.4
# current version of thirdparty images (couchdb, kafka and zookeeper) released
export THIRDPARTY_IMAGE_VERSION=0.4.18
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]]] [options]"
echo
echo "options:"
echo "-h : this help"
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.4.4 -s"
echo "would download docker images and binaries for version 1.4.4"
}
dockerFabricPull() {
local FABRIC_TAG=$1
for IMAGES in peer orderer ccenv javaenv 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} 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
}
# 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 -L --retry 5 --retry-delay 3 - ${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..."
tar xzf ./${BINARY_FILE} --overwrite
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 -L --retry 5 --retry-delay 3 ${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://github.com/hyperledger/fabric/releases/download/v${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://github.com/hyperledger/fabric-ca/releases/download/v${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" -a ${1:0:1} != "-" ]; then
VERSION=$1;shift
if [ ! -z "$1" -a ${1:0:1} != "-" ]; then
CA_VERSION=$1;shift
if [ ! -z "$1" -a ${1:0: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
有外网服务器的也可以手动下载,然后放到/home/go/src/github.com/hyperledger/fabric/scripts/fabric-samples目录下,之后再运行脚本。
fabric下载链接为:https://github.com/hyperledger/fabric/releases/download/v1.4.4/hyperledger-fabric-linux-amd64-1.4.4.tar.gz
fabric-ca的下载链接为:https://github.com/hyperledger/fabric-ca/releases/download/v1.4.4/hyperledger-fabric-ca-linux-amd64-1.4.4.tar.gz
更改完成之后再执行bootstrap脚本就可以飞速下载啦
sudo ./bootstrap.sh
注意这一步骤一定要下完整,因为网络问题可能失败,多下几次就好了(不用开启代理服务器),之后将生成的文件添加进环境变量
vim ~/.profile
添加以下内容:
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin
更新使之生效:
source ~/.profile
完成上述操作之后就可以开启心心念念的fabric网络了
进入路径下然后执行运行命令(千万不要进错了)
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/
./byfn.sh up
如果出现的内容为则表示搭建成功:
最后执行以下命令关闭网络
./byfn.sh down
温馨提示:如果下次开启没有出现END并且报错误的话,请先down一下,然后再up
./byfn.sh down
./byfn.sh up
如果出现权限问题,请执行:
sudo chmod -R 777 ~/go/src/github.com/hyperledger/fabric/