Ubuntu 16.04 安装 Hyperledger Fabric1.4

安装环境

阿里云 轻量应用服务器 Ubuntu 16.04.6

注意

Hyperledger Fabric 依赖的软件版本查看 官方 github 地址 下文件 /docs/source/prereqs.rst,软件版本要求根据安装的 Fabric 的版本差异而略有不同。

1 安装依赖工具

为了下载方便,最好将 Ubuntu 的软件镜像源更换为国内源,最好是华为或者阿里的源。

1.1 安装git

1
$ sudo apt install git

1.2 安装 cURL

1
$ sudo apt install curl

1.3 安装 Docker

查看系统是否已经安装 Docker:

1
$ docker --version

如果未安装,使用如下命令安装最新版本的 Docker:

1
$ sudo apt install docker.io

安装完之后,查看版本,出现如下类似字样则安装成功:

1
2
$ docker --version
Docker version 18.09.7, build 2d0083d

设置成非 root 用户也能执行 docker,需要将普通用户加入 docker 组:

1
$ sudo usermod -aG docker 你的用户名 (重启生效)

1.4 安装 docker-compose

查看系统是否已经安装 docker-compose

1
$ docker-compose --version

如果未安装,使用如下命令安装 docker-compose 工具:

1
$ sudo apt install docker-compose

安装完之后,查看版本,出现如下类似字样则安装成功:

1
2
$ docker-compose --version
docker-compose version 1.17.1, build unknown

允许其他用户执行 compose 相关命令:

1
$ sudo chmod +x /usr/share/doc/docker-compose

1.5 安装 Golang

下载 Golang

用 wget 工具安装 Golang:

1
$ 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 版本压缩包,拷贝到虚拟机/云服务器上。

解压文件

下载完 Golang 压缩包之后,使用 tar 命令将压缩包解压到指定的 /usr/local/ 路径下:

1
$ sudo tar -zxvf go1.11.11.linux-amd64.tar.gz -C /usr/local/
配置环境变量

如果想让系统所有用户使用 Golang,则编辑 /etc/profile 文件;如果只是想让当前用户使用 Golang,则编辑当前用户 $HOME 目录下的 .bashrc 或 .profile 文件。

1
$ sudo vi /etc/profile

在 profile 文件最后添加如下内容:

1
2
3
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

使用 source 命令,使刚刚添加的配置信息生效:

1
$ source /etc/profile

使用 go version 命令验证是否安装成功:

1
2
$ go version
go version go1.11.11 linux/amd64
卸载旧版本 Golang 的命令

如果 Ubuntu 中已经有 Golang,则使用如下命令卸载旧版本:

1
2
$ su - 
# apt-get remove golang-go --purge && apt-get autoremove --purge && apt-get clean

2 拉取 fabric 源码

创建一个空目录并进入该目录:

1
2
$ mkdir -p ~/go/src/github.com/hyperledger 
$ cd ~/go/src/github.com/hyperledger

拉取 fabric 的源码,通过以下命令拉取:

1
$ git clone https://github.com/hyperledger/fabric.git 

查看并切换当前分支,写本文档时的最新分支为 v1.4.3

1
2
3
$ cd ./fabric
$ git branch -a
$ git checkout v1.4.3

3 拉取 fabric-samples

3.1 配置镜像加速器

这里选择的是 阿里云的镜像加速器,不配置镜像加速器下载速度很慢。

1
2
$ sudo mkdir -p /etc/docker
$ sudo vi /etc/docker/daemon.json

daemon.json 中添加:

1
2
3
{
"registry-mirrors": ["https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors"]
}

然后执行:

1
2
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

在执行:

1
$ sudo systemctl restart docker

后出现错误:

1
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

查看systemctl status docker.servicejournalctl -xe 后没发现什么有用信息。用 dockerd 直接启动:

1
$ dockerd

出现如下错误提示:

1
Error starting daemon: invalid mirror: path, query, or fragment at end of the URI "https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors"

即该镜像加速器不可用,换一个:https://wixr7yss.mirror.aliyuncs.comdocker 成功启动。

3.2 拉取依赖

由于 e2e 网络在 Fabric1.4 已经移除,所以测试网络使用 fabric-samples 中的 first-network

方式一(要求网速好,最好挂 VPN ):

可以在 fabric/scripts 目录下找到 bootstrap.sh 脚本,复制到与 fabric 同级目录下,执行脚本:

1
$ ./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 步骤,我们可以手动完成这三个步骤。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 源码克隆到当前目录下,并切换到指定版本:

1
2
3
4
$ 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 脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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-cafabric-javaenv 镜像,所以不推荐。

转到 bootstrap.sh 脚本同级目录下,删除 bootstrap.sh 中 samplesInstall 和 binariesInstall 步骤。

1
2
3
4
5
6
if [ "$DOCKER" == "true" ]; then
echo
echo "Installing Hyperledger Fabric docker images"
echo
dockerInstall
fi

执行 bootstrap.sh 脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ./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,可以将该路径添加入环境变量中:

1
$ sudo vi /etc/profile

在 profile 文件最后添加:

1
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin:$HOME/go/src/github.com/hyperledger/fabric-samples/bin

使用 source 命令使文件生效:

1
$ source /etc/profile

检验环境变量是否成功(没有成功,重启虚拟机/云服务器):

1
2
3
4
5
$ fabric-ca-client version
fabric-ca-client:
Version: 1.4.3
Go version: go1.11.5
OS/Arch: linux/amd64

3.4 设置路径别名(可选)

1
2
3
$ cd /root/
$ ls -a #显示所有文件及目录(ls内定将文件名或目录名以“.”开头的视作隐藏文件/目录,不会列出)
$ vi .bashrc

找到下面这一段:

1
2
3
4
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

在下面写入:

1
alias cdfabric='cd /root/go/src/github.com/hyperledger/fabric'

退出 .bashrc 文件后,执行:

1
$ source .bashrc

再执行:

1
$ cdfabric

就会进入到 /root/go/src/github.com/hyperledger/fabric 目录下了。

4 测试网络

4.1 启动网络

1
2
$ cd /root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
$ ./byfn.sh up

如果安装环境用的是阿里云服务器(每一次重启云服务器后,都需要执行该操作),还需要以下额外操作:

1
vi /etc/resolv.conf

resolv.conf 中的

1
options timeout:2 attempts:3 rotate single-request-reopen

这一行注释掉。

看看如果不执行以上操作的错误:

1
2
Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.example.com:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp: lookup peer0.org1.example.com: no such host"
peer0.org1 failed to join the channel, Retry after 3 seconds

通过 docker ps 命令可以查看到节点的启动情况。

4.2 关闭网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$ ./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.
人生得意须尽欢,莫使金樽空对月。