田园综合体建设网站/百度精准获客平台
硬件环境:CentOS7虚拟机一台,Ubuntu虚拟机一台,一共两台主机,有条件的小伙伴可以试一下在3台以上的主机部署。
软件环境:
名称 | 版本 |
---|---|
Fabric | 2.2.0 |
go | 1.15 |
docker | 19.03.11 |
docker-compose | 1.12.0 |
部署方案:3个orderer、2个组织、每个组织2个普通节点,通过静态IP
的方式实现Hyperledger Fabric多机/分布式部署
1.安装Go、docker、docker-compose(虚拟机1和虚拟机2都需要)
安装Go流程
wget链接获取来源链接为https://studygolang.com/dl,如果以下wget命令失效请到以上网站获取或下载
打开终端,输入指令
sudo wget -P /usr/local https://studygolang.com/dl/golang/go1.15.linux-amd64.tar.gz
cd /usr/local
sudo tar -zxvf go1.15.linux-amd64.tar.gz
添加环境变量,打开编辑器
vim ~/.bashrc
将以下内容复制到bashrc文件中,按I
插入,插入完成后按ESC
退出插入,输出:wq!
保存退出,如下图所示
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/go
export PATH=$PATH:/home/yujialing/go/src/github.com/hyperledger/fabric-samples/bin
export FABRIC_CFG_PATH=/home/yujialing/go/src/github.com/hyperledger/fabric-samples/multiple-deployment

source ~/.bashrc
验证go是否安装成功
go version

安装docker流程
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
sudo yum -y install docker-ce
启动Docker-ce
sudo systemctl start docker
sudo systemctl enable docker
将当前用户添加到docker组中
sudo usermod -aG docker $USER
newgrp - docker
检查docker是否安装成功
docker -v
没有VPN的同学需要修改docker镜像源,否则后面的步骤有些去外网下载的步骤有可能会报连接失败的错误
vim /etc/docker/daemon.json
{
"registry-mirrors":["https://registry.docker-cn.com"]
}
按I
插入,插入完成后按ESC
退出插入,输出:wq!
保存退出
如果显示无法写入请使用sudo chmod -R 777 docker/
指令给docker最高权限方可写入。

重启docker服务
systemctl restart docker.service
安装docker-compose流程
curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
查看docker是否安装成功
docker-compose -v

