Fabric环境搭建及失败解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bondsui/article/details/86581691

官方文档:latest
https://hyperledger-fabric.readthedocs.io/en/latest/

知识准备

学习fabric之前,希望已具备以下知识,便于进一步学习fabric

  • git基本命令

  • linux基本操作

    • 基本命令
    • 修改环境变量
    • 安装文件 ,载文件等
  • go语言基础,链码编写,虽然支持其他语言,但go集成最好最稳定

  • nodejs,客户端编写

  • 区块链、密码学基本概念

    • 账本、共识、加密算法、证书
  • docker

    • 基本操作
    • 镜像管理
    • 容器管理
    • 数据卷管理
  • docker-compose
    *compose文件编写

  • fabric基本概念:

    • 证书、创始块、通道、组织、节点
    • msp账号、链码
    • solo、kafka

环境准备

首先确保以下已经安装,且版本正确,fabric镜像较多,请确保系统硬盘或者虚拟机硬盘大小足够。 本例以ubuntu系统为例,大小8g左右(包含全部环境及镜像文件,系统为18.04.1-server,虚拟机文件太大,无法上传,如需要,单独联系)
在这里插入图片描述

  1. 安装基础软件,curl git python 等
    lrzsz可选,secureCRT 链接ubuntu传输软件使用
# 安装基本软件
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates curl git software-properties-common lrzsz python-pip -y
  1. 安装Docker docker-compose 17.06以上
    安装向导:https://docs.docker.com/install/linux/docker-ce/ubuntu/

  2. 安装golang

$ wget https://dl.google.com/go/go1.11.linux-amd64.tar.gz
$ sudo tar zxvf go1.11.linux-amd64.tar.gz -C /usr/local
$ mkdir $HOME/go
$ vim ~/.bashrc
# 5. 增加下面的环境变量,保存退出
	export GOROOT=/usr/local/go
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
$ source ~/.bashrc  
$ go version
  1. 安装Node.js 、NPM version 8.x
    https://nodejs.org/en/download/
$ wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz
$ sudo tar xvf node-v8.11.4-linux-x64.tar.xz -C /opt
$ sudo vim /etc/profile

添加环境变量
export NODEJS_HOME=/opt/node-v8.11.4-linux-x64
export PATH= P A T H : PATH: NODEJS_HOME/bin

node -v 查看版本

安装fabric

命令模板
$ curl -sSL http://bit.ly/2ysbOFE | bash -s <fabric> <fabric-ca> <thirdparty>
-sSL 脚本文件 该脚本需要安全上墙
- fabric: fabric的版本
- fabric-ca: fabric-ca的版本
- thirdparty: 第三方库的版本


$ mkdir hyperledger-fabric # 创建放置目录
$ cd hyperledger-fabric
# 使用1.4.0版本
$ curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.0
# 下同
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.4.0 1.4.0 0.4.14

漫长的等待,发现

nstalling Hyperledger Fabric binaries

===> Downloading version 1.4.0 platform specific fabric binaries
===> Downloading:  https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.4.0/hyperledger-fabric-linux-amd64-1.4.0.tar.gz
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
​       Dload  Upload   Total   Spent    Left  Speed

可以使用浏览器、迅雷等其他下载工具下载此文件,
一共有两个文件需要下载
1. hyperledger-fabric-linux-amd64-1.4.0.tar.gz
2. hyperledger-fabric-ca-linux-amd64-1.4.0.tar.gz

下载完成后放入~/hyperledger-fabric/fabric-samples文件夹

继续下载

# 复制到 ~/hyperledger-fabric/fabric-samples 后
$ cd ~/hyperledger-fabric
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.4.0 1.4.0 0.4.14
Installing Hyperledger Fabric binaries

