部署 Hyperledger Fabric v1.4 Solo模式

介绍简单Fabric,Fabric 是 Hyperledger 超级账本中的一个子项目,由 Linux 基金会主办。它提供了一个开发区块链应用程序的框架。它的部署架构有两种模式:Solo模式,即单Orderers架构;Kafka模式,即多Orderers架构。

以下我们部署一个Solo模式的测试环境:

1、基础环境准备

1、主机一台

最低配:2C4G

推荐配置:4C8G

2、安装Docker

# 列出所有的docker版本
yum list docker-ce --showduplicates | sort -r
# 安装指定的docker版本
yum install docker-ce-<VERSION_STRING>

3、安装Docker-compose

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

4、安装Python3

# 下载python3
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
# 解压、编译即可

5、安装Go

# 下载Go
wget https://studygolang.com/dl/golang/go1.10.1.linux-amd64.tar.gz
# 解压、设置环境变量
export GOROOT=/root/go
export GOPATH=/root/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

6、安装nodejs、npm

# 下载nodejs
wget https://npm.taobao.org/mirrors/node/v8.0.0/node-v8.0.0-linux-x64.tar.xz
# 解压、配置软链接
ln -s /root/node-v8.0.0-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v8.0.0-linux-x64/bin/npm /usr/local/bin/npm

2、安装Fabric网络

1、Fabric源码下载

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

2、下载需要的二进制执行文件

wget 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
# 解压、配置PATH
export PATH=$PATH:/root/fabric-samples/bin

3、生成网络工件

# 进入到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 ...
/root/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

/root/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-23 08:45:23.484 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-04-23 08:45:23.599 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-04-23 08:45:23.600 UTC [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:23.743 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-04-23 08:45:23.744 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:23.754 UTC [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-04-23 08:45:23.755 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-04-23 08:45:23.830 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-04-23 08:45:23.968 UTC [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:24.101 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-04-23 08:45:24.101 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:24.101 UTC [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-04-23 08:45:24.105 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-04-23 08:45:24.157 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-04-23 08:45:24.263 UTC [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:24.370 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-04-23 08:45:24.370 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:24.370 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-04-23 08:45:24.370 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-04-23 08:45:24.417 UTC [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-04-23 08:45:24.539 UTC [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:24.691 UTC [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-04-23 08:45:24.691 UTC [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/fabric-samples/first-network/configtx.yaml
2019-04-23 08:45:24.691 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-04-23 08:45:24.692 UTC [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

此处必须保证下载的Fabric源码和二进制文件工具版本一致,否则会报如下错

configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-04-23 19:21:42.767 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2019-04-23 19:21:42.911 CST [common/tools/configtxgen/localconfig] Load -> CRIT 002 Error unmarshaling config into struct:  1 error(s) decoding:

* 'Profiles[SampleMultiNodeEtcdRaft].Orderer' has invalid keys: EtcdRaft
2019-04-23 19:21:42.911 CST [common/tools/configtxgen] func1 -> CRIT 003 Error unmarshaling config into struct: 1 error(s) decoding:

* 'Profiles[SampleMultiNodeEtcdRaft].Orderer' has invalid keys: EtcdRaft
panic: Error unmarshaling config into struct: 1 error(s) decoding:

* 'Profiles[SampleMultiNodeEtcdRaft].Orderer' has invalid keys: EtcdRaft [recovered]
	panic: Error unmarshaling config into struct: 1 error(s) decoding:

* 'Profiles[SampleMultiNodeEtcdRaft].Orderer' has invalid keys: EtcdRaft

goroutine 1 [running]:
github.com/hyperledger/fabric/vendor/github.com/op/go-logging.(*Logger).Panic(0xc420193e00, 0xc420340350, 0x1, 0x1)
	/w/workspace/fabric-nightly-release-job-release-1.2-x86_64/gopath/src/github.com/hyperledger/fabric/vendor/github.com/op/go-logging/logger.go:188 +0xbd
main.main.func1()
	/w/workspace/fabric-nightly-release-job-release-1.2-x86_64/gopath/src/github.com/hyperledger/fabric/common/tools/configtxgen/main.go:254 +0x1ae
panic(0xc6ea00, 0xc420340340)
	/opt/go/go1.10.linux.amd64/src/runtime/panic.go:505 +0x229
github.com/hyperledger/fabric/vendor/github.com/op/go-logging.(*Logger).Panic(0xc420193c50, 0xc4200adb60, 0x2, 0x2)
	/w/workspace/fabric-nightly-release-job-release-1.2-x86_64/gopath/src/github.com/hyperledger/fabric/vendor/github.com/op/go-logging/logger.go:188 +0xbd
github.com/hyperledger/fabric/common/tools/configtxgen/localconfig.Load(0x7ffde7950654, 0x15, 0x0, 0x0, 0x0, 0xc4201b1510)
	/w/workspace/fabric-nightly-release-job-release-1.2-x86_64/gopath/src/github.com/hyperledger/fabric/common/tools/configtxgen/localconfig/config.go:284 +0x607
main.main()
	/w/workspace/fabric-nightly-release-job-release-1.2-x86_64/gopath/src/github.com/hyperledger/fabric/common/tools/configtxgen/main.go:265 +0xce7
+ res=2
+ set +x
Failed to generate orderer genesis block...

4、创建启动Fabric网络

./byfn.sh up
# 此步骤需要pull镜像执行较长时间。可以提前使用/scripts/bootstrap.sh脚本来pull需要的镜像

如果Docker和Docker-Compose的版本差距过大,那么这一步也会报如下错

===================== 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=1
+ set +x
2019-04-23 09:50:33.779 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2019-04-23 09:50:33.783 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2019-04-23 09:50:33.793 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2019-04-23 09:50:33.793 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
Error: could not assemble transaction, err proposal response was not successful, error code 500, msg timeout expired while starting chaincode mycc:1.0 for transaction
!!!!!!!!!!!!!!! Chaincode instantiation on peer0.org2 on channel 'mychannel' failed !!!!!!!!!!!!!!!!
========= ERROR !!! FAILED to execute End-2-End Scenario ===========

输出结果大致如下:

Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] Y
proceeding ...
LOCAL_VERSION=1.2.0
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.org2.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer1.org1.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli                    ... done

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

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

Channel name : mychannel
Creating channel...
===================== Channel 'mychannel' created ===================== 

Having all peers join the channel...
===================== peer1.org2 joined channel 'mychannel' ===================== 

Updating anchor peers for org1...
===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' ===================== 

Updating anchor peers for org2...
===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' ===================== 

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

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

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

Querying chaincode on peer0.org1...
===================== Query successful on peer0.org1 on channel 'mychannel' ===================== 
Sending invoke transaction on peer0.org1 peer0.org2...
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' ===================== 

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

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

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


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

此时,Fabric网络以及一个chaincode已经创建完成

5、停止Fabric网络

./byfn.sh down

你可能感兴趣的:(区块链)