安装hyperledger fabric1.4.0环境

安装fabric需要先安装好docker和docker compose,go和node。
以上环境搭建好后,我们开始通过git搭建fabric。

一、 获取源码

1. 生成目录

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

2. git克隆源码

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

3. 切换版本到1.4

$ git checkout v1.4.0

最终目录如下:


图片.png

二、 安装fabric-sample,获取fabric-tools的工具

1. 获取fabric-sample示例

进入脚本文件夹

$ cd $HOME/gowork/src/github.com/hyperledger/fabric/scripts

使用bootsrap.sh安装fabric-sample(执行此命令前,确认docker已启动systemctl start docker,且当前用户有权限运行docker,使用docker ps验证,确保git客户端已安装 sudo apt-get install git)

$ ./bootstrap.sh

等待fabric-sample安装完毕。

2. 将fabric构建网络时用到的tools包括cryptogen,peer等工具移动到外部目录且配置到/etc/profile环境变量中

进入到HOME/gowork/src/github.com/hyperledger/fabric目录下
移动结果如下:

图片.png

配置环境变量

$ sudo vim /etc/profile

在文件末尾添加

export PATH=/home/cc/gowork/src/github.com/hyperledger/fabric/bin:$PATH

保存文件后,更新环境变量

source /etc/profile

三、 运行示例

进入 fabric-sample/first-network目录
执行./byfn.sh down先清理下防止以前运行过
再执行./byfn.sh up 如果能看到以下结果就是两个Org,四个Peer的区块链网络运行成功了
运行示例需要上述的fabric-tools的工具所在的bin文件夹的环境变量正确配置。
执行成功后,会看到以下结果

Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
Removing cli                    ... done
Removing peer0.org1.example.com ... done
Removing orderer.example.com    ... done
Removing network net_byfn
WARNING: Network net_byfn not found.
Removing volume net_orderer.example.com
WARNING: Volume net_orderer.example.com not found.
Removing volume net_peer0.org1.example.com
WARNING: Volume net_peer0.org1.example.com not found.
Removing volume net_peer1.org1.example.com
WARNING: Volume net_peer1.org1.example.com not found.
Removing volume net_peer0.org2.example.com
WARNING: Volume net_peer0.org2.example.com not found.
Removing volume net_peer1.org2.example.com
WARNING: Volume net_peer1.org2.example.com not found.
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
---- No containers available for deletion ----
---- No images available for deletion ----
cc@cc-virtual-machine:~/gowork/src/github.com/hyperledger/fabric/scripts/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] y
proceeding ...
./byfn.sh: line 122: configtxlator: command not found
LOCAL_VERSION=
DOCKER_IMAGE_VERSION=1.4.0
=================== WARNING ===================
  Local fabric binaries and docker images are  
  out of  sync. This may cause problems.       
===============================================
cryptogen tool not found. exiting
cc@cc-virtual-machine:~/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network$ source /etc/profile
cc@cc-virtual-machine:~/gowork/src/github.com/hyperledger/fabric/scripts/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] y
proceeding ...
LOCAL_VERSION=1.4.0
DOCKER_IMAGE_VERSION=1.4.0
/home/cc/gowork/src/github.com/hyperledger/fabric/bin/cryptogen

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

/home/cc/gowork/src/github.com/hyperledger/fabric/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-03-10 17:26:32.777 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-10 17:26:32.865 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-03-10 17:26:32.865 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:32.905 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-03-10 17:26:32.905 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:32.907 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-03-10 17:26:32.927 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-03-10 17:26:32.995 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-10 17:26:33.039 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:33.082 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-03-10 17:26:33.082 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:33.082 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-03-10 17:26:33.084 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-03-10 17:26:33.142 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-10 17:26:33.208 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:33.275 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-03-10 17:26:33.275 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:33.275 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-03-10 17:26:33.275 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-03-10 17:26:33.355 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-03-10 17:26:33.461 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:33.559 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-03-10 17:26:33.559 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/cc/gowork/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-03-10 17:26:33.559 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-03-10 17:26:33.560 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

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 peer0.org2.example.com ... done
Creating orderer.example.com    ... done
Creating peer1.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating peer1.org1.example.com ... done
Creating cli                    ... done

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

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

Channel name : mychannel
Creating channel...
+ 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
+ res=0
+ set +x
2019-03-10 09:26:48.395 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:26:48.487 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-03-10 09:26:48.659 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:26:48.809 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-03-10 09:26:51.962 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:26:52.114 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-03-10 09:26:55.252 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:26:55.435 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-03-10 09:26:58.592 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:26:58.707 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-03-10 09:27:01.850 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:27:01.869 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-03-10 09:27:04.989 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-03-10 09:27:05.011 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-03-10 09:27:08.143 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-10 09:27:08.144 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-03-10 09:27:10.957 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response: 
===================== 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-03-10 09:27:11.112 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-10 09:27:11.113 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-03-10 09:27:11.437 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response: 
===================== Chaincode is installed on peer0.org2 ===================== 

Instantiating chaincode on peer0.org2...
+ 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'\'')'
+ res=0
+ set +x
2019-03-10 09:27:11.535 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-10 09:27:11.535 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"]}'
+ res=0
+ set +x
2019-03-10 09:28:26.815 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 
===================== 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-03-10 09:28:27.021 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-03-10 09:28:27.021 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-03-10 09:28:27.364 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response: 
===================== 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 =========== 


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

在上述脚本运行的同时,新打开一个终端,使用docker ps查看正运行在docker中的进程,有四个peer,一个orderer,fabric-tool,chaincode


图片.png

示例运行成功,fabric1.4的环境也算是构建成功了,下一步就要自己构建组织的关系,编写chaincode,来搭建属于自己的fabric应用。

参考资料

HyperLedger Fabric 1.4 基础环境搭建(7)
Hyperledger Fabric 1.2快速安装

你可能感兴趣的:(安装hyperledger fabric1.4.0环境)