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的值

  

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/lin-zhifeng/p/13737857.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!