1准备工作介绍

1各个主机ip以及节点分配情况

各个主机的节点分配情况 ip地址
orderer0.example.com,peer0.org1.example.com 172.17.3.60
orderer1.example.com,peer1.org1.example.com 172.17.3.61
orderer2.example.com,peer2.org1.example.com 172.17.3.62

2.基于Raft共识的多机的fabric网络搭建

​ 这一节是对每台阿里云主机的各个节点的网络配置,其中主要涉及的是配置文件,每一步的后面会标注目标主机。

1. 创建raft目录(172.17.3.60)

mkdir /root/go/src/github.com/hyperledger/fabric/raft
cd /root/go/src/github.com/hyperledger/fabric/raft

2. 获取生成工具(172.17.3.60)

上传hyperledger-fabric-linux-amd64-1.4.4.tar.gz二进制文件包并解压,然后赋予权限。

tar -zxvf hyperledger-fabric-linux-amd64-1.4.4.tar.gz
chmod -R 777 ./bin

3. 准备crypto-config.yaml和configtx.yaml文件(172.17.3.60)

  • crypto-config.yaml
OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer0
- Hostname: orderer1
- Hostname: orderer2
PeerOrgs:
- Name: Org1
Domain: org1.example.com
EnableNodeOUs: true
Template:
Count: 3
Users:
Count: 3
  • 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
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
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')"
Capabilities:
Channel: &ChannelCapabilities
V1_4_3: true
V1_3: false
V1_1: false
Orderer: &OrdererCapabilities
V1_4_2: true
V1_1: false
Application: &ApplicationCapabilities
V1_4_2: true
V1_3: false
V1_2: false
V1_1: false
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:
- orderer0.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 200
AbsoluteMaxBytes: 2 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"
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
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
OrdererType: etcdraft
EtcdRaft:
Consenters:
- Host: orderer0.example.com
Port: 7050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt
- Host: orderer1.example.com
Port: 7050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt
- Host: orderer2.example.com
Port: 7050
ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt
ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt
Addresses:
- orderer0.example.com:7050
- orderer1.example.com:7050
- orderer2.example.com:7050
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Application:
<<: *ApplicationDefaults
Organizations:
- <<: *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
TwoOrgsChannel:
Consortium: SampleConsortium
<<: *ChannelDefaults
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
Capabilities:
<<: *ApplicationCapabilities

  

4. 生成公私钥和证书(172.17.3.60)

./bin/cryptogen generate --config=./crypto-config.yaml

5.生成创世区块(172.17.3.60)

mkdir channel-artifacts
./bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID systemchannel -outputBlock ./channel-artifacts/genesis.block

6. 生成通道配置区块(172.17.3.60)

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

7.上传智能合约(172.17.3.60)

mkdir  chaincode    //将自己写好的链码放置此处即可

8.拷贝生成文件到其它电脑(172.17.3.60)

cd ..
scp -r raft root@172.17.3.61:/root/go/src/github.com/hyperledger/fabric
scp -r raft root@172.17.3.62:/root/go/src/github.com/hyperledger/fabric

9.准备orderer配置文件(172.17.3.60)

docker-compose-orderer.yaml

version: '2'
volumes:
orderer0.example.com:
services:
orderer0.example.com:
container_name: orderer0.example.com
image: hyperledger/fabric-orderer:latest
environment:
- FABRIC_LOGGING_SPEC=INFO
- GODEBUG=netdns=go
- 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_TOPIC_REPLICATIONFACTOR=1
- ORDERER_KAFKA_VERBOSE=true
- 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/fabric
command: orderer
volumes:
- ./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/tls
- orderer0.example.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"

10.准备peer配置文件(172.17.3.60)

docker-compose-peer.yaml

version: '2'
volumes:
peer0.org1.example.com:
services:
peer0.org1.example.com:
container_name: peer0.org1.example.com
hostname: peer0.org1.example.com
image: hyperledger/fabric-peer:latest
environment:
- CORE_PEER_ID=peer0.org1.example.com
- GODEBUG=netdns=go
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_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
- peer0.org1.example.com:/var/hyperledger/production
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"

11.准备cli配置文件(172.17.3.60)

docker-compose-cli.yaml

version: '2'
services:
cli:
container_name: cli
image: hyperledger/fabric-tools:latest
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
- GODEBUG=netdns=go
- 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:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"
- "peer0.org1.example.com:172.17.3.60"
- "peer1.org1.example.com:172.17.3.61"
- "peer2.org1.example.com:172.17.3.62"

12.准备ca配置文件(172.17.3.60)

docker-compose-ca.yaml

version: '2'

