五、区块链学习-Hyperledger Fabric (基于release-1.0) 官方示例FirstNetwork搭建

Zahirah ·
更新时间:2024-09-20
· 662 次阅读

五、区块链学习-Hyperledger Fabric 基于release-1.0官方示例FirstNetwork搭建1、docker 镜像准备2、源码获取2.1 获取Hyperledger Fabric源码2.2 获取fabric-samples源码2.3 切换代码版本到release-1.03 、编译需要用到的两个插件3.1 准备工作3.2 编译configtxgen组件 用于创世区块以及通道配置3.3 编译cryptogen组件 用于生成证书3.4 可能会出现的错误3.5 验证两个工具是否编译成功4、first-network 目录结构4、生成所需配置文件5、docker 启动First NetWork6、关闭网络

这篇文章搭建fabric-samples示例网络环境。中间会遇到很多的问题。如果有可以补充的欢迎评论添加!
目前先基于release-1.0版本做实验,因为这个版本的资料比较多。

1、docker 镜像准备

为了搭建第一个网络环境 需要准备如下的docker镜像

REPOSITORY TAG IMAGE ID CREATED SIZE hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 2 years ago 1.32GB hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 2 years ago 179MB hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 2 years ago 182MB hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 2 years ago 238MB hyperledger/fabric-baseos x86_64-0.3.1 4b0cab202084 2 years ago 157MB

以hyperledger/fabric-peer镜像为例,使用docker pull命令来拉取镜像

docker pull hyperledger/fabric-ca:x86_64-1.0.0

其他的几个镜像也是这样拉取
因为现在是基于1.0版本的项目来启动的 所以用到的是一下几个镜像(注意版本 为 x86_64-1.0.0)但是当容器启动时 默认会使用 latest 版本镜像,如果没有就会下载镜像启动。由于版本不一致了 所以会报错

!!! Query result on PEER0 is INVALID !!! ================== ERROR !!! FAILED to execute End-2-End Scenario ==================

为了避免这个错误。用docker 的tag命令 将镜像重新命名一个latest 使用一下命令

docker tag hyperledger/fabric-tools:x86_64-1.0.0 hyperledger/fabric-tools:latest docker tag hyperledger/fabric-orderer:x86_64-1.0.0 hyperledger/fabric-orderer:latest docker tag hyperledger/fabric-peer:x86_64-1.0.0 hyperledger/fabric-peer:latest docker tag hyperledger/fabric-ca:x86_64-1.0.0 hyperledger/fabric-ca:latest

再次查看docker images 就能看到tag 为 latest的镜像了 但是 imageId 与 之前 x86_64-1.0.0版本的一致

hyperledger/fabric-tools latest 0403fd1c72c7 2 years ago 1.32GB hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 2 years ago 1.32GB hyperledger/fabric-orderer latest e317ca5638ba 2 years ago 179MB hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 2 years ago 179MB hyperledger/fabric-peer latest 6830dcd7b9b5 2 years ago 182MB hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 2 years ago 182MB hyperledger/fabric-ca latest a15c59ecda5b 2 years ago 238MB hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 2 years ago 238MB 2、源码获取 2.1 获取Hyperledger Fabric源码

源码地址:Fabric源码(GitHup)
执行clone命令获取源码

git clone https://github.com/hyperledger/fabric.git 2.2 获取fabric-samples源码

源码地址:fabric-samples源码(GitHup)
执行Clone命令获取源码

git clone https://github.com/hyperledger/fabric-samples.git 2.3 切换代码版本到release-1.0

通过不中2.2获取到源码,因为要基于release-1.0版本来搭建,所以需要将源码切换到release-1.0的分支上
请先备份代码

切换fabric分支

cd ~/fabric git checkout release-1.0

切换fabric-samples分支

cd ~/fabric-samples git checkout release-1.0 3 、编译需要用到的两个插件 3.1 准备工作

因为go语言在安装组件时会用到其他的依赖包,依赖包的查找是 先找$GOROOT路径再找 $GOPATH 所以需要将下载好的源码移动路径到 $GOPATH路径下

所以现在的源码路径为

$GOPATH/src/github.com/hyperledger/fabric $GOPATH/src/github.com/hyperledger/fabric-samples 3.2 编译configtxgen组件 用于创世区块以及通道配置

