hyperledger fabric 网络操作基本操作和概念

Posted 计划是动力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hyperledger fabric 网络操作基本操作和概念相关的知识,希望对你有一定的参考价值。

hyperledger fabric 网络操作基本操作和概念

hyperledger fabric 网络操作基本操作和概念

在搭建好hyperledger fabric环境后,继续进行网络搭建操作
新建文件夹twonodes(与github.com同级)

1. cryptogen 操作

1.1 cryptogen 生成证书文件

1.1.1 生成证书文件模板

# 打印模板到终端
cryptogen showtemplate
# 打印模板到配置文件crypto-config.yaml
cryptogen showtemplate > crypto-config.yaml

生成模板配置文件说明

1.1.1.1 排序节点OrdererOrgs配置

OrdererOrgs:
  - Name: Orderer # 排序节点名称
    Domain: example.com # 排序节点域名
    EnableNodeOUs: false # 是否使用OU配置(OU表示组织单元即每个组织下面很多节点,可以将一些节点作为一个部门)
    # "Specs" - See PeerOrgs below for complete description
    Specs:
      - Hostname: orderer

1.1.1.2 普通节点PeerOrgs配置

PeerOrgs:
  # Org1 组织1
  - Name: Org1 #组织1名称
    Domain: org1.example.com #组织1域名
    EnableNodeOUs: true

1.1.1.3 Template 配置

Template:
      Count: 1 # 组织节点个数,节点名称顺序排列
      # Start: 5
      # Hostname: .Prefix.Index # default
      # SANS:
      #   - ".Hostname.alt..Domain"

    #

1.1.1.4 Users用户配置

(user和peer区别,user可以理解为终端,指人,而peer是节点,只是一台运行的机器或集群的一个节点)

    # "Users"
    # Count: The number of user accounts _in addition_ to Admin
    Users:
      Count: 1

1.1.1.5 crypto-config.yaml

Order组织节点定义

OrdererOrgs:
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer
    Domain: trace.com
    Specs:
      - Hostname: orderer

PeerOrgs:
  # ---------------------------------------------------------------------------
  # Org1
  # ---------------------------------------------------------------------------
  - Name: Org1
    Domain: org1.trace.com
    Template:
      Count: 1
    Users:
      Count: 1
 # ---------------------------------------------------------------------------
  # Org2
  # ---------------------------------------------------------------------------
  - Name: Org2
    Domain: org2.trace.com
    Template:
      Count: 1
    Users:
      Count: 1
 # ---------------------------------------------------------------------------
  # Org3
  # ---------------------------------------------------------------------------
  - Name: Org3
    Domain: org3.trace.com
    Template:
      Count: 1
    Users:
      Count: 1
 # ---------------------------------------------------------------------------
  # Org4
  # ---------------------------------------------------------------------------
  - Name: Org4
    Domain: org4.trace.com
    Template:
      Count: 1
    Users:
      Count: 1

# ---------------------------------------------------------------------------
  # Org5
  # ---------------------------------------------------------------------------
  - Name: Org5
    Domain: org5.trace.com
    Template:
      Count: 1
    Users:
      Count: 1

1.1.2 生成密钥材料

cryptogen generate --config=crypto-config.yaml

1.2 cryptogen.yaml 创建通道配置