services:
ca0:
image: hyperledger/fabric-ca:latest
environment:
- FABRIC_CA_HOME=/var/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-org1
- FABRIC_CA_SERVER_CA_CERTFILE=/var/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_CA_KEYFILE=/var/hyperledger/fabric-ca-server-config/01695cb495cf30e01ec3f405f9ef370a35713ece8cbbf7d1334da245fbfa029c_sk
- FABRIC_CA_SERVER_TLS_ENABLED=true
- FABRIC_CA_SERVER_TLS_CERTFILE=/var/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_TLS_KEYFILE=/var/hyperledger/fabric-ca-server-config/01695cb495cf30e01ec3f405f9ef370a35713ece8cbbf7d1334da245fbfa029c_sk
- FABRIC_CA_SERVER_PORT=7054
ports:
- "7054:7054"
command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
volumes:
- ./crypto-config/peerOrganizations/org1.example.com/ca/:/var/hyperledger/fabric-ca-server-config
container_name: ca_peerOrg1

13.创建启动容器脚本(172.17.3.60)

up.sh

#!/bin/bash
docker-compose -f ./docker-compose-orderer.yaml up -d
sleep 10
docker-compose -f ./docker-compose-peer.yaml up -d
docker-compose -f ./docker-compose-cli.yaml up -d
docker-compose -f ./docker-compose-ca.yaml up -d
//给脚本添加权限
chmod +x up.sh

准备第二台主机的配置,进入raft文件夹

1.准备orderer配置文件(172.17.3.61)

docker-compose-orderer.yaml

version: '2'
volumes:
orderer1.example.com:
services:
orderer1.example.com:
container_name: orderer1.example.com
image: hyperledger/fabric-orderer:latest
environment:
- FABRIC_LOGGING_SPEC=INFO
- GODEBUG=netdns=go
- 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_TOPIC_REPLICATIONFACTOR=1
- ORDERER_KAFKA_VERBOSE=true
- 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/fabric
command: orderer
volumes:
- ./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/tls
- orderer1.example.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"

2.准备peer配置文件(172.17.3.61)

docker-compose-peer.yaml

version: '2'
volumes:
peer1.org1.example.com:
services:
peer1.org1.example.com:
container_name: peer1.org1.example.com
hostname: peer1.org1.example.com
image: hyperledger/fabric-peer:latest
environment:
- CORE_PEER_ID=peer1.org1.example.com
- GODEBUG=netdns=go
- CORE_PEER_ADDRESS=peer1.org1.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer1.org1.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_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
- peer1.org1.example.com:/var/hyperledger/production
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"

3.准备cli配置文件(172.17.3.61)

docker-compose-cli.yaml

version: '2'
services:
cli:
container_name: cli
image: hyperledger/fabric-tools:latest
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
- GODEBUG=netdns=go
- 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:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"
- "peer0.org1.example.com:172.17.3.60"
- "peer1.org1.example.com:172.17.3.61"
- "peer2.org1.example.com:172.17.3.62"

4.创建启动容器脚本(172.17.3.61)

up.sh

#!/bin/bash
docker-compose -f ./docker-compose-orderer.yaml up -d
sleep 10
docker-compose -f ./docker-compose-peer.yaml up -d
docker-compose -f ./docker-compose-cli.yaml up -d
//给脚本添加权限
chmod +x up.sh

  

准备第三台主机的配置,进入raft文件夹

1.准备orderer配置文件(172.17.3.62)

docker-compose-orderer.yaml

version: '2'
volumes:
orderer2.example.com:
services:
orderer2.example.com:
container_name: orderer2.example.com
image: hyperledger/fabric-orderer:latest
environment:
- FABRIC_LOGGING_SPEC=INFO
- GODEBUG=netdns=go
- 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_TOPIC_REPLICATIONFACTOR=1
- ORDERER_KAFKA_VERBOSE=true
- 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/fabric
command: orderer
volumes:
- ./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/tls
- orderer2.example.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"

2.准备peer配置文件(172.17.3.62)

docker-compose-peer.yaml

version: '2'
volumes:
peer2.org1.example.com:
services:
peer2.org1.example.com:
container_name: peer2.org1.example.com
hostname: peer2.org1.example.com
image: hyperledger/fabric-peer:latest
environment:
- CORE_PEER_ID=peer2.org1.example.com
- GODEBUG=netdns=go
- 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_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_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/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
- peer2.org1.example.com:/var/hyperledger/production
ports:
- 7051:7051
- 7052:7052
- 7053:7053
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"

3.准备cli配置文件(172.17.3.62)

docker-compose-cli.yaml