===> Downloading version 1.4.0 platform specific fabric binaries
===> Downloading:  https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.4.0/hyperledger-fabric-linux-amd64-1.4.0.tar.gz
==> Partial binary file found. Resuming download...
==> File downloaded. Verifying the md5sum...
==> Extracting hyperledger-fabric-linux-amd64-1.4.0.tar.gz...
==> Done.
===> Downloading version 1.4.0 platform specific fabric-ca-client binary
===> Downloading:  https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.4.0/hyperledger-fabric-ca-linux-amd64-1.4.0.tar.gz
==> Partial binary file found. Resuming download...
==> File downloaded. Verifying the md5sum...
==> Extracting hyperledger-fabric-ca-linux-amd64-1.4.0.tar.gz...
==> Done.

Installing Hyperledger Fabric docker images

===> Pulling fabric Images
==> FABRIC IMAGE: peer

1.4.0: Pulling from hyperledger/fabric-peer
Digest: sha256:9707c97f787de1d4d6dd60994d6b8ea2e5cc28b0f42e6849df3fb41c64b41372
Status: Image is up to date for hyperledger/fabric-peer:1.4.0
==> FABRIC IMAGE: orderer

1.4.0: Pulling from hyperledger/fabric-orderer
Digest: sha256:644265186b4887c7d9dcb91895124ccead3c0125c2c4f9eadc421dc9555d7495
Status: Image is up to date for hyperledger/fabric-orderer:1.4.0
==> FABRIC IMAGE: ccenv

1.4.0: Pulling from hyperledger/fabric-ccenv
Digest: sha256:6d797cac9fd39d4c9964fbcf02e47137ce8ae321af60832c41d511f0c7ed4d3e
Status: Image is up to date for hyperledger/fabric-ccenv:1.4.0
==> FABRIC IMAGE: javaenv

1.4.0: Pulling from hyperledger/fabric-javaenv
Digest: sha256:f392f0c568e515b3e14c9afce14340ba18bc456cfd831ffcb4f72db58dbbcc34
Status: Image is up to date for hyperledger/fabric-javaenv:1.4.0
==> FABRIC IMAGE: tools

1.4.0: Pulling from hyperledger/fabric-tools
Digest: sha256:aee256916d0cb938d8023fa32ed2745991d32cfe79018e360f1720707ebfbdb5
Status: Image is up to date for hyperledger/fabric-tools:1.4.0
===> Pulling fabric ca Image
==> FABRIC CA IMAGE

1.4.0: Pulling from hyperledger/fabric-ca
Digest: sha256:c1dce534d9e9202697e0aaad7c5521d958700fda0b05127dafb9333c22e15f74
Status: Image is up to date for hyperledger/fabric-ca:1.4.0
===> Pulling thirdparty docker images
==> THIRDPARTY DOCKER IMAGE: couchdb

0.4.14: Pulling from hyperledger/fabric-couchdb
Digest: sha256:021c7e4a5047432d892fbdf7d5220d3049ff4e8b436fd481bb08e41871f1aac7
Status: Image is up to date for hyperledger/fabric-couchdb:0.4.14
==> THIRDPARTY DOCKER IMAGE: kafka

0.4.14: Pulling from hyperledger/fabric-kafka
Digest: sha256:82ac81938320d05a538b9fd6de0fcd54b5a999188cf9b08822cf25f9ad7970a9
Status: Image is up to date for hyperledger/fabric-kafka:0.4.14
==> THIRDPARTY DOCKER IMAGE: zookeeper

0.4.14: Pulling from hyperledger/fabric-zookeeper
Digest: sha256:ac342ed87997175bfd557c53f7ffc6e0f8aa32bcaebb54a9bd55fb4c7f954802
Status: Image is up to date for hyperledger/fabric-zookeeper:0.4.14