进入到configtxgen文件目录下

cd $GOPATH/src/github.com/hyperledger/fabric/common/configtx/tool/configtxgen

执行

go install 3.3 编译cryptogen组件 用于生成证书

进入到cryptogen目录下

cd $GOPATH/src/github.com/hyperledger/fabric/common/tools/cryptogen

执行

go install 3.4 可能会出现的错误 ------------------------------------------------------------------------------------ go build github . com / hyperledger / fabric / vendor / github . com / miek / pkcsll : invalid flag in # cgo LDFLAGS : - I / usr / ocal / share / Libtool 解决方法:go install --tags=nopkcs11 ------------------------------------------------------------------------------------ main.go:27:2: cannot find package "gopkg.in/alecthomas/kingpin.v2" in any of: 解决方法: go get gopkg.in/alecthomas/kingpin.v2 ------------------------------------------------------------------------------------ main.go:25:2: cannot find package "gopkg.in/yaml.v2" in any of: 解决放方法: go get gopkg.in/yaml.v2 ------------------------------------------------------------------------------------ go install: no install location for directory /Users/lh0811/Desktop/lh-starter/hyperledger/fabric/common/configtx/tool/configtxgen outside GOPATH For more details see: 'go help gopath' 解决方法: vi ~/.bash_profile 添加 export GOROOT="/usr/local/Cellar/go/1.12.9/libexec" export GOPATH="/Users/lh0811/go" export GOBIN=$GOPATH/bin export PATH=$PATH:$GOBIN 然后 source ~/.bash_profile ------------------------------------------------------------------------------------ 3.5 验证两个工具是否编译成功

两个工具都会安装到 $GOPATH/bin目录下

cd $GOPATH/bin ls 输出: configtxgen cryptogen 4、first-network 目录结构

先看first-network项目的目录结构

drwxr-xr-x 13 lh0811 staff 416 2 10 19:59 . drwxr-xr-x 15 lh0811 staff 480 2 10 19:59 .. -rw-r--r-- 1 lh0811 staff 42 2 10 15:38 .env //项目环境配置 -rw-r--r-- 1 lh0811 staff 335 2 10 15:38 README.md drwxr-xr-x 4 lh0811 staff 128 2 10 19:59 base //docker compose 的公共服务 -rwxr-xr-x 1 lh0811 staff 15108 2 10 19:59 byfn.sh //启动脚本 drwxr-xr-x 3 lh0811 staff 96 2 10 15:38 channel-artifacts -rw-r--r-- 1 lh0811 staff 5013 2 10 19:59 configtx.yaml //之前configtxgen 工具 根据该配置 生成配置文件 -rw-r--r-- 1 lh0811 staff 3858 2 10 19:59 crypto-config.yaml //之前cryptogen 工具 更加该配置 生成配置文件 -rw-r--r-- 1 lh0811 staff 3015 2 10 19:59 docker-compose-cli.yaml // docker compose 启动网络配置脚本 -rw-r--r-- 1 lh0811 staff 4604 2 10 19:59 docker-compose-couch.yaml // docker compose 启动网络配置脚本 -rw-r--r-- 1 lh0811 staff 2883 2 10 15:38 docker-compose-e2e-template.yaml // docker compose 启动网络配置脚本 drwxr-xr-x 3 lh0811 staff 96 2 10 19:59 scripts // 提供测试 fabric的一下脚本

目录中的byfn.sh 为该项目的启动脚本

启动脚本 提供的功能
byfn.sh -h

