前言:
在前面已经介绍了环境和证书(fabric-ca),不明白的地方可先查阅前两篇文章。
hyperledger fabric1.0动态添加peer节点之fabric-ca环境配置
hyperledger fabric1.0动态添加peer节点之fabric-ca应用
一、新增节点证书文件
从上篇文章中已获取到了peer2目录,这里参照peer0.org1.example.com或peer1.org1.example.com新增peer2.org1.example.com,并将msp目录替换,tls目录可以为空,上篇中已经关闭了tls功能。
二、修改network_setup.sh
#文件最后进行修改
#新增dynamic判断
if [ "${UP_DOWN}" == "up" ]; then
networkUp
elif [ "${UP_DOWN}" == "dynamic" ]; then ## Clear the network
networkDynamic
elif [ "${UP_DOWN}" == "down" ]; then ## Clear the network
networkDown
elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the network
networkDown
networkUp
else
printHelp
exit 1
fi
#同时在文件上方新增networkDynamic方法
function networkDynamic () {
if [ -f "./crypto-config" ]; then
echo "crypto-config directory already exists."
exit 1
fi
CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE_dynamic up -d 2>&1
docker logs -f cli-dynamic
}
#networkDynamic方法新增了变量$COMPOSE_FILE_dynamic
#文件大概17行新增变量
COMPOSE_FILE_dynamic=docker-compose-dynamic.yaml
三、新增docker-compose-dynamic.yaml文件
我们为新增的节点重新创建文件,不去影响之前的启动文件。具体如下
version: '2'
services:
peer2.org1.example.com:
container_name: peer2.org1.example.com
extends:
file: base/docker-compose-dynamic.yaml
service: peer2.org1.example.com
cli-dynamic:
container_name: cli-dynamic
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=cli-dynamic
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
- 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/[email protected]/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash -c './scripts/script_dy.sh ${CHANNEL_NAME}; sleep $TIMEOUT'
volumes:
- /var/run/:/host/var/run/
- ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
- ./channel/mychannel.block:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block
depends_on:
- peer2.org1.example.com
新增添加的启动节点和cli-dynamic,新增了文件base/docker-compose-dynamic.yaml和scripts/script_dy.sh,并在之前的cli容器的基础上多映射了一个目录文件channel/mychannel.block。
四、新增base/docker-compose-dynamic.yaml
version: '2'
services:
peer2.org1.example.com:
container_name: peer0.org1.example.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer2.org1.example.com
- CORE_PEER_ADDRESS=peer2.org1.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer2.org1.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer2.org1.example.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/tls:/etc/hyperledger/fabric/tls
ports:
- 6051:7051
- 6052:7052
- 6053:7053
注意:端口号可以自行配置,映射端口不可修改。组织锚节点不可随意修改。
五、通道block文件
通道文件需要使用网络启动时创建通道生成的文件,此文件在cli容器中,需要先从容器中拷贝一份。
#拷贝容器文件到主机目录
docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block /Users/cuishuai/Documents/
#在e2e_cli中新增channel目录
#将mychannel.block文件拷贝至channel目录
六、新增scripts/script_dy.sh
#复制scripts/script.sh文件
#大概190行,删除createChannel,updateAnchorPeers
#修改joinChannel方法
for ch in 0 1 2 3; do
为
for ch in 0; do
#修改setGlobals方法
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
为
CORE_PEER_ADDRESS=peer2.org1.example.com:7051
#大概190行之后的执行内容如下
## Join all the peers to the channel
echo "Having all peers join the channel..."
joinChannel
## Install chaincode on Peer0/Org1 and Peer2/Org2
echo "Installing chaincode on org1/peer0..."
installChaincode 0
echo "Instantiating chaincode on org1/peer0..."
instantiateChaincode 0
#Query on chaincode on Peer0/Org1
echo "Querying chaincode on org1/peer0..."
chaincodeQuery 0 90
#Invoke on chaincode on Peer0/Org1
echo "Sending invoke transaction on org1/peer0..."
chaincodeInvoke 0
注意:chaincodeQuery 0 90 此处的90为账本中的数,启动网络时由于进行了一个invoke操作,值由100变为了90,这里90做为验证,如果这里的值与账本中的值不一致,程序执行到这里时超时。如果不确定账本中的值为多少时,可查看链码容器日志
docker logs dev-peer2.org1.example.com-mycc-1.0
七、执行命令
#执行新增命令
./network_setup.sh dynamic
#如果新增过程中出现问题,可单独结束已启的容器
#查看所有容器
docker ps -a
docker rm -f 容器ID
#如果使用./network_setup.sh down会停止整个网络。
以上就是动态添加peer节点的所有内容,可能有所遗漏,有问题欢迎大家下方留言。