拷贝/hyperledger/fabric-samples/test-network/configtx/configx.yaml 到目录下
修改msp配置文件(成员服务提供者 (MSP)及相关证书配置
因为Fabric是一个认证性的网络, 所以区块链参与者需要一种向网络中的其他参与者证实自己身份的机制从而在网络中进行交易
证书机构通过生成可以用来证实身份的由公钥和私钥形成的键值对来发放认证信息。因为一个私钥永远不会被公开,所以引入了一种可以证实身份的机制即MSP。例如,一个peer节点用它的私钥进行数字签名或背书交易。接着排序节点包含的该peer节点的公钥会被用来验证交易携带的签名是否合法。私钥被用作生成交易信息上的,只有与私钥相对应的且作为MSP一部分的公钥可以匹配的签名。因此,MSP是一个可让身份被信任和被网络中其他参与者公认的,而不需要暴露成员的私钥的机制。

MSPDir: ../organizations/ordererOrganizations/example.com/msp
# 改为
MSPDir: crypto-config/ordererOrganizations/example.com/msp

1.2.1 configx.yaml

#
# SPDX-License-Identifier: Apache-2.0
#

---
################################################################################
#
#   Section: Organizations
#
#   - This section defines the different organizational identities which will
#   be referenced later in the configuration.
#
################################################################################
Organizations:

    # SampleOrg defines an MSP using the sampleconfig.  It should never be used
    # in production but may be used as a template for other definitions
    - &OrdererOrg
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: OrdererOrg

        # ID to load the MSP definition as
        ID: OrdererMSP

        # MSPDir is the filesystem path which contains the MSP configuration
        MSPDir: crypto-config/ordererOrganizations/trace.com/msp

    - &Org1
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org1MSP

        # ID to load the MSP definition as
        ID: Org1MSP

        MSPDir: crypto-config/peerOrganizations/org1.trace.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org1.trace.com
              Port: 7051

    - &Org2
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org2MSP

        # ID to load the MSP definition as
        ID: Org2MSP

        MSPDir: crypto-config/peerOrganizations/org2.trace.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org2.trace.com
              Port: 7051

    - &Org3
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org3MSP

        # ID to load the MSP definition as
        ID: Org3MSP

        MSPDir: crypto-config/peerOrganizations/org3.trace.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org3.trace.com
              Port: 7051


    - &Org4
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org4MSP

        # ID to load the MSP definition as
        ID: Org4MSP

        MSPDir: crypto-config/peerOrganizations/org4.trace.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org4.trace.com
              Port: 7051

    - &Org5
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org5MSP

        # ID to load the MSP definition as
        ID: Org5MSP

        MSPDir: crypto-config/peerOrganizations/org5.trace.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org5.trace.com
              Port: 7051

################################################################################
#
#   SECTION: Capabilities
#
#   - This section defines the capabilities of fabric network. This is a new
#   concept as of v1.1.0 and should not be utilized in mixed networks with
#   v1.0.x peers and orderers.  Capabilities define features which must be
#   present in a fabric binary for that binary to safely participate in the
#   fabric network.  For instance, if a new MSP type is added, newer binaries
#   might recognize and validate the signatures from this type, while older
#   binaries without this support would be unable to validate those
#   transactions.  This could lead to different versions of the fabric binaries
#   having different world states.  Instead, defining a capability for a channel
#   informs those binaries without this capability that they must cease
#   processing transactions until they have been upgraded.  For v1.0.x if any
#   capabilities are defined (including a map with all capabilities turned off)
#   then the v1.0.x peer will deliberately crash.
#
################################################################################
Capabilities:
    # Channel capabilities apply to both the orderers and the peers and must be
    # supported by both.  Set the value of the capability to true to require it.
    Global: &ChannelCapabilities
        # V1.1 for Global is a catchall flag for behavior which has been
        # determined to be desired for all orderers and peers running v1.0.x,
        # but the modification of which would cause incompatibilities.  Users
        # should leave this flag set to true.
        V1_1: true

    # Orderer capabilities apply only to the orderers, and may be safely
    # manipulated without concern for upgrading peers.  Set the value of the
    # capability to true to require it.
    Orderer: &OrdererCapabilities
        # V1.1 for Order is a catchall flag for behavior which has been
        # determined to be desired for all orderers running v1.0.x, but the
        # modification of which  would cause incompatibilities.  Users should
        # leave this flag set to true.
        V1_1: true

    # Application capabilities apply only to the peer network, and may be safely
    # manipulated without concern for upgrading orderers.  Set the value of the
    # capability to true to require it.
    Application: &ApplicationCapabilities
        # V1.2 for Application is a catchall flag for behavior which has been
        # determined to be desired for all peers running v1.0.x, but the
        # modification of which would cause incompatibilities.  Users should
        # leave this flag set to true.
        V1_2: true

################################################################################
#
#   SECTION: Application
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for application related parameters
#
################################################################################
Application: &ApplicationDefaults

    # Organizations is the list of orgs which are defined as participants on
    # the application side of the network
    Organizations:

################################################################################
#
#   SECTION: Orderer
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for orderer related parameters
#
################################################################################
Orderer: &OrdererDefaults

    # Orderer Type: The orderer implementation to start
    # Available types are "solo" and "kafka"
    OrdererType: solo

    Addresses:
        - orderer.trace.com:7050
    # Batch Timeout: The amount of time to wait before creating a batch
    BatchTimeout: 2s

    # Batch Size: Controls the number of messages batched into a block
    BatchSize:

        # Max Message Count: The maximum number of messages to permit in a batch
        MaxMessageCount: 10

        # Absolute Max Bytes: The absolute maximum number of bytes allowed for
        # the serialized messages in a batch.
        AbsoluteMaxBytes: 99 MB

        # Preferred Max Bytes: The preferred maximum number of bytes allowed for
        # the serialized messages in a batch. A message larger than the preferred
        # max bytes will result in a batch larger than preferred max bytes.
        PreferredMaxBytes: 512 KB

    Kafka:
        # Brokers: A list of Kafka brokers to which the orderer connects
        # NOTE: Use IP:port notation
        Brokers:
            - 127.0.0.1:9092

    # Organizations is the list of orgs which are defined as participants on
    # the orderer side of the network
    Organizations:

################################################################################
#
#   Profile
#
#   - Different configuration profiles may be encoded here to be specified
#   as parameters to the configtxgen tool
#
################################################################################
Profiles:

    FiveOrgsOrdererGenesis:
        Capabilities:
            <<: *ChannelCapabilities
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
                    - *Org3
                    - *Org4
                    - *Org5
    FiveOrgsChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2
                - *Org3
                - *Org4
                - *Org5
            Capabilities:
                <<: *ApplicationCapabilities
  

1.3 网络初始化

configtxgen -profile SampleMultiNodeEtcdRaft -channelID systemchannel -outputBlock ./channel-artifacts/genesis.block
# 生成系统文件的创世块
configtxgen -profile Thr eeOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID testchannel
# 生成用于创建应用通道的通道文件
configtxgen -profile ThreeOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID appchannel -asOrg Org1MSP
# 生成Org1进行锚节点更新的文件
configtxgen -profile ThreeOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID appchannel -asOrg Org2MSP
# 生成Org2进行锚节点更新的文件
configtxgen -profile ThreeOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID appchannel -asOrg Org3MSP
# 生成Org3进行锚节点更新的文件

2. docker compose

区块链docker节点集群搭建(基于docker compose)

2.1 docker-compose.yaml

#i
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'

volumes:
  orderer.trace.com:
  peer0.org1.trace.com:
  peer0.org2.trace.com:
  peer0.org3.trace.com:
  peer0.org4.trace.com:
  peer0.org5.trace.com:

networks:
  basic:

services:
  ca.trace.com:
    image: hyperledger/fabric-ca
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca.trace.com
      - FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.trace.com-cert.pem
      - FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/38495dada2d3a879981547003d601ed405cde661e55e568cce718a6618360d79_sk
      
    ports:
      - 7054:7054
    command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.trace.com/ca/:/etc/hyperledger/fabric-ca-server-config
    container_name: ca.trace.com
    networks:
      - basic

  orderer.trace.com:
    container_name: orderer.trace.com
    image: hyperledger/fabric-orderer
    environment:
      - ORDERER_GENERAL_LOGLEVEL=INFO
      - 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]
    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/trace.com/orderers/orderer.trace.com/msp:/var/hyperledger/orderer/msp
    - ./crypto-config/ordererOrganizations/trace.com/orderers/orderer.trace.com/tls/:/var/hyperledger/orderer/tls
    - orderer.trace.com:/var/hyperledger/production/orderer
    ports:
      - 7050:7050
    networks:
      - basic


  peer0.org1.trace.com:
    container_name: peer0.org1.trace.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org1.trace.com
      - CORE_LOGGING_PEER=info
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
      # # 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=basic-network_basic
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
      # enabled TLS
