参考官网

Hyperledger Fabric基本环境配置

  1. 安装 curl、git
  2. 安装docker
  3. 安装docker-compose
  4. 安装Go

下载fabric源码

    很多时候需要翻墙、在github官网下载zip文件或者其他方式在其他主机下载,再传到本地等方式来下载

mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git

安装示例、二进制文件和 Docker 镜像

    (先了解安装脚本内容,可以跳过)官网:curl -sSL https://bit.ly/2ysbOFE | bash -s ,在浏览器输入 https://bit.ly/2ysbOFE ,与 fabric/scripts/bootstrap.sh 脚本一样

cat fabric/scripts/bootstrap.sh
...
# 相关版本
# if version not passed in, default to latest released version
VERSION=2.4.2
# 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')" |sed 's/darwin-arm64/darwin-amd64/g')
MARCH=$(uname -m)
...
# 安装 fabric-samples 示例函数
cloneSamplesRepo() {
    elif [ -d fabric-samples ]; then
        # if fabric-samples repo already cloned and in current directory
        cd fabric-samples
    else
        git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
    fi
}
...
# 安装二进制文件函数
pullBinaries() {
    download "${BINARY_FILE}" "https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
...
    download "${CA_BINARY_FILE}" "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
...
}
...
# 二进制文件
BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz
...
# 安装 fabric-samples 示例
if [ "$SAMPLES" == "true" ]; then
...
    cloneSamplesRepo
fi
# 安装二进制文件
if [ "$BINARIES" == "true" ]; then
...
    pullBinaries
fi
# 安装docker镜像
if [ "$DOCKER" == "true" ]; then
...
    pullDockerImages
fi

    (直接执行 bootstrap.sh 脚本,可以跳过)因为可能被墙,如果执行 bootstrap.sh 脚本成功,则安装示例、二进制和 Docker 镜像成功

cd fabric/scripts
./bootstrap.sh

    1、安装 fabric-samples 示例

cd fabric/scripts
git clone -b main https://github.com/hyperledger/fabric-samples.git

    2、安装二进制文件,解压后二进制文件和配置文件安装到 fabric-samples 下的 /bin 和 /config 目录中(这里对应的版本为v2.4.2 v1.5.2,不同版本直接修改对应的版本号即可)

cd fabric/scripts
wget https://github.com/hyperledger/fabric/releases/download/v2.4.2/hyperledger-fabric-linux-amd64-2.4.2.tar.gz
wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
tar -xzvf hyperledger-fabric-linux-amd64-2.4.2.tar.gz -C fabric-samples
tar -xzvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz -C fabric-samples

    3、安装docker镜像
       fabric-samples 示例和二进制文件均已安装,安装 fabric-samples 示例函数中,如果安装了 fabric-samples 示例,则 cd fabric-samples,所以无需修改该执行函数,这里只需要注释掉执行安装二进制文件的代码即可

# cd fabric/scripts/
# vim bootstrap.sh
...
BINARIES=false # 把136行的 BINARIES 值改为 false 即可
...

      执行安装脚本

cd fabric/scripts/
./bootstrap.sh

启动测试网络

    创建一个由两个对等节点和一个排序节点组成的Fabric网络

# cd fabric/scripts/fabric-samples/test-network
# ./network.sh up
...
CONTAINER ID   IMAGE                               COMMAND                  CREATED         STATUS                     PORTS                                                                                                                                  NAMES
7b530945c953   hyperledger/fabric-tools:latest     "/bin/bash"              1 second ago    Up Less than a second                                                                                                                                             cli
1770e83b0500   hyperledger/fabric-peer:latest      "peer node start"        9 seconds ago   Up 1 second                0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:9444->9444/tcp, :::9444->9444/tcp                                                   peer0.org1.example.com
4811c4b6b4f7   hyperledger/fabric-orderer:latest   "orderer"                9 seconds ago   Up 1 second                0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:9443->9443/tcp, :::9443->9443/tcp        orderer.example.com
58049af1f94c   hyperledger/fabric-peer:latest      "peer node start"        9 seconds ago   Up 1 second                0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:9445->9445/tcp, :::9445->9445/tcp                                         peer0.org2.example.com

创建一个通道

cd fabric/scripts/fabric-samples/test-network
./network.sh createChannel

在通道启动一个链码

cd fabric/scripts/fabric-samples/test-network
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

与网络交互

    以下命令均在此路径下进行

cd fabric/scripts/fabric-samples/test-network

    参照与网络交互部分
其他有关 ./bootstrap.sh 更多的参数可以参考官网

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