Fabric部署基础篇

 

(本人发现写博客比较浪费时间,排版比较麻烦,进阶篇等其它文章,请到我的资源那里下载)

本文为基础篇,阅读对象是从未动手实际操作过fabric的新手。

通过本文,读者将对fabric有个基本的感性认识,不需要任何配置,就可以部署第一个简单的Fabric网络[1]

一、环境说明

主机操作系统不限,网络搭建在虚拟机中,所有跟Fabric相关的操作系统一律使用CentOS 7.5或更高版本。目前CentOS最高版本是7.6,下载地址http://mirrors.zju.edu.cn/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso

虚拟机硬件推荐2CPU、2G内存和50G存储空间。

二、环境准备

Fabric网络所需的软件并不多,单机网络只要go、docker和docker-compose,在多机环境中,连docker-compose都不需要。

1. 安装go

  • 下载安装包
wget https://storage.googleapis.com/golang/go1.12.3.linux-amd64.tar.gz

建议下载最新版,Fabric每次版本升级,都有对应的go最低版本需求。查看go最新版本的方法有两个,一是在浏览器内输入“https://storage.googleapis.com/golang”,然后在页面内查找最新版本号;二是直接尝试下载高版本,如果下载失败,再调低版本尝试。

  • 解压
sudo tar -zxvf go1.12.3.linux-amd64.tar.gz -C /usr/local/
  • 验证安装

“sudo vi /etc/profile”编辑文件,在末尾添加

export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export GOPATH=/opt/gopath
export PATH=$GOBIN:$PATH

执行“source /etc/profile”使环境变量立即生效,再执行“go version”查看版本。

source /etc/profile
go version

2. 安装docker

 

  • 安装相关工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  • 配置docker仓库
sudo yum-config-manager --add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

如果上面这个命令失败,可以使用国内镜像:

sudo yum-config-manager --add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • 安装
sudo yum install -y docker-ce
  • 验证安装
docker version

3. 安装docker-compose

直接下载二进制文件,放到“/usr/local/bin”下即可:

curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v

 

三、部署网络

部署Fabric网络,只需要Fabric可执行文件和部署脚本就行,但是直接下载可执行文件速度非常慢,所以本人的方法是直接下载源代码,自己编译,而且源代码在后期还会用到。

1. 下载源代码

为了跟github上的目录一致,创建源代码目录:

mkdir -p $GOPATH/src/github.com/hyperledger

安装git

yum install -y git

下载Fabric源码(此时的版本是1.4)

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

2. 编译源代码

在编译之前,需要安装一些工具:

sudo yum install -y gcc libtool libltdl-dev libtool-ltdl-devel openssl

然后开始编译:

cd $GOPATH/src/github.com/hyperledger/fabric
make release

编译需要一两分钟,等待编译完成后,在Fabric目录下会生成一个目录“release/linux-amd64/bin/”,里面有一系列可执行文件。

  1. 部署网络

下载fabric-samples源码,“sample”即样例,里面有很多样例:

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

在运行样例中的脚本之前,需要设置一下可执行文件[2]目录:

cd $GOPATH/src/github.com/hyperledger/fabric-samples
ln -s ../fabric/release/linux-amd64/bin/ bin

启动网络:

cd first-network/
./byfn.sh generate
sudo ./byfn.sh up

 “./byfn.sh generate”的作用是生成网络所需的证书、创世快和配置文件等材料。执行./byfn.sh up之后,可以看到整个网络的创建过程和链码的运作细节,详见附录1附录2

最后,如果不再需要这个网络了,记得清理:

./byfn.sh down

 

基础篇到这里就结束了,有点linux基础的读者跟着操作基本上没有问题。如果在操作过程中还是碰到问题,不要先急着问度娘,Fabric中的问题在百度中基本上是找不到解决办法的,因为它的报错信息本身就不完善。最好办法就是自己分析问题,寻找思路,实践证明这种办法最管用。

有兴趣的同学可以研读byfn.sh了,读懂它的话,对后续工作是很有帮助的,只不过脚本比较复杂,需要花点功夫。

 

 

附录:

1. ./byfn.sh generate

[ccc@localhost 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] y

proceeding ...

/opt/gopath/src/github.com/hyperledger/fabric-samples/bin/cryptogen

 

##########################################################

##### Generate certificates using cryptogen tool #########

##########################################################

+ cryptogen generate --config=./crypto-config.yaml

org1.example.com

org2.example.com

+ res=0

+ set +x

 

/opt/gopath/src/github.com/hyperledger/fabric-samples/bin/configtxgen

##########################################################

#########  Generating Orderer Genesis block ##############

##########################################################

CONSENSUS_TYPE=solo

+ '[' solo == solo ']'

+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block

2019-04-25 11:58:33.512 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration

2019-04-25 11:58:33.535 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo

2019-04-25 11:58:33.535 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.556 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo

2019-04-25 11:58:33.556 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.557 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block

