多主机部署Fabric1.0
- 本文以fabric下examples/e2e_cli为例,并使用docker-compose-e2e.yaml来启动Fabric网络。
- 在三台主机上进行部署,分别orderer service、org1(包括2个peer,1个ca)、org2(包括2个peer,1个ca)。
准备工作
运行Fabric节点需要依赖以下工具:
- Docker: 用于管理Fabric镜像以及运行peer和orderer等组件。
- Docker-compose:用于配置Fabric容器
- Fabric源码:提供了用于生成证书(cryptogen)和配置channel(configtxgen)的工具和e2e_cli示例。
- Go语言开发环境:源码的工具编译依赖于Go语言。
在主机1利用cryptogen和configtxgen工具生成证书和配置channel等相关文件
命令分别如下:
#生成证书 cryptogen generate --config=./crypto-config.yaml #生成配置channel所需文件 configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
将步骤2生成的文件拷贝到主机2和主机3保持所有主机的这些文件相同。
主机1(orderer service)
IP:10.168.195.108
编辑docker-compose-e2e.yaml文件,只保留services下的orderer.example.com ,其它services注释掉。内容如下所示:
services:
orderer.example.com:
extends:
file: base/docker-compose-base-peer.yaml
service: orderer.example.com
container_name: orderer.example.com
注意: 在部署org1和org2的主机上编辑/etc/hosts,并在文件末尾添加如下内容
10.168.195.98 orderer.example.com (按照自己实际情况)
主机2(org1:包括2个peer、1个ca)
编辑docker-compose-e2e.yaml文件,只保留services下的ca0、peer0.org1.example.com、peer1.org1.example.com,其它services注释掉。
给peer0.org1和peer1.org1添加如下内容:
extra_hosts: - "orderer.example.com:10.168.195.108"
编辑cli,修改如下内容:
#将command注释掉 command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT' #在volumes添加 - ./peer:/opt/gopath/src/github.com/hyperledger/fabric/peer/ - /etc/hosts:/etc/hosts
编辑base/peer-base.yaml文件,添加如下内容:
扫描二维码关注公众号,回复: 1430048 查看本文章volumes: - /etc/hosts:/etc/hosts
主机3(org2:包括2个peer、1个ca)
编辑docker-compose-e2e.yaml文件,只保留services下的ca1、peer0.org2.example.com、peer1.org2.example.com,其它services注释掉。
给peer0.org1和peer1.org1添加如下内容:
extra_hosts: - "orderer.example.com:10.168.195.108"
编辑cli,修改如下内容:
#将command注释掉 command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT' #在volumes添加 - ./peer:/opt/gopath/src/github.com/hyperledger/fabric/peer/ - /etc/hosts:/etc/hosts #修改environment内容 environment: - GOPATH=/opt/gopath - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_ID=cli - CORE_PEER_ADDRESS=peer0.org2.example.com:7051 - CORE_PEER_LOCALMSPID=Org2MSP - CORE_PEER_TLS_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key - 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
编辑base/peer-base.yaml文件,添加如下内容:
volumes: - /etc/hosts:/etc/hosts
测试
启动Fabric网络
先启动主机1上上的orderer service
sudo docker-compose -f docker-compose-e2e.yaml up
分别在主机2、主机3上启动org1、org2的peer
sudo docker-compose -f docker-compose-e2e.yaml up
Channel
在主机2上进入cli容器
sudo docker exec -it cli bash
创建channel
peer channel create -o orderer.example.com:7050 -c mychannel -f channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
在e2e_cli/peer目录下生成一个名为mychannel.block文件,将它拷贝到主机3的e2e_cli/peer目录下。
加入channel
#在主机2、3上的cli容器内分别执行如下命令 peer channel join -b mychannel.block
更新背书节点信息
#在主机2上执行 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem #在主机3上执行 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Chaincode
Install
#主机2、3上都需要执行该命令 peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
Instantiate
#该命令只需要在一个节点上实例化即可。该示例我们在主机2上进行。 peer chaincode instantiate -o orderer.example.com:7050 --tls --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 -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
Query
#此时在主机2、3上分别查询a的值都为100. peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Invoke
#在主机2上进行转账操作。a转给b10. peer chaincode invoke -o orderer.example.com:7050 --tls --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 -c '{"Args":["invoke","a","b","10"]}'
Query
再次执行步骤3的Query操作,此时a为90,b为210.