2.下载fabric二进制文件、docker镜像和fabric样例(虚拟机1、2都要)
下载fabric二进制文件、docker镜像、fabric样例
cd /home/yujialing
mkdir go
mkdir go/src
mkdir go/src/github.com
mkdir go/src/github.com/hyperledger
cd /home/yujialing/go/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
git checkout 22393b629bcac7f7807cc6998aa44e06ecc77426
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.0 1.4.8 -s
最后一步用时稍微有点长,请耐心等待
3.建立多机部署的文件夹
在fabric-samples
文件夹下
虚拟机1和虚拟机2的操作
mkdir multiple-deployment
cd multiple-deployment
以下是虚拟机1
的操作,复制fabric样例中的名为abstore
的链码(智能合约)到chaincode/go
下
mkdir chaincode
mkdir chaincode/go
cp -r ../chaincode/abstore/go/* chaincode/go
下载链码的依赖包
cd chaincode/go
go env -w GOPROXY=https://goproxy.io,direct
go env -w GO111MODULE=on
go mod vendor
可以看到go文件夹下多出存放链码(智能合约)依赖的vendor
文件夹
4.生成证书及通道配置
虚拟机1的操作
在multiple-deployment
文件夹下新建crypto-config.yaml
文件和configtx.yaml
文件
cd ../..
touch crypto-config.yaml
touch configtx.yaml
将以下内容写进crypto-config.yaml
文件中
OrdererOrgs:- Name: OrdererDomain: example.comSpecs:- Hostname: orderer0- Hostname: orderer1- Hostname: orderer2PeerOrgs:- Name: Org1Domain: org1.example.comEnableNodeOUs: trueTemplate:Count: 2Users:Count: 1- Name: Org2Domain: org2.example.comEnableNodeOUs: trueTemplate:Count: 2Users:Count: 1
将以下内容写进configtx.yaml
文件中
Organizations:- &OrdererOrgName: OrdererOrgID: OrdererMSPMSPDir: crypto-config/ordererOrganizations/example.com/mspPolicies:Readers:Type: SignatureRule: "OR('OrdererMSP.member')"Writers:Type: SignatureRule: "OR('OrdererMSP.member')"Admins:Type: SignatureRule: "OR('OrdererMSP.admin')"- &Org1Name: Org1MSPID: Org1MSPMSPDir: crypto-config/peerOrganizations/org1.example.com/mspPolicies:Readers:Type: SignatureRule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"Writers:Type: SignatureRule: "OR('Org1MSP.admin', 'Org1MSP.client')"Admins:Type: SignatureRule: "OR('Org1MSP.admin')"Endorsement:Type: SignatureRule: "OR('Org1MSP.peer')"AnchorPeers:- Host: peer0.org1.example.comPort: 7051- &Org2Name: Org2MSPID: Org2MSPMSPDir: crypto-config/peerOrganizations/org2.example.com/mspPolicies:Readers:Type: SignatureRule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"Writers:Type: SignatureRule: "OR('Org2MSP.admin', 'Org2MSP.client')"Admins:Type: SignatureRule: "OR('Org2MSP.admin')"Endorsement:Type: SignatureRule: "OR('Org2MSP.peer')"AnchorPeers:- Host: peer0.org2.example.comPort: 7051Capabilities:Channel: &ChannelCapabilitiesV2_0: trueOrderer: &OrdererCapabilitiesV2_0: trueApplication: &ApplicationCapabilitiesV2_0: trueApplication: &ApplicationDefaultsOrganizations:Policies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"LifecycleEndorsement:Type: ImplicitMetaRule: "MAJORITY Endorsement"Endorsement:Type: ImplicitMetaRule: "MAJORITY Endorsement"Capabilities:<<: *ApplicationCapabilitiesOrderer: &OrdererDefaultsOrdererType: etcdraftAddresses: # orderer 集群节点- orderer0.example.com:7050- orderer1.example.com:8050- orderer2.example.com:7050# Batch Timeout: The amount of time to wait before creating a batchBatchTimeout: 2s# Batch Size: Controls the number of messages batched into a blockBatchSize:MaxMessageCount: 10AbsoluteMaxBytes: 99 MBPreferredMaxBytes: 512 KBOrganizations:Policies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"# BlockValidation specifies what signatures must be included in the block# from the orderer for the peer to validate it.BlockValidation:Type: ImplicitMetaRule: "ANY Writers"Channel: &ChannelDefaultsPolicies:# Who may invoke the 'Deliver' APIReaders:Type: ImplicitMetaRule: "ANY Readers"# Who may invoke the 'Broadcast' APIWriters:Type: ImplicitMetaRule: "ANY Writers"# By default, who may modify elements at this config levelAdmins:Type: ImplicitMetaRule: "MAJORITY Admins"Capabilities:<<: *ChannelCapabilitiesProfiles:TwoOrgsChannel:Consortium: SampleConsortium<<: *ChannelDefaultsApplication:<<: *ApplicationDefaultsOrganizations:- *Org1- *Org2Capabilities:<<: *ApplicationCapabilitiesSampleMultiNodeEtcdRaft:<<: *ChannelDefaultsCapabilities:<<: *ChannelCapabilitiesOrderer:<<: *OrdererDefaultsOrdererType: etcdraftEtcdRaft:Consenters:- Host: orderer0.example.comPort: 7050ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crtServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt- Host: orderer1.example.comPort: 8050ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crtServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt- Host: orderer2.example.comPort: 7050ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crtServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crtAddresses:- orderer0.example.com:7050- orderer1.example.com:8050- orderer2.example.com:7050Organizations:- *OrdererOrgCapabilities:<<: *OrdererCapabilitiesApplication:<<: *ApplicationDefaultsOrganizations:- <<: *OrdererOrgConsortiums:SampleConsortium:Organizations:- *Org1- *Org2
虚拟机1的操作
通过cryptogen
命令生成证书配置
cryptogen generate --config=./crypto-config.yaml

通过configtxgen
命令生成创世区块
configtxgen -profile SampleMultiNodeEtcdRaft -channelID multiple-deployment-channel -outputBlock ./channel-artifacts/genesis.block

通过configtxgen
命令生成通道配置
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

定义组织1
的锚节点(也称主节点)
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

定义组织2
的锚节点
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

将channel-artifacts
和crypto-config
这两个文件夹拷贝到虚拟机2
中
6.IP与域名映射配置(虚拟机1和虚拟机2都需要设定)
通过修改/etc/hosts文件 添加IP地址与域名的映射
修改/etc/hosts文件指令
sudo vim /etc/hosts
按I
插入,插入完成后按ESC
退出插入,输入:wq!
保存退出,如下图所示:
虚拟机1的IP orderer0.example.com
虚拟机1的IP orderer1.example.com
虚拟机2的IP orderer2.example.com
虚拟机1的IP peer0.org1.example.com
虚拟机1的IP peer1.org1.example.com
虚拟机2的IP peer0.org2.example.com
虚拟机2的IP peer1.org2.example.com

完成添加后重启一下网络
Ubuntu:
sudo /etc/init.d/networking restart
CentOS:
sudo /etc/init.d/network restart
7.编写网络启动脚本并启动网络
虚拟机1的操作
touch docker-compose-up.yaml
将以下内容写入docker-compose-up.yaml
中,yaml文件中的虚拟机的IP
记得修改成你们的虚拟机IP
version: '2'services:orderer0.example.com:container_name: orderer0.example.comimage: hyperledger/fabric-ordererenvironment:- FABRIC_LOGGING_SPEC=DEBUG- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0- ORDERER_GENERAL_BOOTSTRAPMETHOD=file- ORDERER_GENERAL_BOOTSTRAPFILE=/var/hyperledger/orderer/orderer.genesis.block- ORDERER_GENERAL_LOCALMSPID=OrdererMSP- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp# enabled TLS- ORDERER_GENERAL_TLS_ENABLED=true- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]working_dir: /opt/gopath/src/github.com/hyperledger/fabriccommand: orderervolumes:- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block- ./crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp:/var/hyperledger/orderer/msp- ./crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/:/var/hyperledger/orderer/tlsports:- 7050:7050extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"orderer1.example.com:container_name: orderer1.example.comimage: hyperledger/fabric-ordererenvironment:- FABRIC_LOGGING_SPEC=INFO- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0- ORDERER_GENERAL_BOOTSTRAPMETHOD=file- ORDERER_GENERAL_BOOTSTRAPFILE=/var/hyperledger/orderer/orderer.genesis.block- ORDERER_GENERAL_LOCALMSPID=OrdererMSP- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp# enabled TLS- ORDERER_GENERAL_TLS_ENABLED=true- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]working_dir: /opt/gopath/src/github.com/hyperledger/fabriccommand: orderervolumes:- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block- ./crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp:/var/hyperledger/orderer/msp- ./crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/:/var/hyperledger/orderer/tlsports:- 8050:8050extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"peer0.org1.example.com:container_name: peer0.org1.example.comimage: hyperledger/fabric-peerenvironment:- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_PEER_ID=peer0.org1.example.com- CORE_PEER_ADDRESS=peer0.org1.example.com:7051- CORE_PEER_LISTENADDRESS=0.0.0.0:7051- CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051- CORE_PEER_LOCALMSPID=Org1MSP- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_TLS_ENABLED=true- CORE_PEER_GOSSIP_USELEADERELECTION=true- CORE_PEER_GOSSIP_ORGLEADER=false- CORE_PEER_PROFILE_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt# Allow more time for chaincode container to build on install.- CORE_CHAINCODE_EXECUTETIMEOUT=300sworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startvolumes:- /var/run/:/host/var/run/- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tlsports:- 7051:7051- 7052:7052- 7053:7053extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"peer1.org1.example.com:container_name: peer1.org1.example.comimage: hyperledger/fabric-peerenvironment:- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_PEER_ID=peer1.org1.example.com- CORE_PEER_ADDRESS=peer1.org1.example.com:8051- CORE_PEER_LISTENADDRESS=0.0.0.0:8051- CORE_PEER_CHAINCODEADDRESS=peer1.org1.example.com:8052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:8052- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.example.com:8051- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:8051- CORE_PEER_LOCALMSPID=Org1MSP- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_TLS_ENABLED=true- CORE_PEER_GOSSIP_USELEADERELECTION=true- CORE_PEER_GOSSIP_ORGLEADER=false- CORE_PEER_PROFILE_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt# Allow more time for chaincode container to build on install.- CORE_CHAINCODE_EXECUTETIMEOUT=300sworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startvolumes:- /var/run/:/host/var/run/- ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/fabric/msp- ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls:/etc/hyperledger/fabric/tlsports:- 8051:8051- 8052:8052- 8053:8053extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"cli1:container_name: cli1image: hyperledger/fabric-toolstty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock#- FABRIC_LOGGING_SPEC=DEBUG- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_ID=cli1- 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/mspworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/multiple-deployment/chaincode/go- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifactsextra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"- "peer0.org1.example.com:虚拟机1的IP"- "peer1.org1.example.com:虚拟机1的IP"- "peer0.org2.example.com:虚拟机2的IP"- "peer1.org2.example.com:虚拟机2的IP"cli2:container_name: cli2image: hyperledger/fabric-toolstty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock#- FABRIC_LOGGING_SPEC=DEBUG- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_ID=cli2- CORE_PEER_ADDRESS=peer1.org1.example.com:8051- 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/peer1.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/peer1.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/peer1.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/mspworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/multiple-deployment/chaincode/go- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifactsextra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"- "peer0.org1.example.com:虚拟机1的IP"- "peer1.org1.example.com:虚拟机1的IP"- "peer0.org2.example.com:虚拟机2的IP"- "peer1.org2.example.com:虚拟机2的IP"
虚拟机2的操作
touch docker-compose-up.yaml
将以下内容写入docker-compose-up.yaml
中,yaml文件中的虚拟机的IP
记得修改成你们的虚拟机IP
version: '2'services:orderer2.example.com:container_name: orderer2.example.comimage: hyperledger/fabric-ordererenvironment:- FABRIC_LOGGING_SPEC=INFO- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0- ORDERER_GENERAL_BOOTSTRAPMETHOD=file- ORDERER_GENERAL_BOOTSTRAPFILE=/var/hyperledger/orderer/orderer.genesis.block- ORDERER_GENERAL_LOCALMSPID=OrdererMSP- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp# enabled TLS- ORDERER_GENERAL_TLS_ENABLED=true- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]working_dir: /opt/gopath/src/github.com/hyperledger/fabriccommand: orderervolumes:- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block- ./crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/msp:/var/hyperledger/orderer/msp- ./crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/:/var/hyperledger/orderer/tlsports:- 7050:7050extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"peer0.org2.example.com:container_name: peer0.org2.example.comimage: hyperledger/fabric-peerenvironment:- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_PEER_ID=peer0.org2.example.com- CORE_PEER_ADDRESS=peer0.org2.example.com:7051- CORE_PEER_LISTENADDRESS=0.0.0.0:7051- CORE_PEER_CHAINCODEADDRESS=peer0.org2.example.com:7052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051- CORE_PEER_LOCALMSPID=Org2MSP- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_TLS_ENABLED=true- CORE_PEER_GOSSIP_USELEADERELECTION=true- CORE_PEER_GOSSIP_ORGLEADER=false- CORE_PEER_PROFILE_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt# Allow more time for chaincode container to build on install.- CORE_CHAINCODE_EXECUTETIMEOUT=300sworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startvolumes:- /var/run/:/host/var/run/- ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp- ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tlsports:- 7051:7051- 7052:7052- 7053:7053extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"peer1.org2.example.com:container_name: peer1.org2.example.comimage: hyperledger/fabric-peerenvironment:- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_PEER_ID=peer1.org2.example.com- CORE_PEER_ADDRESS=peer1.org2.example.com:8051- CORE_PEER_LISTENADDRESS=0.0.0.0:8051- CORE_PEER_CHAINCODEADDRESS=peer1.org2.example.com:8052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:8052- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:8051- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org2.example.com:8051- CORE_PEER_LOCALMSPID=Org2MSP- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_TLS_ENABLED=true- CORE_PEER_GOSSIP_USELEADERELECTION=true- CORE_PEER_GOSSIP_ORGLEADER=false- CORE_PEER_PROFILE_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt# Allow more time for chaincode container to build on install.- CORE_CHAINCODE_EXECUTETIMEOUT=300sworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node startvolumes:- /var/run/:/host/var/run/- ./crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/msp:/etc/hyperledger/fabric/msp- ./crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls:/etc/hyperledger/fabric/tlsports:- 8051:8051- 8052:8052- 8053:8053extra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"cli1:container_name: cli1image: hyperledger/fabric-toolstty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock#- FABRIC_LOGGING_SPEC=DEBUG- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_ID=cli1- 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/mspworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/multiple-deployment/chaincode/go- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifactsextra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"- "peer0.org1.example.com:虚拟机1的IP"- "peer1.org1.example.com:虚拟机1的IP"- "peer0.org2.example.com:虚拟机2的IP"- "peer1.org2.example.com:虚拟机2的IP"cli2:container_name: cli2image: hyperledger/fabric-toolstty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock#- FABRIC_LOGGING_SPEC=DEBUG- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_ID=cli2- CORE_PEER_ADDRESS=peer1.org2.example.com:8051- 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/peer1.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/peer1.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/peer1.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/mspworking_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/multiple-deployment/chaincode/go- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifactsextra_hosts:- "orderer0.example.com:虚拟机1的IP"- "orderer1.example.com:虚拟机1的IP"- "orderer2.example.com:虚拟机2的IP"- "peer0.org1.example.com:虚拟机1的IP"- "peer1.org1.example.com:虚拟机1的IP"- "peer0.org2.example.com:虚拟机2的IP"- "peer1.org2.example.com:虚拟机2的IP"
接下来在两台虚拟机上启动网络
虚拟机1和虚拟机2的操作
docker-compose -f docker-compose-up.yaml up -d


虚拟机1的操作
进入cli1
容器,也就是以peer0.org1
的角色与网络交互
docker exec -it cli1 bash
创建通道,这一步最好等网络跑起来15秒之后再操作,因为orderer之间要握手和选举raft领导节点,需要点时间。操作太快会报no Raft leader
的错误
peer channel create -o orderer0.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/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

组织1
的peer0
节点加入通道mychannel
peer channel join -b mychannel.block

更新组织1
的锚节点
peer channel update -o orderer0.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/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

退出cli1
容器,从cli1
容器中拷贝出mychannel.block
文件到multiple-deployment
文件夹下,并复制到虚拟机2
中
exit
docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./

接下来到虚拟机2的操作
将mychannel.block
拷贝到虚拟机2
的cli1
(组织2
的peer0
角色)容器中
docker cp mychannel.block cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/

进入cli1
容器
docker exec -it cli1 bash
加入通道
peer channel join -b mychannel.block

更新组织2的锚节点(主节点)
peer channel update -o orderer0.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/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

8.安装与调用链码(智能合约)
接下来到虚拟机1的操作
进入cli1
容器中
docker exec -it cli1 bash
peer lifecycle chaincode package mycc.tar.gz --path github.com/hyperledger/multiple-deployment/chaincode/go --lang golang --label mycc_1

安装链码
peer lifecycle chaincode install mycc.tar.gz

退出cli1
容器,将打包的链码mycc.tar.gz
从cli1容器中提取出来,并拷贝到虚拟机2
的multiple-deployment
目录下
exit
docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mycc.tar.gz ./

接下来到虚拟机2的操作
退出cli1
容器,将multiple-deployment
目录下的链码压缩包mycc.tar.gz
复制到cli1
(组织2
的peer0
)容器中
exit
docker cp mycc.tar.gz cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/

再次进入cli1
容器,安装链码
docker exec -it cli1 bash
peer lifecycle chaincode install mycc.tar.gz

组织2
同意提交链码,下面指令中的链码id记得替换成上一条指令生成的链码id
peer lifecycle chaincode approveformyorg --channelID mychannel --name mycc --version 1.0 --init-required --package-id mycc_1:242b275209a0cacb00772667b69b4ed1d6efe91dab266042b0b7047ded06adb3 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

接下来到虚拟机1的操作
再次进入cli1
容器,同意提交链码
docker exec -it cli1 bash
peer lifecycle chaincode approveformyorg --channelID mychannel --name mycc --version 1.0 --init-required --package-id mycc_1:242b275209a0cacb00772667b69b4ed1d6efe91dab266042b0b7047ded06adb3 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

查看链码的状态是否就绪
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc --version 1.0 --init-required --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json

提交链码
peer lifecycle chaincode commit -o orderer0.example.com:7050 --channelID mychannel --name mycc --version 1.0 --sequence 1 --init-required --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

初始化链码
peer chaincode invoke -o orderer0.example.com:7050 --isInit --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["Init","a","100","b","100"]}' --waitForEvent

虚拟机1
查询a的余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

虚拟机2
查询a的余额
双方均可查询到a的余额
在虚拟机2
上操作a
转账10给b
peer chaincode invoke -o orderer0.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' --waitForEvent
转账完成后查询a
的余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

查询结果为90,转账成功
在虚拟机1
查询a的余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

在虚拟机2
的操作已经fabric
网络的各节点中同步
Hyperledger Fabric 2.x 多机部署/分布式集群部署成功
如果要结束网络,可以在退出cli1容器
后使用以下指令退出
docker-compose -f docker-compose-up.yaml down
docker-compose-up.yaml
文件中还有各组织1、2的peer1
节点,容器名为cli2
,可以进入容器中使他们加入网络,同步消息,操作与peer0
一样
欢迎小伙伴讨论,如有错误请在评论区评论或发私聊消息,谢谢你。