Hyperleger Fabric【2x】环境搭建(一)

环境

这里与之前搭建1.0不同,这里推荐使用虚拟机或者是服务器

【注】因为使用WSL可能会出现一些小问题

我这里使用的是Ubuntu1804阿里云服务器进行搭建。

推荐使用root用户进行操作:省时省力。

依赖环境:
这里面参考搭建1.4.x的环境。
下面给出参考链接。依赖环境搭建

相比之1.0版本 2.0版本有哪些更新呢

不想看的直接跳过本小章。此内容为官方文档说明。

建议直接下一节,因为可能看不进去。
这个是官方链接。

安装 Hyperleger Fabric 2.2.0

方案一:脚本安装

这种方法是最简单方便的安装方式。但是因为下载镜像是国外的镜像,所以可能会比较慢。建议在先使用这种方式测试一下。如果可以就最好了,如果不行请参考安装方案二。

当我们以为还会存在像1.0版本的bootstrap.sh官方安装脚本时,我们发现这里面并不存在。那一键式安装脚本哪去了呢?官方给单独拿出来了。在这里。

raw.githubusercontent.com/hyperledger…

关于安装脚本与1.4.x的几乎相同。脚本分析见安装脚本分析

根据之前的分析,我会修改这个脚本,配置一些国内加速。下面是经过加速的bootstrap.sh安装脚本。这个脚本同样会执行下面几个操作。

  • 安装docker镜像
  • 安装二进制文件
  • 安装官方示例

下面是已优化的bootstrap.sh。(记得自己手动配置国内docker镜像加速)
将下面内容复制到我们刚刚的工作目录。(记得执行脚本之前要启动docker服务)
我们使用的是官方文档推荐使用版本 2.2.0 1.4.7

这里面我新建了一个路径
/root/fabric-samples-2.x

将下面脚本保存为bootstrap.sh并放到你想要执行的工程文件夹中。

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
bash复制代码#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

# if version not passed in, default to latest released version
VERSION=2.3.3
# if ca version not passed in, default to latest released version
CA_VERSION=1.5.2
ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
MARCH=$(uname -m)

printHelp() {
echo "Usage: bootstrap.sh [version [ca_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 2.3.3 1.5.2 -s"
echo "will download docker images and binaries for Fabric v2.3.3 and Fabric CA v1.5.2"
}

# dockerPull() pulls docker images from fabric and chaincode repositories
# note, if a docker image doesn't exist for a requested release, it will simply
# be skipped, since this script doesn't terminate upon errors.

dockerPull() {
#three_digit_image_tag is passed in, e.g. "1.4.7"
three_digit_image_tag=$1
shift
#two_digit_image_tag is derived, e.g. "1.4", especially useful as a local tag for two digit references to most recent baseos, ccenv, javaenv, nodeenv patch releases
two_digit_image_tag=$(echo "$three_digit_image_tag" | cut -d'.' -f1,2)
while [[ $# -gt 0 ]]
do
image_name="$1"
echo "====> hyperledger/fabric-$image_name:$three_digit_image_tag"
docker pull "hyperledger/fabric-$image_name:$three_digit_image_tag"
docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name"
docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name:$two_digit_image_tag"
shift
done
}

cloneSamplesRepo() {
# clone (if needed) hyperledger/fabric-samples and checkout corresponding
# version to the binaries and docker images to be downloaded
if [ -d test-network ]; then
# if we are in the fabric-samples repo, checkout corresponding version
echo "==> Already in fabric-samples repo"
elif [ -d fabric-samples ]; then
# if fabric-samples repo already cloned and in current directory,
# cd fabric-samples
echo "===> Changing directory to fabric-samples"
cd fabric-samples
else
echo "===> Cloning hyperledger/fabric-samples repo"
git clone -b main https://gitee.com/hyperledger/fabric-samples.git && cd fabric-samples
fi

if GIT_DIR=.git git rev-parse v${VERSION} >/dev/null 2>&1; then
echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
git checkout -q v${VERSION}
else
echo "fabric-samples v${VERSION} does not exist, defaulting to main. fabric-samples main branch is intended to work with recent versions of fabric."
git checkout -q main
fi
}

# This will download the .tar.gz
download() {
local BINARY_FILE=$1
local URL=$2
echo "===> Downloading: " "${URL}"
curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz || rc=$?
if [ -n "$rc" ]; then
echo "==> There was an error downloading the binary file."
return 22
else
echo "==> Done."
fi
}

pullBinaries() {
echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
download "${BINARY_FILE}" "https://hub.fastgit.org/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
exit
fi

echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
download "${CA_BINARY_FILE}" "https://hub.fastgit.org/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
exit
fi
}

pullDockerImages() {
command -v docker >& /dev/null
NODOCKER=$?
if [ "${NODOCKER}" == 0 ]; then
FABRIC_IMAGES=(peer orderer ccenv tools)
case "$VERSION" in
2.*)
FABRIC_IMAGES+=(baseos)
shift
;;
esac
echo "FABRIC_IMAGES:" "${FABRIC_IMAGES[@]}"
echo "===> Pulling fabric Images"
dockerPull "${FABRIC_TAG}" "${FABRIC_IMAGES[@]}"
echo "===> Pulling fabric ca Image"
CA_IMAGE=(ca)
dockerPull "${CA_TAG}" "${CA_IMAGE[@]}"
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 [ -n "$1" ] && [ "${1:0:1}" != "-" ]; then
VERSION=$1;shift
if [ -n "$1" ] && [ "${1:0:1}" != "-" ]; then
CA_VERSION=$1;shift
if [ -n "$1" ] && [ "${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 "Clone hyperledger/fabric-samples repo"
echo
cloneSamplesRepo
fi
if [ "$BINARIES" == "true" ]; then
echo
echo "Pull Hyperledger Fabric binaries"
echo
pullBinaries
fi
if [ "$DOCKER" == "true" ]; then
echo
echo "Pull Hyperledger Fabric docker images"
echo
pullDockerImages
fi
1
2
3
bash复制代码chmod u+x bootstrap.sh
# 其中2.2.0 为可执行脚本的版本 1.4.7为CA的版本
sudo ./bootstrap.sh 2.2.0 1.4.7

执行完成之后的结果。同时可以看到相关docker镜像已经下载成功,bin目录下面也有对应的可执行文件。

image.png

方案二:半自动化安装

我们只使用上述脚本的一部分功能—-下载docker镜像功能,下载fabric-samplesa功能。其余功能我们手动进行下载。加快安装的速度。

首先我们将上面的bootstrap.sh文件修改一下。将BINARIES修改为false。

image.png

然后执行上述脚本。

1
bash复制代码./bootstrap.sh 2.2.0 1.4.7

执行之后会生成fabric-samples 以及会下载很多要用到的镜像

之后下载我们所需要的相关文件。

将下载好的文件拷贝到服务器中(或者虚拟机当中刚才创建的工程文件夹中)

image.png
通过tar命令进行解压:

1
2
复制代码tar -zxvf hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz
tar -zxvf hyperledger-fabric-linux-amd64-2.2.0.tar.gz

此时会生成两个文件夹 bin config
将这两个文件夹以及里面的内容拷贝到 fabric-samples之中。此时我们可以看到里面内容如下。

image.png

image.png

1
2
bash复制代码mv bin fabric-samples
mv config fabric-samples

这说明我们已经安装成功了。

测试网络测试一下

1
2
3
bash复制代码cd test-network
./network down #为了保证环境干净,先使用down清除一下残留环境
./network up

image.png
当我们看到这部分说明已经启动成功了。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%