2019-04-25 11:58:33.558 CST [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-04-25 11:58:33.620 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration

2019-04-25 11:58:33.642 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.664 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo

2019-04-25 11:58:33.664 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.664 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx

2019-04-25 11:58:33.665 CST [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-04-25 11:58:33.687 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration

2019-04-25 11:58:33.712 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.733 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo

2019-04-25 11:58:33.733 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.733 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update

2019-04-25 11:58:33.733 CST [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-04-25 11:58:33.755 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration

2019-04-25 11:58:33.778 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.799 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo

2019-04-25 11:58:33.799 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml

2019-04-25 11:58:33.799 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update

2019-04-25 11:58:33.799 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update

+ res=0

+ set +x

2. ./byfn.sh up

 [ccc@localhost first-network]$ ./byfn.sh up

Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds

Continue? [Y/n] y

proceeding ...

LOCAL_VERSION=1.4.1

DOCKER_IMAGE_VERSION=1.4.1

Creating network "net_byfn" with the default driver

Creating volume "net_orderer.example.com" with default driver

Creating volume "net_peer0.org1.example.com" with default driver

Creating volume "net_peer1.org1.example.com" with default driver

Creating volume "net_peer0.org2.example.com" with default driver

Creating volume "net_peer1.org2.example.com" with default driver

Creating orderer.example.com    ... done

Creating peer1.org1.example.com ... done

Creating peer0.org1.example.com ... done

Creating peer0.org2.example.com ... done

Creating peer1.org2.example.com ... done

Creating cli                    ... done

 

 ____    _____      _      ____    _____

/ ___|  |_   _|    / \    |  _ \  |_   _|

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

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

|____/    |_|   /_/   \_\ |_| \_\   |_| 

 

Build your first network (BYFN) end-to-end test

 

+ peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Channel name : mychannel

Creating channel...

+ res=0

+ set +x

2019-04-25 04:01:20.280 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:20.302 UTC [cli.common] readBlock -> INFO 002 Received block: 0

===================== Channel 'mychannel' created =====================

 

Having all peers join the channel...

+ peer channel join -b mychannel.block

+ res=0

+ set +x

2019-04-25 04:01:20.390 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:20.444 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

===================== peer0.org1 joined channel 'mychannel' =====================

 

+ peer channel join -b mychannel.block

+ res=0

+ set +x

2019-04-25 04:01:23.509 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:23.550 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

===================== peer1.org1 joined channel 'mychannel' =====================

 

+ peer channel join -b mychannel.block

+ res=0

+ set +x

2019-04-25 04:01:26.628 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:26.673 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

===================== peer0.org2 joined channel 'mychannel' =====================

 

+ peer channel join -b mychannel.block

+ res=0

+ set +x

2019-04-25 04:01:29.764 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:29.824 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

===================== peer1.org2 joined channel 'mychannel' =====================

 

Updating anchor peers for org1...

+ peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

+ res=0

+ set +x

2019-04-25 04:01:32.897 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:32.905 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update

===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' =====================

 

Updating anchor peers for org2...

+ peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

+ res=0

+ set +x

2019-04-25 04:01:36.039 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-04-25 04:01:36.055 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update

===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' =====================

 

Installing chaincode on peer0.org1...

+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/

+ res=0

+ set +x

2019-04-25 04:01:39.145 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc

2019-04-25 04:01:39.145 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

2019-04-25 04:01:39.614 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >

===================== Chaincode is installed on peer0.org1 =====================

 

Install chaincode on peer0.org2...

+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/

+ res=0

+ set +x

2019-04-25 04:01:39.673 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc

2019-04-25 04:01:39.673 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

2019-04-25 04:01:39.846 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >

+ peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'

===================== Chaincode is installed on peer0.org2 =====================

 

Instantiating chaincode on peer0.org2...

+ res=0

+ set +x

2019-04-25 04:01:39.905 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc

2019-04-25 04:01:39.905 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' =====================

 

Querying chaincode on peer0.org1...

===================== Querying on peer0.org1 on channel 'mychannel'... =====================

Attempting to Query peer0.org1 ...3 secs

+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

+ res=0

+ set +x

 

100

===================== Query successful on peer0.org1 on channel 'mychannel' =====================

Sending invoke transaction on peer0.org1 peer0.org2...

+ peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'

2019-04-25 04:02:09.753 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

+ res=0

+ set +x

===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' =====================

 

Installing chaincode on peer1.org2...

+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/

+ res=0

+ set +x

2019-04-25 04:02:09.816 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc

2019-04-25 04:02:09.816 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

2019-04-25 04:02:09.961 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >

===================== Chaincode is installed on peer1.org2 =====================

 

Querying chaincode on peer1.org2...

===================== Querying on peer1.org2 on channel 'mychannel'... =====================

Attempting to Query peer1.org2 ...3 secs

+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

+ res=0

+ set +x

 

90

===================== Query successful on peer1.org2 on channel 'mychannel' =====================

 

========= All GOOD, BYFN execution completed ===========

 

 

 _____   _   _   ____  

| ____| | \ | | |  _ \ 

|  _|   |  \| | | | | |

| |___  | |\  | | |_| |

|_____| |_| \_| |____/ 

 

 

 

 

 

 


[1] 这里的网络指fabric区块链网络,不是英特网。

[2] fabric目录的编译结果

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