byfn.sh -m up|down|restart|generate [-c ] [-t ] [-d ] [-f ] [-s ] [-i ] byfn.sh -h|--help (print this message) -m - one of 'up', 'down', 'restart' or 'generate' - 'up' - bring up the network with docker-compose up 启动docker-compose 网络 - 'down' - clear the network with docker-compose down 关闭docker-compose 网络 - 'restart' - restart the network 重启docker-compose 网络 - 'generate' - generate required certificates and genesis block 生成第一个创世区块 -c - channel name to use (defaults to "mychannel") channel的名称 默认是mychannel -t - CLI timeout duration in microseconds (defaults to 10000) cli超时时间 默认10000ms -d - delay duration in seconds (defaults to 3) -f - specify which docker-compose file use (defaults to docker-compose-cli.yaml) -s - the database backend to use: goleveldb (default) or couchdb 数据库引擎选择 一般使用默认的goleveldb -i - pass the image tag to launch the network using the tag: 1.0.1, 1.0.2, 1.0.3, 1.0.4 (defaults to latest) 指定启动images版本 Typically, one would first generate the required certificates and genesis block, then bring up the network. e.g.: 通常,首先生成所需的证书和genesis块,然后启动网络。 一般使用如下的方法来测试 byfn.sh -m generate -c mychannel // 先生成相关证书和配置文件使用之前编译的组件 -c 是指定channel名称,默认就是mychannel byfn.sh -m up -c mychannel -s couchdb // 启动网络 一般使用默认的数据库引擎 所以一般不写-s byfn.sh -m up -c mychannel -s couchdb -i 1.0.6 // -i 是指定镜像版本启动,尝试过 会失败 所以还是修改精选tag到laster byfn.sh -m down -c mychannel // 关闭网络 这一步会删掉生成的docker 容器 如果不需要特殊的配置 用一下的三个命令就够了 也就是默认的配置 Taking all defaults: byfn.sh -m generate byfn.sh -m up byfn.sh -m down 4、生成所需配置文件

按照byfn.sh -h提供的帮助文档。

进入first-network目录

cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network

执行生成证书和配置文件的命令

./byfn.sh -m generate

输出

#询问客户端超时时间 Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' Continue (y/n)? y 键入y继续

输出相关证书生产过程

########################################################## ##### Generate certificates using cryptogen tool ######### ##### 生成证书使用cryptogen工具 ######### ########################################################## org1.example.com #组织1 org2.example.com #组织2 /Users/lh0811/go/bin/configtxgen #使用之前编译好的工具 生成 创世区块 以及通道配置 ########################################################## ######### Generating Orderer Genesis block ############## ######### 生成创世区块 ############## ########################################################## 2020-02-10 21:10:24.817 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-02-10 21:10:24.846 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block 2020-02-10 21:10:24.849 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block # 生成 创世区块 ################################################################# ### Generating channel configuration transaction 'channel.tx' ### ### 生成 channel并写入到 'channel.tx' 文件### ################################################################# 2020-02-10 21:10:24.893 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-02-10 21:10:24.898 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx 2020-02-10 21:10:24.899 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx # 生成channel 并写入channel配置 ################################################################# ####### Generating anchor peer update for Org1MSP ########## ####### 生成Org1MSP的锚节点 ########## ################################################################# 2020-02-10 21:10:24.940 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-02-10 21:10:24.944 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2020-02-10 21:10:24.944 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update # 生成组织1 的锚节点 ################################################################# ####### Generating anchor peer update for Org2MSP ########## ####### 生成Org2MSP的锚节点 ########## ################################################################# 2020-02-10 21:10:24.974 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2020-02-10 21:10:24.977 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2020-02-10 21:10:24.977 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update # 生成组织2 的锚节点

查看生成的文件

channel-artifacts 文件夹下保存了channel配置 创世区块信息 以及两个组织的锚节点信息 crypto-config 文件夹下生成了两个配置文件夹 保存了其生成的证书文件 crypto-config/ordererOrganizations: crypto-config/peerOrganizations: 5、docker 启动First NetWork

按照byfn.sh -h提供的帮助文档。

进入first-network目录

cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network

执行生成启动网络命令

./byfn.sh -m up #询问客户端超时时间 Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' Continue (y/n)? y 键入y继续

输出相关日志

出现一下日志 则是节点启动完成 ========= All GOOD, BYFN execution completed =========== _____ _ _ ____ | ____| | \ | | | _ \ | _| | \| | | | | | | |___ | |\ | | |_| | |_____| |_| \_| |____/

查看docker容器

docker ps

会看到docker-compose 启动的容器列表

6、关闭网络

按照byfn.sh -h提供的帮助文档。

进入first-network目录

cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network

执行关闭网络命令

./byfn.sh -m down

再次查看docker 容器

docker ps -a

所有的容器都删除了


作者:LH_0811



区块链学习 hyperledger RELEASE 学习 fabric 区块链

需要 登录 后方可回复, 如果你还没有账号请 注册新账号