#      - CORE_PEER_TLS_ENABLED=true
#      - 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

    #  - CORE_PEER_GOSSIP_USELEADERELECTION=true
     # - CORE_PEER_GOSSIP_ORGLEADER=false
     # - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.trace.com:7051
     # - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.trace.com:7051
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    #command: peer node start --peer-chaincodedev=true
    ports:
      - 7051:7051
#      - 7052:7052
      - 7053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org1.trace.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
#        - ./crypto-config/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/msp:/etc/hyperledger/fabric/msp
#        - ./crypto-config/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org1.trace.com:/var/hyperledger/production
    depends_on:
      - orderer.trace.com
      - couchdb
    networks:
      - basic


  peer0.org2.trace.com:
    container_name: peer0.org2.trace.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org2.trace.com
      - CORE_LOGGING_PEER=info
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org2MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org2.trace.com:7051
      # # 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=basic-network_basic
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
      # enabled TLS
#      - CORE_PEER_TLS_ENABLED=true
#      - 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
      
      #- CORE_PEER_GOSSIP_USELEADERELECTION=true
      #- CORE_PEER_GOSSIP_ORGLEADER=false
      #- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.trace.com:7051
      #- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.trace.com:7051

    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    #command: peer node start --peer-chaincodedev=true
    ports:
      - 8051:7051
