Hyperledger Fabric v1.0跨两台机器部署balance transfer示例步骤咨询
Deploying Hyperledger Fabric v1.0 Balance Transfer Across Two Machines
Got it, let's walk through how to get your balance transfer sample up and running on two separate machines. I’ve worked through this exact setup before, so here’s a detailed, step-by-step breakdown tailored to your scenario:
1. Prepare Both Machines
- First, ensure both machines meet Hyperledger Fabric v1.0’s prerequisites: Docker (1.12+), Docker Compose (1.8+), Go (1.7+), and have the
fabric-samplesrepo cloned with therelease/balance-transferbranch checked out. - Confirm the two machines are on the same network (e.g., local LAN) and can ping each other. Note their IPs—let’s call Machine A (hosts orderer + Org1 peers)
192.168.1.100and Machine B (hosts Org2 peers)192.168.1.101. - Sync the base directory structure (
crypto-config,configtx,channel-artifacts) between both machines to keep configurations consistent.
2. Update Crypto & Channel Configs
- Modify
crypto-config.yaml: Split node ownership between machines. For example:- Assign OrdererOrg’s nodes to Machine A
- Assign PeerOrg1’s
peer0to Machine A, PeerOrg2’speer0to Machine B - Update the
SANSfield for each node to include the target machine’s IP (e.g.,SANS: ["peer0.org2.example.com", "192.168.1.101"]for Org2’s peer)
- Regenerate crypto materials: Run
cryptogen generate --config=./crypto-config.yamlon Machine A, then copy the entirecrypto-configdirectory to Machine B (usescpor a shared drive). - Update
configtx.yaml: Set the Orderer address to Machine A’s IP (OrdererAddresses: - orderer.example.com:7050). Then add host mappings to both machines’/etc/hostsfiles:192.168.1.100 orderer.example.com peer0.org1.example.com 192.168.1.101 peer0.org2.example.com - Generate channel artifacts: On Machine A, run:
Copy theconfigtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannelchannel-artifactsdirectory to Machine B.
3. Start Orderer & Org1 Peer on Machine A
- Trim
docker-compose-cli.yaml: Remove services for Org2 peers, keeping onlyorderer.example.com,peer0.org1.example.com, andcli. Addextra_hoststo each service to map Machine B’s nodes:extra_hosts: - "peer0.org2.example.com:192.168.1.101" - Start services: Run:
docker-compose -f docker-compose-cli.yaml up -d orderer.example.com peer0.org1.example.com cli - Verify with
docker ps—all three services should be running without errors.
4. Start Org2 Peer on Machine B
- Create a simplified
docker-compose-peer2.yaml: Define only thepeer0.org2.example.comservice. Set environment variables to point to Machine A’s orderer:
Addenvironment: - ORDERER_ADDRESS=orderer.example.com:7050extra_hoststo map Machine A’s nodes:extra_hosts: - "orderer.example.com:192.168.1.100" - "peer0.org1.example.com:192.168.1.100" - Start the peer: Run:
docker-compose -f docker-compose-peer2.yaml up -d peer0.org2.example.com - Check logs with
docker logs peer0.org2.example.comto confirm it connects to the orderer successfully.
5. Create Channel & Join Peers
- Create the channel on Machine A: Enter the cli container and run:
docker exec -it cli bash 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 - Sync channel block to Machine B: Copy the generated
mychannel.blockfrom the cli container to your local Machine A, then send it to Machine B:docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block . scp mychannel.block user@192.168.1.101:/path/to/fabric-samples/balance-transfer/ - Join peers to the channel:
- On Machine A (cli container):
peer channel join -b mychannel.block - On Machine B (enter peer0.org2 container):
peer channel join -b /path/to/mychannel.block
- On Machine A (cli container):
6. Install & Instantiate Chaincode
- Install chaincode on both machines:
- Machine A (cli container):
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 - Machine B (peer0.org2 container):
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
- Machine A (cli container):
- Instantiate chaincode on Machine A: From the cli container, run:
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 -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
7. Test Cross-Machine Balance Transfer
- Query balance on Machine A: In the cli container:
You should getpeer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'100as the result. - Initiate transfer: Still on Machine A:
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 -c '{"Args":["invoke","a","b","10"]}' - Verify sync on Machine B: In the peer0.org2 container:
You should now seepeer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'90, confirming the transaction synced across machines.
内容的提问来源于stack exchange,提问作者user12345
相关产品推荐
相关产品推荐