version: '2'
services:
cli:
container_name: cli
image: hyperledger/fabric-tools:latest
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_SPEC=INFO
- GODEBUG=netdns=go
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer2.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/peer2.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/peer2.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/peer2.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:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
extra_hosts:
- "orderer0.example.com:172.17.3.60"
- "orderer1.example.com:172.17.3.61"
- "orderer2.example.com:172.17.3.62"
- "peer0.org1.example.com:172.17.3.60"
- "peer1.org1.example.com:172.17.3.61"
- "peer2.org1.example.com:172.17.3.62"

4.创建启动容器脚本(172.17.3.62)

up.sh

#!/bin/bash
docker-compose -f ./docker-compose-orderer.yaml up -d
sleep 10
docker-compose -f ./docker-compose-peer.yaml up -d
docker-compose -f ./docker-compose-cli.yaml up -d
//给脚本添加权限
chmod +x up.sh
  • 各个主机进行交互时,主机名的解析会影响交互速度,所以我们在/etc/hosts中将ip与主机名绑定起来,执行vi /etc/hosts,并将下面代码块中的内容填写到文件中(每台主机都改)
172.17.3.60 peer0.org1.example.com
172.17.3.61 peer1.org1.example.com
172.17.3.62 peer2.org1.example.com
172.17.3.60 orderer0.example.com
172.17.3.61 orderer1.example.com
172.17.3.62 orderer2.example.com

  然后就可以启动每一台主机的启动容器脚本up.sh了

./up.sh

  

3.利用cli命令行工具和fabric底层网络进行交互

172.17.3.60:

docker exec -it cli bash    //进入到cli容器中
peer channel create -o orderer0.example.com:7050 -c testchannel -f ./channel-artifacts/channel.tx --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 channel join -b testchannel.block //当前peer加入到通道中
peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode //在当前peer上安装链码
peer chaincode package -p github.com/chaincode -n mycc -v 1.0 mycc.1.0.out //将链码打包
exit //退出容器
docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/testchannel.block ./channel-artifacts //将生成的创世块文件从docker容器中拷贝出来
docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mycc.1.0.out ./channel-artifacts //将链码文件从docker容器中拷贝出来
//然后将创世块和链码拷贝至 其他两台主机
cd channel-artifacts
scp -r testchannel.block root@172.17.3.61:/root/go/src/github.com/hyperledger/fabric/raft/channel-artifacts
scp -r mycc.1.0.out root@172.17.3.62:/root/go/src/github.com/hyperledger/fabric/raft/channel-artifacts

172.17.3.61:

docker exec -it cli bash    //进入到cli容器中
mv ./channel-artifacts/testchannel.block /opt/gopath/src/github.com/hyperledger/fabric/peer //将创世块文件拷贝到docker容器内
mv ./channel-artifacts/mycc.1.0.out /opt/gopath/src/github.com/hyperledger/fabric/peer //将链码文件拷贝到docker容器内
peer channel join -b testchannel.block //当前peer加入到通道中
peer chaincode install mycc.1.0.out //当前peer上安装链码

  

172.17.3.62:

docker exec -it cli bash    //进入到cli容器中
mv ./channel-artifacts/testchannel.block /opt/gopath/src/github.com/hyperledger/fabric/peer //将创世块文件拷贝到docker容器内
mv ./channel-artifacts/mycc.1.0.out /opt/gopath/src/github.com/hyperledger/fabric/peer //将链码文件拷贝到docker容器内
peer channel join -b testchannel.block //当前peer加入到通道中
peer chaincode install mycc.1.0.out //当前peer上安装链码

  

172.17.3.60:

docker exec -it cli bash    //进入到cli容器中

peer chaincode instantiate -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 testchannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND('Org1MSP.peer')"    //实例化链码

peer chaincode query -C testchannel -n mycc -c '{"Args":["query","a"]}'   //查询a的数据,看是否初始化成功

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 testchannel -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 -c '{"Args":["invoke","a","b","10"]}'       //将a的10块钱转账给b

  

172.17.3.61:

docker exec -it cli bash    //进入到cli容器中
peer chaincode query -C testchannel -n mycc -c '{"Args":["query","a"]}' //查询交互后的a的值

172.17.3.62:

docker exec -it cli bash    //进入到cli容器中
peer chaincode query -C testchannel -n mycc -c '{"Args":["query","b"]}' //查询交互后的b的值

  