#      - 8052:7052
      - 8053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org2.trace.com/peers/peer0.org2.trace.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org2.trace.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
#        - ./crypto-config/peerOrganizations/org2.trace.com/peers/peer0.org2.trace.com/msp:/etc/hyperledger/fabric/msp
#        - ./crypto-config/peerOrganizations/org2.trace.com/peers/peer0.org2.trace.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org2.trace.com:/var/hyperledger/production
    depends_on:
      - orderer.trace.com
      - couchdb
    networks:
      - basic


  peer0.org3.trace.com:
    container_name: peer0.org3.trace.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org3.trace.com
      - CORE_LOGGING_PEER=info
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org3MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org3.trace.com:7051
      # # 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=basic-network_basic
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
      # enabled TLS
#      - CORE_PEER_TLS_ENABLED=true
#      - 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

      #- CORE_PEER_GOSSIP_USELEADERELECTION=true
      #- CORE_PEER_GOSSIP_ORGLEADER=false
      #- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org3.trace.com:7051
      #- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org3.trace.com:7051
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    #command: peer node start --peer-chaincodedev=true
    ports:
      - 9051:7051
      - 9053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org3.trace.com/peers/peer0.org3.trace.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org3.trace.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
#        - ./crypto-config/peerOrganizations/org3.trace.com/peers/peer0.org3.trace.com/msp:/etc/hyperledger/fabric/msp
#        - ./crypto-config/peerOrganizations/org3.trace.com/peers/peer0.org3.trace.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org3.trace.com:/var/hyperledger/production
    depends_on:
      - orderer.trace.com
      - couchdb
    networks:
      - basic


  peer0.org4.trace.com:
    container_name: peer0.org4.trace.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org4.trace.com
      - CORE_LOGGING_PEER=info
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org4MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org4.trace.com:7051
      # # 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=basic-network_basic
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
      # enabled TLS
 #     - CORE_PEER_TLS_ENABLED=true
 #     - 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

      #- CORE_PEER_GOSSIP_USELEADERELECTION=true
      #- CORE_PEER_GOSSIP_ORGLEADER=false
      #- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org4.trace.com:7051
      #- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org4.trace.com:7051
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    #command: peer node start --peer-chaincodedev=true
    ports:
      - 10051:7051
      - 10053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org4.trace.com/peers/peer0.org4.trace.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org4.trace.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
 #       - ./crypto-config/peerOrganizations/org4.trace.com/peers/peer0.org4.trace.com/msp:/etc/hyperledger/fabric/msp
 #       - ./crypto-config/peerOrganizations/org4.trace.com/peers/peer0.org4.trace.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org4.trace.com:/var/hyperledger/production
    depends_on:
      - orderer.trace.com
      - couchdb
    networks:
      - basic

  peer0.org5.trace.com:
    container_name: peer0.org5.trace.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org4.trace.com
      - CORE_LOGGING_PEER=info
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org5MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org5.trace.com:7051
      # # 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=basic-network_basic
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
      # enabled TLS
