启动命令与过程与官方2org的相似,但由于组织增至了4个,所以无法用官方提供的scripts/scripts.sh脚本一键启动网络,具体分步操作如下:
首先来启动orderer节点,在orderer服务器上运行:
运行完毕后我们可以使用docker ps看到运行了一个名字为orderer.example.com的节点。
然后我们切换到peer0.org1.example.com服务器,启动本服务器的peer节点和cli,命令为:
运行完毕后我们使用docker ps应该可以看到2个正在运行的容器。
接下来依次在另外3台服务器运行启动peer节点容器的命令:
现在我们整个Fabric4+1服务器网络已经成型。
切换到peer0.org1.example.com服务器上,使用该服务器上的cli来运行创建Channel和运行ChainCode的操作。首先进入cli容器:
docker exec -it cli bash
进入容器后我们可以看到命令提示变为:
root@b41e67d40583:/opt/gopath/src/github.com/hyperledger/fabric/peer#
说明我们已经以root的身份进入到cli容器内部。
创建Channel的命令是peer channel create,我们前面创建Channel的配置区块时,指定了Channel的名字是mychannel,那么这里我们必须创建同样名字的Channel。
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA
执行该命令后,系统会提示:
2017-08-29 20:36:47.486 UTC [channelCmd] readBlock -> DEBU 020 Received block:0
系统会在cli内部的当前目录创建一个mychannel.block文件,这个文件非常重要,接下来其他节点要加入这个Channel就必须使用这个文件。
#peer1加入channel peer channel join -b mychannel.block #修改环境变量,使peer2加入channel CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer channel join -b mychannel.block #修改环境变量,使peer3加入channel CORE_PEER_LOCALMSPID="Org3MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp CORE_PEER_ADDRESS=peer0.org3.example.com:7051 peer channel join -b mychannel.block #修改环境变量,使peer4加入channel CORE_PEER_LOCALMSPID="Org4MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/users/Admin@org4.example.com/msp CORE_PEER_ADDRESS=peer0.org4.example.com:7051 peer channel join -b mychannel.block
对于Org1来说,peer0.org1是锚节点,我们需要连接上它并更新锚节点,其他组织同理,peer0为锚节点:
#更新org1的锚节点 CORE_PEER_LOCALMSPID="Org1MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp CORE_PEER_ADDRESS=peer0.org1.example.com:7051 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile $ORDERER_CA #更新org2的锚节点 CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA #更新org3的锚节点 CORE_PEER_LOCALMSPID="Org3MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp CORE_PEER_ADDRESS=peer0.org3.example.com:7051 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org3MSPanchors.tx --tls true --cafile $ORDERER_CA #更新org4的锚节点 CORE_PEER_LOCALMSPID="Org4MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/users/Admin@org4.example.com/msp CORE_PEER_ADDRESS=peer0.org4.example.com:7051 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org4MSPanchors.tx --tls true --cafile $ORDERER_CA
以上,整个Fabric网络和Channel都准备完毕,接下来我们来安装和运行ChainCode。这里我们不用官方提供的Example02等链码,而是自己实现了一个招标投标的合约链码,放在/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/mychaincode
目录下。该链码可以实现一个公司提出一条链路需求,其他公司针对该链路的每一段进行投标,最后招标公司每一段选出一个投标公司达成合约,或者关闭合约的功能。