本文演示如何在raft共識(shí)啟動(dòng)的fabric網(wǎng)絡(luò)中動(dòng)態(tài)添加orderer節(jié)點(diǎn),fabric版本v1.4.2锡移。
為方便實(shí)驗(yàn)呕童,使用fabric-samples/first-network/
腳本演示,且默認(rèn)您有一定的fabric基礎(chǔ)了解罩抗。
下載源碼
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples/first-network/
git checkout v1.4.2
修改配置
- 修改
crypto-config.yaml
文件拉庵,增加orderer6配置- Hostname: orderer6
,用于生成orderer6節(jié)點(diǎn)的msp套蒂、tls等(生產(chǎn)環(huán)境用fabric-ca生成)
OrdererOrgs:
# ---------------------------------------------------------------------------
# Orderer
# ---------------------------------------------------------------------------
- Name: Orderer
Domain: example.com
# ---------------------------------------------------------------------------
# "Specs" - See PeerOrgs below for complete description
# ---------------------------------------------------------------------------
Specs:
- Hostname: orderer
- Hostname: orderer2
- Hostname: orderer3
- Hostname: orderer4
- Hostname: orderer5
- Hostname: orderer6
- 修改
docker-compose-cli.yaml
文件钞支,增加config文件映射- ./config:/opt/gopath/src/github.com/hyperledger/fabric/peer/config/
,方便后面文件修改
cli:
container_name: cli
image: hyperledger/fabric-tools:$IMAGE_TAG
tty: true
stdin_open: true
environment:
- SYS_CHANNEL=$SYS_CHANNEL
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
#- FABRIC_LOGGING_SPEC=DEBUG
- FABRIC_LOGGING_SPEC=INFO
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=true
- 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/Admin@org1.example.com/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./../chaincode/:/opt/gopath/src/github.com/chaincode
- ./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
- ./config:/opt/gopath/src/github.com/hyperledger/fabric/peer/config/
depends_on:
- orderer.example.com
- peer0.org1.example.com
- peer1.org1.example.com
- peer0.org2.example.com
- peer1.org2.example.com
networks:
- byfn
啟動(dòng)fabric網(wǎng)絡(luò)
./byfn.sh up -o etcdraft -c mychannel -s couchdb -i 1.4.2
更新配置
- 進(jìn)入
cli
容器操刀,設(shè)置全局環(huán)境變量
docker exec -it cli bash
### Global env
CHANNEL_NAME=mychannel
CORE_PEER_TLS_ENABLED=true
FABRIC_LOGGING_SPEC=INFO
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
PEER0_ORG1_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
PEER0_ORG2_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
- 更新系統(tǒng)channel配置
1.設(shè)置OrdererMSP
2.獲取byfn-sys-channel最新配置塊文件config_block.pbexport CORE_PEER_LOCALMSPID=OrdererMSP export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin@example.com/msp/
3.從config_block.pb中提取有效的數(shù)據(jù)烁挟,并轉(zhuǎn)換成可編輯json格式mkdir -p config/system/ peer channel fetch config config/system/config_block.pb -o orderer.example.com:7050 -c byfn-sys-channel --tls --cafile $ORDERER_CA
4.修改config.json里面的orderer配置configtxlator proto_decode --input config/system/config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config/system/config.json
建議使用IDE,比如VsCode或EditPlus等來編輯骨坑,復(fù)制config/system/config.json
為config/system/config_updated.json
撼嗓,搜索.example.com
增加orderer6相關(guān)配置{ "client_tls_cert": "YOUR ORDERER6 TLS SERVER CERT", "host": "orderer6.example.com", "port": 7050, "server_tls_cert": "YOUR ORDERER6 TLS SERVER CERT" }
其中"OrdererAddresses": { "mod_policy": "/Channel/Orderer/Admins", "value": { "addresses": [ "orderer.example.com:7050", "orderer2.example.com:7050", "orderer3.example.com:7050", "orderer4.example.com:7050", "orderer5.example.com:7050", "orderer6.example.com:7050" ] }, "version": "0" }
YOUR ORDERER6 TLS SERVER CERT
可通過腳本或者其他base64工具轉(zhuǎn)換得到柬采,然后copy到上述位置
5.把前后的兩個(gè)json文件重新轉(zhuǎn)換回pb類型文件cat crypto-config/ordererOrganizations/example.com/orderers/orderer6.example.com/tls/server.crt|base64
6.比較前后兩個(gè)pb文件的差異,得到改動(dòng)部分configtxlator proto_encode --input config/system/config.json --type common.Config > config/system/config_block_origin.pb configtxlator proto_encode --input config/system/config_updated.json --type common.Config > config/system/config_block_updated.pb
7.把配置變化部分轉(zhuǎn)化為json文件configtxlator compute_update --channel_id byfn-sys-channel --original config/system/config_block_origin.pb --updated config/system/config_block_updated.pb > config/system/config_diff.pb
8.為上述json文件添加頭部信息(Header)且警,封裝成一個(gè)完整的config update請(qǐng)求粉捻,注意channel_id設(shè)置為configtxlator proto_decode --input config/system/config_diff.pb --type common.ConfigUpdate > config/system/config_diff.json
byfn-sys-channel
9.把封裝好的json文件轉(zhuǎn)換回pb格式文件echo '{"payload":{"header":{"channel_header":{"channel_id":"byfn-sys-channel", "type":2}},"data":{"config_update":'$(cat config/system/config_diff.json)'}}}' | jq . > config/system/config_diff_envelope.json
10.簽名configtxlator proto_encode --input config/system/config_diff_envelope.json --type common.Envelope > config/system/config_diff_envelope.pb
11.提交修改請(qǐng)求到ordererpeer channel signconfigtx -f config/system/config_diff_envelope.pb
peer channel update -f config/system/config_diff_envelope.pb -c byfn-sys-channel -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA
- 更新自定義mychannel配置
1.設(shè)置OrdererMSP
2.獲取mychannel最新配置塊文件config_block.pbexport CORE_PEER_LOCALMSPID=OrdererMSP export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin@example.com/msp/
3.從config_block.pb中提取有效的數(shù)據(jù),并轉(zhuǎn)換成可編輯json格式mkdir -p config/mychannel/ peer channel fetch config config/mychannel/config_block.pb -o orderer.example.com:7050 -c mychannel --tls --cafile $ORDERER_CA
4.修改config.json里面的orderer配置configtxlator proto_decode --input config/mychannel/config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config/mychannel/config.json
與系統(tǒng)channel配置文件修改類似斑芜,復(fù)制config/mychannel/config.json
為config/mychannel/config_updated.json
,并修改肩刃。tls_cert、host與上述一致杏头。{ "client_tls_cert": "YOUR ORDERER6 TLS SERVER CERT", "host": "orderer6.example.com", "port": 7050, "server_tls_cert": "YOUR ORDERER6 TLS SERVER CERT" }
5.把前后的兩個(gè)json文件重新轉(zhuǎn)換回pb類型文件"OrdererAddresses": { "mod_policy": "/Channel/Orderer/Admins", "value": { "addresses": [ "orderer.example.com:7050", "orderer2.example.com:7050", "orderer3.example.com:7050", "orderer4.example.com:7050", "orderer5.example.com:7050", "orderer6.example.com:7050" ] }, "version": "0" }
6.比較前后兩個(gè)pb文件的差異盈包,得到改動(dòng)部分configtxlator proto_encode --input config/mychannel/config.json --type common.Config > config/mychannel/config_block_origin.pb configtxlator proto_encode --input config/mychannel/config_updated.json --type common.Config > config/mychannel/config_block_updated.pb
7.把配置變化部分轉(zhuǎn)化為json文件configtxlator compute_update --channel_id mychannel --original config/mychannel/config_block_origin.pb --updated config/mychannel/config_block_updated.pb > config/mychannel/config_diff.pb
8.為上述json文件添加頭部信息(Header),封裝成一個(gè)完整的config update請(qǐng)求醇王,注意channel_id設(shè)置為mychannelconfigtxlator proto_decode --input config/mychannel/config_diff.pb --type common.ConfigUpdate > config/mychannel/config_diff.json
9.把封裝好的json文件轉(zhuǎn)換回pb格式文件echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat config/mychannel/config_diff.json)'}}}' | jq . > config/mychannel/config_diff_envelope.json
10.簽名configtxlator proto_encode --input config/mychannel/config_diff_envelope.json --type common.Envelope > config/mychannel/config_diff_envelope.pb
11.提交修改請(qǐng)求到ordererpeer channel signconfigtx -f config/mychannel/config_diff_envelope.pb
peer channel update -f config/mychannel/config_diff_envelope.pb -c mychannel -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA
啟動(dòng)orderer6節(jié)點(diǎn)
-
cli
容器內(nèi)獲取系統(tǒng)channel最新配置塊文件(channel-artifacts/genesis_updated.block
)
export CORE_PEER_LOCALMSPID=OrdererMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin@example.com/msp/
peer channel fetch config channel-artifacts/genesis_updated.block -o orderer.example.com:7050 -c byfn-sys-channel --tls --cafile $ORDERER_CA
- 在
fabric-samples/first-network
目錄添加docker-compose-orderer6.yaml
文件呢燥,內(nèi)容如下
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
volumes:
orderer6.example.com:
networks:
byfn:
services:
orderer6.example.com:
extends:
file: base/peer-base.yaml
service: orderer-base
container_name: orderer6.example.com
networks:
- byfn
volumes:
- ./channel-artifacts/genesis_updated.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer6.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer6.example.com/tls/:/var/hyperledger/orderer/tls
- orderer6.example.com:/var/hyperledger/production/orderer
ports:
- 12050:7050
- 啟動(dòng)orderer6
IMAGE_TAG=1.4.2 docker-compose -f docker-compose-orderer6.yaml up -d
docker ps -a|grep orderer6
#查看orderer6 log
docker logs -f orderer6.example.com
驗(yàn)證orderer6節(jié)點(diǎn)
- 在cli容器里設(shè)置peer0環(huán)境變量
#peer0 env
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
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_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_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
- peer invoke 提交到
orderer.example.com:7050
節(jié)點(diǎn)
#查詢a
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
90
#執(zhí)行 a b 10
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles $PEER0_ORG1_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles $PEER0_ORG2_CA -c '{"Args":["invoke","a","b","10"]}'
#查詢
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
80
- peer invoke 提交到
orderer6.example.com:7050
節(jié)點(diǎn)
#查詢
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
80
#執(zhí)行 a b 5
peer chaincode invoke -o orderer6.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles $PEER0_ORG1_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles $PEER0_ORG2_CA -c '{"Args":["invoke","a","b","5"]}'
#查詢
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
75
PS.官方文檔還有最后一步我沒有做,更新endpoint配置操作
Adding a new node to a Raft cluster
參考文檔:
- https://hyperledger-fabric.readthedocs.io/en/master/build_network.html
- https://hyperledger-fabric.readthedocs.io/en/master/channel_update_tutorial.html
- https://hyperledger-fabric.readthedocs.io/en/master/raft_configuration.html
- http://www.reibang.com/p/30b0ff60918d
如遇問題或有疑義隨時(shí)聯(lián)系寓娩,請(qǐng)多多關(guān)照