#      - CORE_PEER_TLS_ENABLED=true
#      - 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
 
      #- CORE_PEER_GOSSIP_USELEADERELECTION=true
      #- CORE_PEER_GOSSIP_ORGLEADER=false
      #- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org5.trace.com:7051
      #- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org5.trace.com:7051
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    #command: peer node start --peer-chaincodedev=true
    ports:
      - 11051:7051
      - 11053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org5.trace.com/peers/peer0.org5.trace.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org5.trace.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
#        - ./crypto-config/peerOrganizations/org5.trace.com/peers/peer0.org5.trace.com/msp:/etc/hyperledger/fabric/msp
#        - ./crypto-config/peerOrganizations/org5.trace.com/peers/peer0.org5.trace.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org5.trace.com:/var/hyperledger/production
    depends_on:
      - orderer.trace.com
      - couchdb
    networks:
      - basic


  couchdb:
    container_name: couchdb
    image: hyperledger/fabric-couchdb
    # Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
    # for CouchDB.  This will prevent CouchDB from operating in an "Admin Party" mode.
    environment:
      - COUCHDB_USER=
      - COUCHDB_PASSWORD=
    ports:
      - 5984:5984
    networks:
      - basic

  cli:
    container_name: cli
    image: hyperledger/fabric-tools
    tty: true
    stdin_open: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_LOGGING_LEVEL=info
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.trace.com/users/Admin@org1.trace.com/msp
      - CORE_CHAINCODE_KEEPALIVE=10
#      - CORE_PEER_TLS_ENABLED=true
#      - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/tls/server.crt
#      - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/tls/server.key
#      - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/tls/ca.crt
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: /bin/bash
    volumes:
        - /var/run/:/host/var/run/
        - ./../chaincode/:/opt/gopath/src/github.com/chaincode
        - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
        - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
        - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
    depends_on:
      - orderer.trace.com
      - peer0.org1.trace.com
      - peer0.org2.trace.com
      - peer0.org3.trace.com
      - peer0.org4.trace.com
      - peer0.org5.trace.com

    networks:
        - basic
    #depends_on:
    #  - orderer.example.com
    #  - peer0.org1.example.com
    #  - couchdb

3 问题解决记录

问题解决:

Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.trace.com:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp 172.22.0.7:7051: connect: connection refused"
Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.trace.com:7051: failed to create new connection: context deadline exceeded

解决方法:

sudo vi /etc/resolv.conf

按:wq!退出

4 启动区块链docker节点

4.1 常用docker命令

删除所有容器

# 1.停用全部运行中的容器:
docker stop $(docker ps -q)
# 2.删除全部容器:
docker rm $(docker ps -aq)

删除所有镜像

docker rmi -f $(docker images -qa)

删除数据卷

# 删除数据卷:
docker volume rm $(docker volume ls -q)
# 删除 network:
docker network rm $(docker network ls -q)

一键执行

docker stop $(docker ps -q)
docker rm $(docker ps -aq)
docker volume rm $(docker volume ls -q)
./start.sh

拉取对应版本镜像(包含区块链fabric1.4.7和1.2.0)

## 1) 基础镜像
docker pull hyperledger/fabric-peer:1.4.7
docker tag hyperledger/fabric-peer:1.4.7 hyperledger/fabric-peer:latest

docker pull hyperledger/fabric-orderer:1.4.7
docker tag hyperledger/fabric-orderer:1.4.7 hyperledger/fabric-orderer:latest

docker pull hyperledger/fabric-tools:1.4.7
docker tag hyperledger/fabric-tools:1.4.7 hyperledger/fabric-tools:latest

docker pull hyperledger/fabric-ccenv:1.4.7
docker tag hyperledger/fabric-ccenv:1.4.7 hyperledger/fabric-ccenv:latest

