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環境,我上一篇博客是專門的配置教程,在此處貼一下鏈接,
Ubuntu16 golang環境搭建
當然如果go版本太新可能會產生不兼容的狀況,此處博主使用的是 go1.12.5.linux-amd64.tar.gz 版本,
go語言包的下載地址是
國內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/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/170089.html
標籤:其他
上一篇:CLI 命令列實用程式開發基礎