基于raft共识搭建的Fabric1.4.4多机网络环境的更多相关文章

  1. 基于Ubuntu系统搭建以太坊go-ethereum源码的开发环境

    第一.先安装geth的CLI环境sudo apt-get install geth,这个很重要 第二.下载源代码 git clone https://github.com/ethereum/go-et ...

  2. 搭建Mac+Java+appium+IOS真机自动化环境

    一.安装前环境准备 1.确保电脑已经有homebrew(包管理器)  下载链接[https://brew.sh/]   2.通过 brew 安装node.js brew install node 安装 ...

  3. raft共识算法

    raft共识算法 分布式一致性问题 如果说,服务器只有一个节点,那么,要保证一致性,没有任何问题,因为所有读写都在一个节点上发生.那如果server端有2个.3个甚至更多节点,要怎么达成一致性呢?下面 ...

  4. 实践案例丨基于 Raft 协议的分布式数据库系统应用

    摘要:简单介绍Raft协议的原理.以及存储节点(Pinetree)如何应用 Raft实现复制的一些工程实践经验. 1.引言 在华为分布式数据库的工程实践过程中,我们实现了一个计算存储分离. 底层存储基 ...

  5. Fabric2.2中的Raft共识模块源码分析

    引言 Hyperledger Fabric是当前比较流行的一种联盟链系统,它隶属于Linux基金会在2015年创建的超级账本项目且是这个项目最重要的一个子项目.目前,与Hyperledger的另外几个 ...

  6. 【PHP】基于ThinkPHP框架搭建OAuth2.0服务

    [PHP]基于ThinkPHP框架搭建OAuth2.0服务 http://leyteris.iteye.com/blog/1483403

  7. 基于Raft构建弹性伸缩的存储系统的一些实践

    基于Raft构建弹性伸缩的存储系统的一些实践 原创 2016-07-18 黄东旭 聊聊架构 最近几年来,越来越多的文章介绍了 Raft 或者 Paxos 这样的分布式一致性算法,但主要集中在算法细节和 ...

  8. 基于 Jenkins 快速搭建持续集成环境--转

    源地址:http://www.ibm.com/developerworks/cn/java/j-lo-jenkins/ 持续集成是一种软件开发实践,对于提高软件开发效率并保障软件开发质量提供了理论基础 ...

  9. 基于Android Studio搭建hello world工程

    基于Android Studio搭建hello world工程 版本:ANDROID STUDIO V0.4.6 This download includes: ·        Android St ...

随机推荐

  1. Mybatis源码学习第八天(总结)

    源码学习到这里就要结束了; 来总结一下吧 Mybatis的总体架构 这次源码学习我们,学习了重点的模块,在这里我想说一句,源码的学习不是要所有的都学,一行一行的去学,这是错误的,我们只需要学习核心,专 ...

  2. JVM执行子程序

    Class文件结构 计算机只认识0和1,这个称之为本地机器NativeCode Jvm的无关性 与平台无关性是建立在操作系统上,虚拟机厂商提供了许多可以运行在各种不同平台的虚拟机,它们都可以载入和执行 ...

  3. 企业邮箱选择,商务办公为什么选TOM企业邮箱?

    企业邮箱是工作中的重要工具,它可以帮助我们更规范的上传下达.更高效的管理工作,也是拓展合作伙伴的敲门砖及必杀技.比如写一封诚意满满的合作邀请,再比如重要关头写一封合作协议.毫不夸张,企业邮箱不仅能节省 ...

  4. pytest测试框架 -- setup和teardown等

    一.用例运行级别 1.函数级别(setup.teardown 或 setup_function.teardown_function): 仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测 ...

  5. oracle之WHERE子句中常用的运算符

    WHERE子句中常用的运算符 4.1 运算符及优先级: 算数运算符*,/,+,-, 逻辑运算符not, and ,or 比较运算符 单行比较运算 =,>, >=,<,<=, & ...

  6. CEO的行为风格会影响公司业绩吗?

    中国的两大互联网巨头--腾讯和阿里,创始人的风格非常不同.在公众面前,马云的形象是高谈阔论,而马化腾则显得较为低调.在公司管理上,马云不插手具体事务,而是站在高处务虚,抓战略.抓文化,而马化腾则是腾讯 ...

  7. Java基本数据类型、关键字

    Java基本数据类型.关键字 一.Java基本数据类型 Java有8中基本数据类型:byte\char \short \int\float\double\long\boolean 在说明每种类型大小前 ...

  8. Spock测试套件入门

    目录 Spock测试套件 核心概念 整体认识 前置.后置 同junit的类比 Feature 方法 blocks 典型的用法 异常condition then和expect的区别 cleanup bl ...

  9. hystrix文档翻译之概述

    Hystrix是什么 在一个大型的分布式系统中,难免有些依赖服务会失败.hystrix通过容错逻辑来控制不同服务间的交互.hystrix通过隔离各服务交互节点来防止连级错误,并且提供降级功能,最终保证 ...

  10. Anaconda使用及管理

    接下来均是以命令行模式进行介绍,Windows用户请打开"Anaconda Prompt":macOS和Linux用户请打开"Terminal"("终 ...