hyperledger fabric 1.4 之自定义多机部署

在经过长期折腾终于摸索出fabric多机部署的方法,故此记录。
本文使用的代码上传于https://github.com/githubofljl/fabric-test

本文假设已在相应服务器上搭建好fabric环境,理论上github上代码修改ip后可以直接使用。
本文网络环境如下:
- orderer.example.com:192.168.1.4 server1
- cli:192.168.1.4
- peer0.org1.example.com:192.168.1.8 server2
- peer1.org1.example.com:192.168.1.8
- ca0:192.168.1.8
- peer0.org2.example.com:192.168.1.12 server3
- peer1.org2.example.com:192.168.1.12
- ca1:192.168.1.12
- peer0.org3.example.com:192.168.1.16 server4
- peer1.org3.example.com:192.168.1.16
- ca2:192.168.1.16
即每台服务器部署有一个Org,两个peer,一个ca。

1. 文件结构

文件目录结构如下所示:

├── bin
├── chaincode
└── first-network

  • bin文件夹

其中bin用于存放fabric二进制文件工具,放在文件夹里的bootstrap.sh脚本可以下载相关文件。

├── bin
│ ├── configtxgen
│ ├── configtxlator
│ ├── cryptogen
│ ├── discover
│ ├── fabric-ca-client
│ ├── idemixgen
│ ├── orderer
│ └── peer

  • chaincode文件夹

顾名思义文件夹chaincode用于存放chaincode,本处chaincode来自于fabirc-samples/chaincode。

  • first-network文件夹
    first-network文件夹中有以下文件。

└── first-network
├── base
├── channel-artifacts
├── configtx.yaml
├── crypto-config
├── crypto-config.yaml
├── docker-compose-cliandorderer.yaml
├── docker-compose-Org1.yaml
├── docker-compose-Org2.yaml
└── docker-compose-Org3.yaml

其具体用途在下一节讲解。

2. 准备步骤

  1. 生成证书

首先根据需要部署的网络结构修改crypto-config.yaml文件。
然后在当前目录运行
../bin/cryptogen generate --config=./crypto-config.yaml
在crypto-config文件夹下会生成网络中需要的证书。(若之前存在crypto-config文件夹需要先删除)

  1. 生成初始块等相关信息
    根据需要部署的网络结构修改configtx.yaml文件。
    在当前目录运行:
    生成初始块
../bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block

生成通道信息(可以根据需要更改channelID,默认为mychannel。

../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

生成各个组织信息:

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID mychannel -asOrg Org3MSP
  1. 修改docker-compose-*.yaml文件

根据生成的证书修改 docker-compose-*.yaml文件(必改)

在每个文件根据crypto-config/peerOrganizations/Org*.example.com/ca/…_sk修改对应docker-compose文件中的…_sk。

在docker-compose-*.yaml文件中加入couchdb等新功能(可选)

3. 多机部署

首先将fabric-test文件夹上传到每台服务器中。

  • 启动docker容器。
    在server1的/fabric-test/first-network目录下运行
docker-compose -f docker-compose-cliandorderer.yaml up -d

在server2的/fabric-test/first-network目录下运行

docker-compose -f docker-compose-Org1.yaml up -d

在server3的/fabric-test/first-network目录下运行

docker-compose -f docker-compose-Org2.yaml up -d

在server4的/fabric-test/first-network目录下运行

docker-compose -f docker-compose-Org3.yaml up -d

若需要重新启动容器运行

docker-compose -f *** down --volumes

4. 运行与测试

进入server1服务器,依次做如下操作:

#进入cli
docker exec -it cli bash  

#设置org1环境变量
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
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

#通道名称
export CHANNEL_NAME=mychannel

#获取初始块信息
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -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

#org1.peer0加入channel
 peer channel join -b mychannel.block

#org2.peer0加入channel
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp CORE_PEER_ADDRESS=peer0.org2.example.com:9051 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 peer channel join -b mychannel.block

#org3.peer0加入channel
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org3.example.com:11051
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

peer channel join -b mychannel.block

#各个Org的pee1 加入组织

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt

 peer channel join -b mychannel.block



CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/ca.crt

 peer channel join -b mychannel.block

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer1.org3.example.com:12051
CORE_PEER_LOCALMSPID="Org3MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer1.org3.example.com/tls/ca.crt


 peer channel join -b mychannel.block


#Update the anchor peers
#org1
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
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
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -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

#org2
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp CORE_PEER_ADDRESS=peer0.org2.example.com:9051 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 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -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

#org3
 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org3.example.com:11051
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


peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org3MSPanchors.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

#install chaincode
#org1
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
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

peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/

#org2
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
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

peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/

#org3 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org3.example.com:11051 
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 

peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/

#实例化chaincode,背书策略为 -P "OR ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')

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 $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"

此时chaincode已经安装成功,可以运行query测试

peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

结果应为‘100’

参考:
byfn.sh
https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html

你可能感兴趣的:(hyperledger,fabric学习)