docker pull hyperledger/fabric-ca:1.4.7
docker tag hyperledger/fabric-ca:1.4.7 hyperledger/fabric-ca:latest

## 2) 数据库与消息队列镜像
docker pull hyperledger/fabric-baseos:0.4.15
docker tag hyperledger/fabric-baseos:0.4.15 hyperledger/fabric-baseos:latest

docker pull hyperledger/fabric-couchdb:0.4.15
docker tag hyperledger/fabric-couchdb:0.4.15 hyperledger/fabric-couchdb:latest

docker pull hyperledger/fabric-kafka:0.4.15
docker tag hyperledger/fabric-kafka:0.4.15 hyperledger/fabric-kafka:latest

docker pull hyperledger/fabric-zookeeper:0.4.15 
docker tag hyperledger/fabric-zookeeper:0.4.15 hyperledger/fabric-zookeeper:latest

## 3) Java语言包镜像(可选)
# docker pull hyperledger/fabric-javaenv:1.4.7
# docker tag hyperledger/fabric-javaenv:1.4.7 hyperledger/fabric-javaenv:latest

1.2 版本

## 1) 基础镜像
docker pull hyperledger/fabric-peer:1.2.0
docker tag hyperledger/fabric-peer:1.2.0 hyperledger/fabric-peer:latest

docker pull hyperledger/fabric-orderer:1.2.0
docker tag hyperledger/fabric-orderer:1.2.0 hyperledger/fabric-orderer:latest

docker pull hyperledger/fabric-tools:1.2.0
docker tag hyperledger/fabric-tools:1.2.0 hyperledger/fabric-tools:latest

docker pull hyperledger/fabric-ccenv:1.2.0
docker tag hyperledger/fabric-ccenv:1.2.0 hyperledger/fabric-ccenv:latest

docker pull hyperledger/fabric-ca:1.2.0
docker tag hyperledger/fabric-ca:1.2.0 hyperledger/fabric-ca:latest

## 2) 数据库与消息队列镜像
docker pull hyperledger/fabric-baseos:0.4.10
docker tag hyperledger/fabric-baseos:0.4.10 hyperledger/fabric-baseos:latest

docker pull hyperledger/fabric-couchdb:0.4.10
docker tag hyperledger/fabric-couchdb:0.4.10 hyperledger/fabric-couchdb:latest

docker pull hyperledger/fabric-kafka:0.4.10
docker tag hyperledger/fabric-kafka:0.4.10 hyperledger/fabric-kafka:latest

docker pull hyperledger/fabric-zookeeper:0.4.10 
docker tag hyperledger/fabric-zookeeper:0.4.10 hyperledger/fabric-zookeeper:latest

## 3) Java语言包镜像(可选)
# docker pull hyperledger/fabric-javaenv:1.2.0
# docker tag hyperledger/fabric-javaenv:1.2.0 hyperledger/fabric-javaenv:latest

5 fabric-node-sdk后台搭建

nodejs 后台搭建(在ubuntu 阿里云服务器)
安装nvm

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash

执行

source ~/.bashrc

下载node依赖

nvm install 12.13.1
rm -rf node_modules
rm -rf hfc-key-store
nvm use 12.13.1
npm install
nvm use 12.13.1
npm rebuild
npm install fabric-client
npm install fabric-ca-client
node enrollAdmin.js
node registerUser.js
npm install express
npm install body-parser
npm install influx

npm install forever -g
forever start app.js

访问http://xxx.xxx.xxx:5984/_utils/#查看区块链的couchdb

node sdk后台查看

docker节点查看

以上是关于hyperledger fabric 网络操作基本操作和概念的主要内容,如果未能解决你的问题,请参考以下文章

Hyperledger-fabric 手动操作第一次运行简单网络

HyperLedger Fabric实战:基础环境构建

如何使用Hyperledger Fabric和Composer构建区块链网络(上)

Hyperledger Fabric:在运行 raft 网络中添加 Orderer

区块链——Hyperledger Fabric2.2单点搭建网络

万字解析——区块链hyperledger fabric2.2部署实战教程