===> List out hyperledger docker images
hyperledger/fabric-javaenv     1.4.0               3d91b3bf7118        6 days ago          1.75GB
hyperledger/fabric-javaenv     latest              3d91b3bf7118        6 days ago          1.75GB
hyperledger/fabric-tools       1.4.0               0a44f4261a55        11 days ago         1.56GB
hyperledger/fabric-tools       latest              0a44f4261a55        11 days ago         1.56GB
hyperledger/fabric-ccenv       1.4.0               5b31d55f5f3a        11 days ago         1.43GB
hyperledger/fabric-ccenv       latest              5b31d55f5f3a        11 days ago         1.43GB
hyperledger/fabric-orderer     1.4.0               54f372205580        11 days ago         150MB
hyperledger/fabric-orderer     latest              54f372205580        11 days ago         150MB
hyperledger/fabric-peer        1.4.0               304fac59b501        11 days ago         157MB
hyperledger/fabric-peer        latest              304fac59b501        11 days ago         157MB
hyperledger/fabric-ca          1.4.0               1a804ab74f58        11 days ago         244MB
hyperledger/fabric-ca          latest              1a804ab74f58        11 days ago         244MB
hyperledger/fabric-zookeeper   0.4.14              d36da0db87a4        3 months ago        1.43GB
hyperledger/fabric-zookeeper   latest              d36da0db87a4        3 months ago        1.43GB
hyperledger/fabric-kafka       0.4.14              a3b095201c66        3 months ago        1.44GB
hyperledger/fabric-kafka       latest              a3b095201c66        3 months ago        1.44GB
hyperledger/fabric-couchdb     0.4.14              f14f97292b4c        3 months ago        1.5GB

下载完成后一共 8个镜像
hyperledger/fabric-javaenv chaincode java环境
hyperledger/fabric-tools 工具包
hyperledger/fabric-ccenv chaincode go环境
hyperledger/fabric-orderer order节点
hyperledger/fabric-peer peer节点
hyperledger/fabric-ca ca
hyperledger/fabric-zookeeper zookeeper集群,设置kafka时用到
hyperledger/fabric-kafka kafuka

环境变量设置

将二进制文件拷贝到 /usr/local/bin 目录下

sui@server_root:~/hyperledger-fabric/fabric-samples/bin$ tree
.
├── configtxgen
├── configtxlator
├── cryptogen
├── discover
├── fabric-ca-client
├── get-docker-images.sh
├── idemixgen
├── orderer
└── peer
$ sudo cp * /usr/local/bin

下载 fabric-samples

git clone https://github.com/hyperledger/fabric-samples

下载后进入到

sui@server_root:~/hyperledger-fabric/fabric-samples/fabric-samples/first-network$ tree -L 2
.
├── base
│   ├── docker-compose-base.yaml
│   └── peer-base.yaml
├── byfn.sh #启动脚本
├── channel-artifacts
│   ├── channel.tx
│   ├── genesis.block
│   ├── Org1MSPanchors.tx
│   └── Org2MSPanchors.tx
├── configtx.yaml
├── crypto-config
│   ├── ordererOrganizations
│   └── peerOrganizations
├── crypto-config.yaml
├── docker-compose-cli.yaml
├── docker-compose-couch-org3.yaml
├── docker-compose-couch.yaml
├── docker-compose-e2e-template.yaml
├── docker-compose-e2e.yaml
├── docker-compose-kafka.yaml
├── docker-compose-org3.yaml
├── eyfn.sh
├── org3-artifacts
│   ├── configtx.yaml
│   └── org3-crypto.yaml
├── README.md
└── scripts
    ├── script.sh
    ├── step1org3.sh
    ├── step2org3.sh
    ├── step3org3.sh
    ├── testorg3.sh
    ├── upgrade_to_v14.sh
    └── utils.sh

7 directories, 27 files
sui@server_root:~/hyperledger-fabric/fabric-samples/fabric-samples/first-network$ 

./byfn.sh generate 生成
./byfn.sh up启动
./byfn.sh down 停止

生成配置文件

sui@server_root:~/hyperledger-fabric/fabric-samples/fabric-samples/first-network$ ./byfn.sh generate
Generating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] 
proceeding ...
/usr/local/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

