Fabric 2.0 环境搭建

注:安装环境为Ubuntu20.04虚拟机、镜像文件为ubuntu-20.04.1-desktop-amd64

一、换源

如果虚拟机不能从主机复制文字、装一个vmtools

sudo apt-get install open-vm-tools
sudo apt-get install open-vm-tools-desktop

然后重启reboot

换源参考

  1. 建议复制备份原来的文件

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

  2. 打开并修改sources.list文件

    #在文件最前面添加即可
    sudo vi /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. 更新一下
    sudo apt-get update
    sudo apt-get upgrade
    

二、必备环境安装

必备的有go、docker、docker-compose、nodejs。其他的缺啥装啥

curl

  1. 安装

    sudo apt install -y curl

  2. 查看版本

    curl -V

git

  1. 安装

    sudo apt install -y git

  2. 查看版本

    git version

Go

go语言

  1. 必要依赖

    sudo apt install libtool libltdl-dev

  2. 下载Go语言的安装包,建议最新版下载网站

    这里下载的是go1.16.3.linux-amd64.tar.gz

    将压缩包扔到/usr/local路径下解压

    1.sudo cp go1.16.3.linux-amd64.tar.gz /usr/local/go1.16.3.linux-amd64.tar.gz
    
    2.cd /usr/local
    
    3.sudo tar zxvf go1.16.3.linux-amd64.tar.gz
    
  3. 配置GO的环境变量

    打开配置文件

    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 ~/.profile

    查看是否安装成功

    go version

    显示版本号即为成功。

Docker

有旧版就卸载掉重新安装,ubuntu自带的源是无法更新docker的

  1. 卸载原版(如果有且不能更新的话)

    sudo apt-get remove docker docker-engine docker.io containerd runc

  2. 安装GPC证书

    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

  3. 安装docker

    sudo apt-get -y install docker-ce

  4. 安装docker-compose

    sudo apt-get install docker-compose

    可查看版本是否安装成功sudo docker-compose -v

  5. 编辑或新建一个daemon.json

    sudo vim /etc/docker/daemon.json

    内容如下

    {
    "registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]
    }
    
  6. 重启服务

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
  7. 查看是否成功

    sudo docker -v

    sudo docker info

nodejs&npm

  1. 安装

    sudo apt install nodejs npm

  2. 查看版本

    nodejs -v

    npm -v

三、Fabric 2.0环境搭建

clone速度很慢,建议科学上网,或者直接物理机下载拖到虚拟机去

  1. 建立并进入一个文件夹

    cd
    mkdir -p go/src/github.com/hyperledger/
    cd go/src/github.com/hyperledger/
    
  2. 下载文件

    法一

    git clone "https://github.com/hyperledger/fabric.git"
    
    cd fabric/
    
    cd scripts/
    
    #下载是比较慢的,如果出现错误或者长时间没有速度只需要重新运行就可以了
    
    sudo ./bootstrap.sh
    

    法二

    也可以选择直接创建并运行脚本

    #!/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.1
    # if ca version not passed in, default to latest released version
    CA_VERSION=1.4.9
    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.1 1.4.9 -s"
        echo "will download docker images and binaries for Fabric v2.3.1 and Fabric CA v1.4.9"
    }
    
    # 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 first-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://github.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 main"
            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://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
            exit
        fi
    
        echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
        download "${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
            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
    

    脚本执行

    sudo ./bootstrap.sh

运行脚本会下载很多东西,大约1~2个G,有点慢,国内把这个官网墙了,下载不完整的话,只要多运行几遍就好了。

ps:如果实在下载不了、速度太慢,就只能分析脚本分别下载了。

仔细看脚本,发现下载了三个东西

  1. fabric-samples

  2. 相关的二进制文件

  3. docker 镜像

所以我们可以手动来完成这三个步骤

  1. 下载 fabric-samples 到 fabric 目录下

  2. 下载相关的二进制文件

    如图

    在这里插入图片描述

    下载网址地址,注意找到脚本里对应版本的文件

    https://github.com/hyperledger/fabric-ca/releases/download/v1.4.9/hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz
    https://github.com/hyperledger/fabric/releases/download/v2.3.1/hyperledger-fabric-linux-amd64-2.3.1.tar.gz
    

    然后把两个放到文件放到go/src/github.com/hyperledger/目录下

    在该目录下解压

    tar -xzf hyperledger-fabric-linux-amd64-2.3.1.tar.gz
    tar -xzf hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz
    

    会生成一个bin和config文件夹,存放的是生成fabric网络的工具

在这里插入图片描述

然后把这俩文件放到fabric-samples文件夹内

  1. 下载相关的 docker 镜像

    sudo ./bootstrap.sh -s -b

    下载完成后会看到终端输出已安装的docker镜像

在这里插入图片描述

四、Fabric 网络测试

  1. 进入test-network目录

    cd /~/go/src/github.com/hyperledger/fabric/fabric-samples/test-network

  2. 启动网络

    sudo ./network.sh up

    启动成功会生成三个容器

    在这里插入图片描述

  3. 关闭网络

    sudo ./network.sh down

官方文档

部署参考1

部署参考2

猜你喜欢

转载自blog.csdn.net/qq_43271194/article/details/115470220