多机多节点指在多台电脑上部署多个组织和节点,本案例部署一个排序(orderer)服务,两个组织(org1,org2)和四个节点(peer),每个组织包括两个节点,需要五台服务器,规划如下:
orderer.example.com | 192.168.2.227 |
peer0.org1.example.com | 192.168.2.223 |
peer1.org1.example.com | 192.168.2.224 |
peer0.org2.example.com | 192.168.2.225 |
peer1.org2.example.com | 192.168.2.228 |
1G,2CPU,10G.
多节点部署结构图:
五台机器统一安装基本环境(注意开头带#为不执行内容)
ansible批量部署:
yum -y install epel-release ansible
sed -i 's/#host_key_checking = False/host_key_checking = False/' /etc/ansible/ansible.cfg
#sed -ri ' /#host_key_checking = False/ s/(#)(.*)/\2/' /etc/ansible/ansible.cfg
#或者直接如下多插入一条
#echo "host_key_checking = False" >>/etc/ansible/ansible.cfg
echo "192.168.2.227 ansible_ssh_user=root ansible_ssh_pass=123456" >>/etc/ansible/hosts
echo "192.168.2.223 ansible_ssh_user=root ansible_ssh_pass=123456" >>/etc/ansible/hosts
echo "192.168.2.224 ansible_ssh_user=root ansible_ssh_pass=123456" >>/etc/ansible/hosts
echo "192.168.2.225 ansible_ssh_user=root ansible_ssh_pass=123456" >>/etc/ansible/hosts
echo "192.168.2.228 ansible_ssh_user=root ansible_ssh_pass=123456" >>/etc/ansible/hosts
编写安装脚本:
vim fabric.sh
#!/bin/bash
#fabric集群基本环境自动化部署
yum -y install git gcc-c++ wget lrzsz telnet
#docker17.06.2-ce或更高版本
yum install -y yum-utils && yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && yum makecache fast && yum install docker-ce -y && systemctl start docker && systemctl enable docker && docker --version && echo "docker 部署OK"
#安装docker-compose,负责实现对Docker容器集群的快速编排
yum -y install epel-release && yum -y install python-pip && pip --version && pip install --upgrade pip && pip install docker-compose && yum install bash-completion -y && docker-compose version && echo "docker-compose 部署OK"
#安装go语言版本1.12.x或更高
wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz && tar -xf go1.12.9.linux-amd64.tar.gz && mv go /usr/local/ && echo "export PATH=$PATH:/usr/local/go/bin" >>/etc/profile && source /etc/profile && echo "go 部署OK"
echo "请继续安装nodejs"
安装Node.js and NPM(注意node要单独安装)
fabric目前不支持低于8.x系列的其他版本
wget https://npm.taobao.org/mirrors/node/v8.9.4/node-v8.9.4-linux-x64.tar.xz
tar xf node-v8.9.4-linux-x64.tar.xz
mv node-v8.9.4-linux-x64/ /usr/local/nodejs
chown -R root.root /usr/local/nodejs/
ln -s /usr/local/nodejs/bin/node /usr/bin/node
echo "export PATH=$PATH:/usr/local/nodejs/bin" >>/etc/profile
source /etc/profile
node -v
npm -v
npm太慢,用淘宝镜像代替npm官方镜像
npm config set registry https://registry.npm.taobao.org
配置后可通过下面方式来验证是否成功
npm config get registry
安装express module测试
npm install express -g # -g是全局安装的意思
批量部署fabric(这步主要是后续会用到里面的bin文件和安装智能合约需要的链码go文件):
编写安装fabric脚本:
vim fabric.sh
#!/bin/bash
cd /usr/local/go/src/ && mkdir -p github.com/hyperledger && cd github.com/hyperledger && yum -y install libtool libltdl-dev && wget https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh && chmod +x ./bootstrap.sh && ./bootstrap.sh && echo "export PATH=$PATH:/usr/local/go/src/github.com/hyperledger/fabric-samples/bin" >>/etc/profile
echo "Fabric 安装完毕"
执行脚本:
ansible fabric -m script -a "/root/fabric.sh"
#注意所有服务器执行完脚本要手动重新加载环境变量
source /etc/profile
#如果是window上编辑的脚本需要执行下面命令,否则会报错-bash: ./fabric.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录
解决方法:
sed -i 's/\r$//' fabric.sh
#windows下,每一行的结尾是\n\r,而在linux下文件的结尾是\n
批量安装其它软件:
ansible all -m yum “name=lrzsz,wget,vim”
本文以orderer节点为例,先在192.168.2.227服务器上进行操作。
1、创建工作目录
midir -p /usr/local/go/src/github.com/hyperledger/fabric/first
cd /usr/local/go/src/github.com/hyperledger/fabric/first
2、生成证书和区块配置文件
配置crypto-config.yaml和configtx.yaml文件(也可以用上面安装的fabric-samples下面的文件内容,基本一致)
vim configtx.yaml
---
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
Policies:
Readers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Writers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Admins:
Type: Signature
Rule: "OR('OrdererMSP.admin')"
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
Policies:
Readers:
Type: Signature
Rule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"
Writers:
Type: Signature
Rule: "OR('Org1MSP.admin', 'Org1MSP.client')"
Admins:
Type: Signature
Rule: "OR('Org1MSP.admin')"
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
Policies:
Readers:
Type: Signature
Rule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"
Writers:
Type: Signature
Rule: "OR('Org2MSP.admin', 'Org2MSP.client')"
Admins:
Type: Signature
Rule: "OR('Org2MSP.admin')"
AnchorPeers:
- Host: peer0.org2.example.com
Port: 7051
Capabilities:
Global: &ChannelCapabilities
V1_1: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_2: true
Application: &ApplicationDefaults
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ApplicationCapabilities
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
BlockValidation:
Type: ImplicitMeta
Rule: "ANY Writers"
Capabilities:
<<: *OrdererCapabilities
Channel: &ChannelDefaults
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ChannelCapabilities
Profiles:
TwoOrgsOrdererGenesis:
<<: *ChannelDefaults
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
vim crypto-config.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
# ---------------------------------------------------------------------------
# Orderer
# ---------------------------------------------------------------------------
- Name: Orderer
Domain: example.com
CA:
Country: US
Province: California
Locality: San Francisco
# ---------------------------------------------------------------------------
# "Specs" - See PeerOrgs below for complete description
# ---------------------------------------------------------------------------
Specs:
- Hostname: orderer
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
# ---------------------------------------------------------------------------
# Org1
# ---------------------------------------------------------------------------
- Name: Org1
Domain: org1.example.com
EnableNodeOUs: true
CA:
Country: US
Province: California
Locality: San Francisco
# ---------------------------------------------------------------------------
# "Specs"
# ---------------------------------------------------------------------------
# Uncomment this section to enable the explicit definition of hosts in your
# configuration. Most users will want to use Template, below
#
# Specs is an array of Spec entries. Each Spec entry consists of two fields:
# - Hostname: (Required) The desired hostname, sans the domain.
# - CommonName: (Optional) Specifies the template or explicit override for
# the CN. By default, this is the template:
#
# "{{.Hostname}}.{{.Domain}}"
#
# which obtains its values from the Spec.Hostname and
# Org.Domain, respectively.
# ---------------------------------------------------------------------------
# Specs:
# - Hostname: foo # implicitly "foo.org1.example.com"
# CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above
# - Hostname: bar
# - Hostname: baz
# ---------------------------------------------------------------------------
# "Template"
# ---------------------------------------------------------------------------
# Allows for the definition of 1 or more hosts that are created sequentially
# from a template. By default, this looks like "peer%d" from 0 to Count-1.
# You may override the number of nodes (Count), the starting index (Start)
# or the template used to construct the name (Hostname).
#
# Note: Template and Specs are not mutually exclusive. You may define both
# sections and the aggregate nodes will be created for you. Take care with
# name collisions
# ---------------------------------------------------------------------------
Template:
Count: 2
# Start: 5
# Hostname: {{.Prefix}}{{.Index}} # default
# ---------------------------------------------------------------------------
# "Users"
# ---------------------------------------------------------------------------
# Count: The number of user accounts _in addition_ to Admin
# ---------------------------------------------------------------------------
Users:
Count: 1
# ---------------------------------------------------------------------------
# Org2: See "Org1" for full specification
# ---------------------------------------------------------------------------
- Name: Org2
Domain: org2.example.com
EnableNodeOUs: true
CA:
Country: US
Province: California
Locality: San Francisco
Template:
Count: 2
Users:
Count: 1
生成公钥和证书
cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
生成创世区块
mkdir channel-artifacts
configtxgen -profile TwoOrgsOrdererGenesis -channelID mychannel -outputBlock ./channel-artifacts/genesis.block
生成通道配置文件,通道名称mychannel可以修改为自己想要的name
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
生成锚节点配置文件
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
所有需要的配置文件全部建立完成,在channel-artifacts中应该有以下四个文件:
[root@localhost first]# cd channel-artifacts/
[root@localhost channel-artifacts]# ll
-rw-r--r-- 1 root root 352 8月 30 16:06 channel.tx
-rw-r--r-- 1 root root 17457 8月 30 16:05 genesis.block
-rw-r--r-- 1 root root 288 8月 30 16:07 Org1MSPanchors.tx
-rw-r--r-- 1 root root 288 8月 30 16:07 Org2MSPanchors.tx
3、准备docker配置文件
配置docker-compose-orderer.yaml文件,拷贝到first目录下。
vim docker-compose-orderer.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer
environment:
- ORDERER_GENERAL_LOGLEVEL=debug
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/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_KAFKA_RETRY_SHORTINTERVAL=1s
- ORDERER_KAFKA_RETRY_SHORTTOTAL=30s
- ORDERER_KAFKA_VERBOSE=true
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
ports:
- 7050:7050
启动Fabric网络
docker-compose -f docker-compose-orderer.yaml up
#docker-compose -f docker-compose-orderer.yaml up -d(-d参数:后台启动)
再打开一个终端查看容器:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24dd990f8267 hyperledger/fabric-orderer:latest "orderer" 6 minutes ago Up 6 minutes 0.0.0.0:7050->7050/tcp orderer.example.com
orderer节点启动成功后,拷贝生成文件到其它电脑
cd /usr/local/go/src/github.com/hyperledger/fabric/
scp -r first/ root@192.168.2.223:/usr/local/go/src/github.com/hyperledger/fabric-samples/
scp -r first/ root@192.168.2.224:/usr/local/go/src/github.com/hyperledger/fabric-samples/
scp -r first/ root@192.168.2.225:/usr/local/go/src/github.com/hyperledger/fabric-samples/
scp -r first/ root@192.168.2.228:/usr/local/go/src/github.com/hyperledger/fabric-samples/
到此为止oderer节点配置完毕。
部署peer0.org1.example.com
配置docker-compose-peer.yaml文件,拷贝到first目录下。
vim docker-compose-peer.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer0.org1.example.com:
container_name: peer0.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=first_default
#- CORE_LOGGING_LEVEL=ERROR
- CORE_LOGGING_LEVEL=DEBUG
- 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
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /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/tls
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer.example.com:192.168.2.166"
cli:
container_name: cli
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
- 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
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/first/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/srcgithub.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer0.org1.example.com
extra_hosts:
- "orderer.example.com:192.168.2.166"
- "peer0.org1.example.com:192.168.2.223"
- "peer1.org1.example.com:192.168.2.224"
- "peer0.org2.example.com:192.168.2.225"
- "peer1.org2.example.com:192.168.2.228"
启动Fabric网络
docker-compose -f docker-compose-peer.yaml up -d
#如果要成功搭建多机下的Fabric运行环境,首先要保证五台机子上的Fabric网络可以正常运行。
所有节点网络启动之后要用telnet相互检测网络是否通信
telnet 192.168.2.227 7050
telnet 192.168.2.223 7051
如果所有节点都可以成功启动的话,接下来就可以进行链码的安装测试了
准备部署智能合约
从orderer节点拷贝下载的源码fabric-samples/chaincode/go/chaincode_example02目录到first/chaincode/go/下(我这里只在orderer节点上拉取了fabric源码文件)
cd /usr/local/go/src/github.com/hyperledger/fabric/fabric-samples/chaincode
scp -r chaincode_example02/ root@192.168.2.223:/usr/local/go/src/github.com/hyperledger/fabric/first/chaincode/go/
scp -r chaincode_example02/ root@192.168.2.224:/usr/local/go/src/github.com/hyperledger/fabric/first/chaincode/go/
scp -r chaincode_example02/ root@192.168.2.225:/usr/local/go/src/github.com/hyperledger/fabric/first/chaincode/go/
scp -r chaincode_example02/ root@192.168.2.228:/usr/local/go/src/github.com/hyperledger/fabric/first/chaincode/go/
启动cli容器(多节点部署之后启动cli容器智能从peer节点进入)
这里先在peer0.org1上操作(192.168.2.223)
docker exec -it cli bash
创建Channel
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
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/mychannel.tx --tls true --cafile $ORDERER_CA
Peer加入Channel
peer channel join -b mychannel.block
peer channel list(查看通道列表)
保存mychannel.block #保存图中红框中的字符。
mychannel.block拷贝到其它peer节点:
exit
docker cp 65854691cc7c:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block /usr/local/go/src/github.com/hyperledger/fabric/first
安装与运行智能合约
安装智能合约
docker exec -it cli bash
#-n 指定mycc是由用户定义的链码名字,-v 指定1.0是链码的版本,-p ...是指定链码的路径
peer chaincode install -n mycc -p github.com/hyperledger/fabric/first/chaincode/go/chaincode_example02/go/ -v 1.0
实例化智能合约:区块初始化数据为a为100,b为200。
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
peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
Peer上查询a,显示100 ,(第一次查询都会比较慢)
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
查询b当然就是200
Peer上进行a向b转10交易
peer chaincode invoke --tls --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
查询b成功结果如下图所示
查看智能合约容器:
[root@localhost first]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9031b208c201 dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9 "chaincode -peer.add…" 16 hours ago Up 16 hours dev-peer0.org1.example.com-mycc-1.0
dc0e1c4df140 hyperledger/fabric-tools "/bin/bash" 16 hours ago Up 16 hours cli
ee9033384f67 hyperledger/fabric-peer "peer node start" 16 hours ago Up 16 hours 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com
#到此为止peer0-Org1部署完毕。
部署peer0.org2.example.com
配置docker-compose-peer.yaml文件,拷贝到first目录下。
vim docker-compose-peer.yaml:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer0.org2.example.com:
container_name: peer0.org2.example.com
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer0.org2.example.com
- CORE_PEER_ADDRESS=peer0.org2.example.com: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_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=first_default
#- CORE_LOGGING_LEVEL=ERROR
- CORE_LOGGING_LEVEL=DEBUG
- 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
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer.example.com:192.168.2.227"
cli:
container_name: cli
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
- CORE_PEER_ADDRESS=peer0.org2.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.org2.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.org2.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.org2.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
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/first/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer0.org2.example.com
extra_hosts:
- "orderer.example.com:192.168.2.227"
- "peer0.org1.example.com:192.168.2.223"
- "peer1.org1.example.com:192.168.2.224"
- "peer0.org2.example.com:192.168.2.225"
- "peer1.org2.example.com:192.168.2.228"
准备部署智能合约
启动Fabric网络
cd /usr/local/go/src/github.com/hyperledger/fabric/first
docker-compose -f docker-compose-peer.yaml up -d
启动cli容器
docker exec -it cli bash
拷贝mychannel.block到peer中
docker cp mychannel.block c88f501f0237:/opt/gopath/src/github.com/hyperledger/fabric/peer/
Peer加入Channel
docker exec -it cli bash
peer channel join -b mychannel.block
安装与运行智能合约
安装智能合约
peer chaincode install -n mycc -p github.com/hyperledger/fabric/first/chaincode/go/chaincode_example02/go/ -v 1.0
Peer上查询b,显示210
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Peer上进行a再向b转10交易
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
peer chaincode invoke --tls --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
Peer上查询a,显示80
部署peer1.org1.example.com
配置docker-compose-peer.yaml文件,拷贝到first目录下。
vim docker-compose-peer.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer1.org1.example.com:
container_name: peer1.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer1.org1.example.com
- CORE_PEER_ADDRESS=peer1.org1.example.com:7051
- CORE_PEER_CHAINCODEADDRESS=peer1.org1.example.com:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=first_default
#- CORE_LOGGING_LEVEL=ERROR
- CORE_LOGGING_LEVEL=DEBUG
- 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
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /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/tls
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer.example.com:192.168.2.227"
cli:
container_name: cli
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
- CORE_PEER_ADDRESS=peer1.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/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/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/first/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer1.org1.example.com
extra_hosts:
- "orderer.example.com:192.168.2.227"
- "peer0.org1.example.com:192.168.2.223"
- "peer1.org1.example.com:192.168.2.224"
- "peer0.org2.example.com:192.168.2.225"
- "peer1.org2.example.com:192.168.2.228"
准备部署智能合约
启动Fabric网络
cd /usr/local/go/src/github.com/hyperledger/fabric/first
docker-compose -f docker-compose-peer.yaml up -d
后续操作步骤省略…(与前面所有的操作都一致)
部署peer1.org2.example.com
准备docker配置文件
配置docker-compose-peer.yaml文件,拷贝到first目录下。
vim docker-compose-peer.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer1.org2.example.com:
container_name: peer1.org2.example.com
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer1.org2.example.com
- CORE_PEER_ADDRESS=peer1.org2.example.com:7051
- CORE_PEER_CHAINCODEADDRESS=peer1.org2.example.com:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:7051
- CORE_PEER_LOCALMSPID=Org2MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=first_default
#- CORE_LOGGING_LEVEL=ERROR
- CORE_LOGGING_LEVEL=DEBUG
- 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
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /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/tls
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer.example.com:192.168.2.227"
cli:
container_name: cli
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
- CORE_PEER_ADDRESS=peer1.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/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/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/first/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer1.org2.example.com
extra_hosts:
- "orderer.example.com:192.168.2.227"
- "peer0.org1.example.com:192.168.2.223"
- "peer1.org1.example.com:192.168.2.224"
- "peer0.org2.example.com:192.168.2.225"
- "peer1.org2.example.com:192.168.2.228"
准备部署智能合约
启动Fabric网络
cd /usr/local/go/src/github.com/hyperledger/fabric/first
docker-compose -f docker-compose-peer.yaml up -d
后续操作步骤省略…(与前面所有的操作都一致)
关闭网络:(建议每次up前先执行down):
docker-compose -f docker-compose-orderer.yaml down --volumes
docker-compose -f docker-compose-peer0-.yaml down –volumes
#docker-compose -f docker-compose-peer0-Org1.yaml -f docker-compose-peer0-Org1-couch.yaml
出现容器无法删除的情况,我们可以执行以下命令进行删除:
docker rm $(docker ps -aq)
Hyperledger Fabric单机部署文档