Fabric 2.0 环境搭建

编程入门 行业动态 更新时间:2024-10-27 16:23:37

Fabric 2.0 <a href=https://www.elefans.com/category/jswz/34/1771403.html style=环境搭建"/>

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 / bionic main restricted universe multiverse
    deb / bionic-security main restricted universe multiverse
    deb / bionic-updates main restricted universe multiverse
    deb / bionic-proposed main restricted universe multiverse
    deb / bionic-backports main restricted universe multiverse
    deb-src / bionic main restricted universe multiverse
    deb-src / bionic-security main restricted universe multiverse
    deb-src / bionic-updates main restricted universe multiverse
    deb-src / bionic-proposed main restricted universe multiverse
    deb-src / 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.gz2.cd /usr/local3.sudo tar zxvf go1.16.3.linux-amd64.tar.gz
    
  3. 配置GO的环境变量

    打开配置文件

    sudo vim ~/.profile

    添加下面内容

    
    export PATH=$PATH:/usr/local/go/binexport GOROOT=/usr/local/goexport GOPATH=$HOME/goexport 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 | 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": [""]
    }
    
  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 ".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]"echoecho "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"echoecho "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=$1shift#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 releasestwo_digit_image_tag=$(echo "$three_digit_image_tag" | cut -d'.' -f1,2)while [[ $# -gt 0 ]]doimage_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"shiftdone
    }cloneSamplesRepo() {# clone (if needed) hyperledger/fabric-samples and checkout corresponding# version to the binaries and docker images to be downloadedif [ -d first-network ]; then# if we are in the fabric-samples repo, checkout corresponding versionecho "==> Already in fabric-samples repo"elif [ -d fabric-samples ]; then# if fabric-samples repo already cloned and in current directory,# cd fabric-samplesecho "===> Changing directory to fabric-samples"cd fabric-sampleselseecho "===> Cloning hyperledger/fabric-samples repo"git clone -b main .git && cd fabric-samplesfiif GIT_DIR=.git git rev-parse v${VERSION} >/dev/null 2>&1; thenecho "===> Checking out v${VERSION} of hyperledger/fabric-samples"git checkout -q v${VERSION}elseecho "fabric-samples v${VERSION} does not exist, defaulting main"git checkout -q mainfi
    }# This will download the .tar.gz
    download() {local BINARY_FILE=$1local URL=$2echo "===> Downloading: " "${URL}"curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz || rc=$?if [ -n "$rc" ]; thenecho "==> There was an error downloading the binary file."return 22elseecho "==> Done."fi
    }pullBinaries() {echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"download "${BINARY_FILE}" "${VERSION}/${BINARY_FILE}"if [ $? -eq 22 ]; thenechoecho "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"echoexitfiecho "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"download "${CA_BINARY_FILE}" "${CA_VERSION}/${CA_BINARY_FILE}"if [ $? -eq 22 ]; thenechoecho "------> ${CA_TAG} fabric-ca-client binary is not available to download  (Available from 1.1.0-rc1) <----"echoexitfi
    }pullDockerImages() {command -v docker >& /dev/nullNODOCKER=$?if [ "${NODOCKER}" == 0 ]; thenFABRIC_IMAGES=(peer orderer ccenv tools)case "$VERSION" in2.*)FABRIC_IMAGES+=(baseos)shift;;esacecho "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 hyperledgerelseecho "========================================================="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}" != "-" ]; thenVERSION=$1;shiftif [ -n "$1" ]  && [ "${1:0:1}" != "-" ]; thenCA_VERSION=$1;shiftif [ -n  "$1" ] && [ "${1:0:1}" != "-" ]; thenTHIRDPARTY_IMAGE_VERSION=$1;shiftfifi
    fi# prior to 1.2.0 architecture was determined by uname -m
    if [[ $VERSION =~ ^1\.[0-1]\.* ]]; thenexport 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"}"
    fiBINARY_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; docase "$opt" inh|\?)printHelpexit 0;;d)  DOCKER=false;;s)  SAMPLES=false;;b)  BINARIES=false;;esac
    doneif [ "$SAMPLES" == "true" ]; thenechoecho "Clone hyperledger/fabric-samples repo"echocloneSamplesRepo
    fi
    if [ "$BINARIES" == "true" ]; thenechoecho "Pull Hyperledger Fabric binaries"echopullBinaries
    fi
    if [ "$DOCKER" == "true" ]; thenechoecho "Pull Hyperledger Fabric docker images"echopullDockerImages
    fi
    

    脚本执行

    sudo ./bootstrap.sh

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

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

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

  1. fabric-samples

  2. 相关的二进制文件

  3. docker 镜像

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

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

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

    如图

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

    .4.9/hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz
    .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

更多推荐

Fabric 2.0 环境搭建

本文发布于:2023-07-28 15:44:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境   Fabric

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!