/usr/local/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-01-21 09:38:50.298 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-21 09:38:50.326 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-01-21 09:38:50.326 UTC [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.351 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-01-21 09:38:50.352 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.354 UTC [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-01-21 09:38:50.354 UTC [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2019-01-21 09:38:50.385 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-21 09:38:50.419 UTC [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.446 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-01-21 09:38:50.446 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.447 UTC [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-01-21 09:38:50.450 UTC [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2019-01-21 09:38:50.480 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-21 09:38:50.506 UTC [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.535 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-01-21 09:38:50.536 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.536 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-01-21 09:38:50.537 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2019-01-21 09:38:50.570 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-21 09:38:50.600 UTC [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.626 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-01-21 09:38:50.626 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/sui/hyperledger-fabric/fabric-samples/fabric-samples/first-network/configtx.yaml
2019-01-21 09:38:50.627 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-01-21 09:38:50.627 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

启动网络./byfn.sh up

sui@server_root:~/hyperledger-fabric/fabric-samples/fabric-samples/first-network$ ./byfn.sh up
Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] 
proceeding ...
LOCAL_VERSION=1.4.0
DOCKER_IMAGE_VERSION=1.4.0
Creating network "net_byfn" with the default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_orderer.example.com" with default driver
Creating peer1.org2.example.com ... done
Creating peer1.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done

 ____    _____      _      ____    _____ 
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |  
 ___) |   | |    / ___ \  |  _ <    | |  
|____/    |_|   /_/   \_\ |_| \_\   |_|  

看到start后耐心等待 节点操作及连码安装初始化查询

整个操作流程

docker启动 节点后


/ | | | / \ | _ \ | |
_
\ | | / _ \ | |
) | | |
) | | | / ___ \ | _ < | |
|
/ || // _\ || _\ |_|

创建通道 mychannel,生成通道文件
peer0.org1 加入通道 mychannel
peer1.org1 加入通道 mychannel
peer0.org2 加入通道 mychannel
peer1.org2 加入通道 mychannel
更新锚节点 Org1MSP
更新锚节点 Org2MSP
安装连码到 peer0.org1
安装连码到 peer0.org2
初始化连码 instantiated on peer0.org2 on channel ‘mychannel’
查询连码 peer0.org1…
发送交易 transaction on peer0.org1 peer0.org2…
查询交易

查看启动的镜像

1个order、4个peer、一个cli、3个chaincode (链码是运行在容器上的)

sui@server_root:~/hyperledger-fabric/fabric-samples/fabric-samples/first-network$ 
sui@server_root:~/hyperledger-fabric/fabric-samples/fabric-samples/first-network$ docker ps
CONTAINER ID        IMAGE                                                                                                  COMMAND                  CREATED             STATUS              PORTS                                              NAMES
3155b041b7b2        dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"   12 minutes ago      Up 12 minutes                                                          dev-peer1.org2.example.com-mycc-1.0
63cc0666ab59        dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"   13 minutes ago      Up 13 minutes                                                          dev-peer0.org1.example.com-mycc-1.0
37068532709e        dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b   "chaincode -peer.add…"   14 minutes ago      Up 14 minutes                                                          dev-peer0.org2.example.com-mycc-1.0
a0ee3aff031e        hyperledger/fabric-tools:latest                                                                        "/bin/bash"              15 minutes ago      Up 15 minutes                                                          cli
4a65cbeddb44        hyperledger/fabric-peer:latest                                                                         "peer node start"        15 minutes ago      Up 15 minutes       0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
b3d76edb8d70        hyperledger/fabric-peer:latest                                                                         "peer node start"        15 minutes ago      Up 15 minutes       0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
5f7b9f7201db        hyperledger/fabric-peer:latest                                                                         "peer node start"        15 minutes ago      Up 15 minutes       0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
25d249dab78f        hyperledger/fabric-orderer:latest                                                                      "orderer"                15 minutes ago      Up 15 minutes       0.0.0.0:7050->7050/tcp                             orderer.example.com
bb5dec251998        hyperledger/fabric-peer:latest                                                                         "peer node start"        15 minutes ago      Up 15 minutes       0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com

猜你喜欢

转载自blog.csdn.net/bondsui/article/details/86581691