超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)

超级账本 fabric2.0 集群多机部署


首先按照确保已经安装好fabric2.0相关环境,且保证docker环境一致(先跑通first-network!!!)

搭建fabric2.0环境请参考
fabric2.0开发 基本环境安装配置
fabric2.0开发 部署fabric环境和fabric-samples的启动

我们使用 docker swarm 进行集群管理

组织架构

超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第1张图片

4台服务器 4个组织 8个peer

服务器分别为

192.168.1.188   (host1)

192.168.1.201   (host2)

192.168.1.202   (host3)

192.168.1.206   (host4)

其中每台服务器一个组织两个peer

部署过程

1.拉取docker swarm 镜像(分别在host1,host2,host3,host4拉取镜像)

docker pull swarm

2.host1执行命令(终端192.168.1.188)

docker swarm init --advertise-addr 192.168.1.188

执行截图

在这里插入图片描述

3.执行获取其他机器连接的manager命令(终端192.168.1.188)

docker swarm join-token manager

在这里插入图片描述

  • 执行此命令我们可以看到返回了一个加入集群的命令
  • 命令 docker swarm join --token SWMTKN-1-4jpo4z6xnd9jz2ne2blzoe8erzo28vbqja83m5qd01pqfd88p1-9ir3qsc5ctdqnlnlkizwqi6yy 192.168.1.188:2377

4.使用最后的输出,将其他节点以管理者身份加入swarm

  • 在host2,host3,host4分别执行刚刚返回的命令
  • host2执行:docker swarm join --token SWMTKN-1-4jpo4z6xnd9jz2ne2blzoe8erzo28vbqja83m5qd01pqfd88p1-9ir3qsc5ctdqnlnlkizwqi6yy 192.168.1.188:2377(终端192.168.1.201)
  • host3执行:docker swarm join --token SWMTKN-1-4jpo4z6xnd9jz2ne2blzoe8erzo28vbqja83m5qd01pqfd88p1-9ir3qsc5ctdqnlnlkizwqi6yy 192.168.1.188:2377(终端192.168.1.202)
  • host4执行:docker swarm join --token SWMTKN-1-4jpo4z6xnd9jz2ne2blzoe8erzo28vbqja83m5qd01pqfd88p1-9ir3qsc5ctdqnlnlkizwqi6yy 192.168.1.188:2377(终端192.168.1.206)

5.从host 1创建叠加网络first-network(终端192.168.1.188)

  • 执行命令如下

  • docker network create --attachable --driver overlay first-network

  • 使用docker network ls 查看我们创建的叠加网络
    超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第2张图片

  • 我们去查看其他其他三台主机

  • 主机192.168.1.201
    超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第3张图片

  • 主机192.168.1.202
    在这里插入图片描述

  • 主机192.168.1.206

在这里插入图片描述

6.在host1上准备材料(终端192.168.1.188)

  • 执行命令
    cd fabric-samples
    mkdir asset_network
    cd asset_network
  • 直接从first-network拷贝crypto-config.yaml和configtx.yaml文件
    cp …/first-network/crypto-config.yaml .
    cp …/first-network/configtx.yaml .
    mkdir scripts
    cp …/first-network/scripts/* ./scripts
  • 接下来我们开始修改用于生成MSP相关证书组织架构和节点数的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
      # ---------------------------------------------------------------------------
      # "Specs" - See PeerOrgs below for complete description
      # ---------------------------------------------------------------------------
      Specs:
        - Hostname: orderer
        - Hostname: orderer2
        - Hostname: orderer3
        - Hostname: orderer4
        - Hostname: orderer5
  
  # ---------------------------------------------------------------------------
  # "PeerOrgs" - Definition of organizations managing peer nodes
  # ---------------------------------------------------------------------------
  PeerOrgs:
    # ---------------------------------------------------------------------------
    # Org1
    # ---------------------------------------------------------------------------
    - Name: Org1
      Domain: org1.example.com
      EnableNodeOUs: true
      # ---------------------------------------------------------------------------
      # "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
      Template:
        Count: 2
      Users:
        Count: 1
  
    # ---------------------------------------------------------------------------
    # add org3
    # ---------------------------------------------------------------------------
    - Name: Org3
      Domain: org3.example.com
      EnableNodeOUs: true
      Template:
        Count: 2
      Users:
        Count: 1
  
    - Name: Org4
      Domain: org4.example.com
      EnableNodeOUs: true
      Template:
        Count: 2
      Users:
        Count: 1
  • 接下来我们开始修改用于生成创世区块,交易通道configtx.yaml配置文件(修改如下)

  • # Copyright IBM Corp. All Rights Reserved.
    #
    # 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/example.com/msp
    
            # Policies defines the set of policies at this level of the config tree
            # For organization policies, their canonical path is usually
            #   /Channel///
            Policies:
                Readers:
                    Type: Signature
                    Rule: "OR('OrdererMSP.member')"
                Writers:
                    Type: Signature
                    Rule: "OR('OrdererMSP.member')"
                Admins:
                    Type: Signature
                    Rule: "OR('OrdererMSP.admin')"
    
            OrdererEndpoints:
                - orderer.example.com:7050
    
        - &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.example.com/msp
    
            # Policies defines the set of policies at this level of the config tree
            # For organization policies, their canonical path is usually
            #   /Channel///
            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')"
                Endorsement:
                    Type: Signature
                    Rule: "OR('Org1MSP.peer')"
    
            # leave this flag set to true.
            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.example.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.example.com/msp
    
            # Policies defines the set of policies at this level of the config tree
            # For organization policies, their canonical path is usually
            #   /Channel///
            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')"
                Endorsement:
                    Type: Signature
                    Rule: "OR('Org2MSP.peer')"
    
            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.example.com
                  Port: 7050
    
        - &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.example.com/msp
    
            # Policies defines the set of policies at this level of the config tree
            # For organization policies, their canonical path is usually
            #   /Channel///
            Policies:
                Readers:
                    Type: Signature
                    Rule: "OR('Org3MSP.admin', 'Org3MSP.peer', 'Org3MSP.client')"
                Writers:
                    Type: Signature
                    Rule: "OR('Org3MSP.admin', 'Org3MSP.client')"
                Admins:
                    Type: Signature
                    Rule: "OR('Org3MSP.admin')"
                Endorsement:
                    Type: Signature
                    Rule: "OR('Org3MSP.peer')"
    
            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.example.com
                  Port: 7050
    
        - &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.example.com/msp
    
            # Policies defines the set of policies at this level of the config tree
            # For organization policies, their canonical path is usually
            #   /Channel///
            Policies:
                Readers:
                    Type: Signature
                    Rule: "OR('Org4MSP.admin', 'Org4MSP.peer', 'Org4MSP.client')"
                Writers:
                    Type: Signature
                    Rule: "OR('Org4MSP.admin', 'Org4MSP.client')"
                Admins:
                    Type: Signature
                    Rule: "OR('Org4MSP.admin')"
                Endorsement:
                    Type: Signature
                    Rule: "OR('Org4MSP.peer')"
    
            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.example.com
                  Port: 7050
            
    
    ################################################################################
    #
    #   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.
        Channel: &ChannelCapabilities
            # V2_0 capability ensures that orderers and peers behave according
            # to v2.0 channel capabilities. Orderers and peers from
            # prior releases would behave in an incompatible way, and are therefore
            # not able to participate in channels at v2.0 capability.
            # Prior to enabling V2.0 channel capabilities, ensure that all
            # orderers and peers on a channel are at v2.0.0 or later.
            V2_0: true
    
        # Orderer capabilities apply only to the orderers, and may be safely
        # used with prior release peers.
        # Set the value of the capability to true to require it.
        Orderer: &OrdererCapabilities
            # V2_0 orderer capability ensures that orderers behave according
            # to v2.0 orderer capabilities. Orderers from
            # prior releases would behave in an incompatible way, and are therefore
            # not able to participate in channels at v2.0 orderer capability.
            # Prior to enabling V2.0 orderer capabilities, ensure that all
            # orderers on channel are at v2.0.0 or later.
            V2_0: true
    
        # Application capabilities apply only to the peer network, and may be safely
        # used with prior release orderers.
        # Set the value of the capability to true to require it.
        Application: &ApplicationCapabilities
            # V2_0 application capability ensures that peers behave according
            # to v2.0 application capabilities. Peers from
            # prior releases would behave in an incompatible way, and are therefore
            # not able to participate in channels at v2.0 application capability.
            # Prior to enabling V2.0 application capabilities, ensure that all
            # peers on channel are at v2.0.0 or later.
            V2_0: 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:
    
        # Policies defines the set of policies at this level of the config tree
        # For Application policies, their canonical path is
        #   /Channel/Application/
        Policies:
            Readers:
                Type: ImplicitMeta
                Rule: "ANY Readers"
            Writers:
                Type: ImplicitMeta
                Rule: "ANY Writers"
            Admins:
                Type: ImplicitMeta
                Rule: "MAJORITY Admins"
            LifecycleEndorsement:
                Type: ImplicitMeta
                Rule: "MAJORITY Endorsement"
            Endorsement:
                Type: ImplicitMeta
                Rule: "MAJORITY Endorsement"
    
        Capabilities:
            <<: *ApplicationCapabilities
    ################################################################################
    #
    #   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
        OrdererType: etcdraft
    
        # 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
    
        # Organizations is the list of orgs which are defined as participants on
        # the orderer side of the network
        Organizations:
    
        # Policies defines the set of policies at this level of the config tree
        # For Orderer policies, their canonical path is
        #   /Channel/Orderer/
        Policies:
            Readers:
                Type: ImplicitMeta
                Rule: "ANY Readers"
            Writers:
                Type: ImplicitMeta
                Rule: "ANY Writers"
            Admins:
                Type: ImplicitMeta
                Rule: "MAJORITY Admins"
            # BlockValidation specifies what signatures must be included in the block
            # from the orderer for the peer to validate it.
            BlockValidation:
                Type: ImplicitMeta
                Rule: "ANY Writers"
    
    ################################################################################
    #
    #   CHANNEL
    #
    #   This section defines the values to encode into a config transaction or
    #   genesis block for channel related parameters.
    #
    ################################################################################
    Channel: &ChannelDefaults
        # Policies defines the set of policies at this level of the config tree
        # For Channel policies, their canonical path is
        #   /Channel/
        Policies:
            # Who may invoke the 'Deliver' API
            Readers:
                Type: ImplicitMeta
                Rule: "ANY Readers"
            # Who may invoke the 'Broadcast' API
            Writers:
                Type: ImplicitMeta
                Rule: "ANY Writers"
            # By default, who may modify elements at this config level
            Admins:
                Type: ImplicitMeta
                Rule: "MAJORITY Admins"
    
        # Capabilities describes the channel level capabilities, see the
        # dedicated Capabilities section elsewhere in this file for a full
        # description
        Capabilities:
            <<: *ChannelCapabilities
    
    ################################################################################
    #
    #   Profile
    #
    #   - Different configuration profiles may be encoded here to be specified
    #   as parameters to the configtxgen tool
    #
    ################################################################################
    Profiles:
    
        FourOrgsChannel:
            Consortium: SampleConsortium
            <<: *ChannelDefaults
            Application:
                <<: *ApplicationDefaults
                Organizations:
                    - *Org1
                    - *Org2
                    - *Org3
                    - *Org4
                Capabilities:
                    <<: *ApplicationCapabilities
    
        SampleMultiNodeEtcdRaft:
            <<: *ChannelDefaults
            Capabilities:
                <<: *ChannelCapabilities
            Orderer:
                <<: *OrdererDefaults
                OrdererType: etcdraft
                EtcdRaft:
                    Consenters:
                    - Host: orderer.example.com
                      Port: 7050
                      ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
                      ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer.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
                    - Host: orderer3.example.com
                      Port: 7050
                      ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer3.example.com/tls/server.crt
                      ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer3.example.com/tls/server.crt
                    - Host: orderer4.example.com
                      Port: 7050
                      ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer4.example.com/tls/server.crt
                      ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer4.example.com/tls/server.crt
                    - Host: orderer5.example.com
                      Port: 8050
                      ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer5.example.com/tls/server.crt
                      ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer5.example.com/tls/server.crt
                Addresses:
                    - orderer.example.com:7050
                    - orderer2.example.com:7050
                    - orderer3.example.com:7050
                    - orderer4.example.com:7050
                    - orderer5.example.com:8050
    
                Organizations:
                - *OrdererOrg
                Capabilities:
                    <<: *OrdererCapabilities
            Application:
                <<: *ApplicationDefaults
                Organizations:
                - <<: *OrdererOrg
            Consortiums:
                SampleConsortium:
                    Organizations:
                    - *Org1
                    - *Org2
                    - *Org3
                    - *Org4
    
  • 然后根据配置文件生成必要的密码学资料

  • ../bin/cryptogen generate --config=./crypto-config.yaml
    
    export FABRIC_CFG_PATH=$PWD
    
    mkdir channel-artifacts
    
    ../bin/configtxgen -profile SampleMultiNodeEtcdRaft -outputBlock ./channel-artifacts/genesis.block -channelID byfn-sys-channel
    
    ../bin/configtxgen -profile FourOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
    
    ../bin/configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
    
    ../bin/configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
    
    ../bin/configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID mychannel -asOrg Org3MSP
    
    ../bin/configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org4MSPanchors.tx -channelID mychannel -asOrg Org4MSP
    

7.为所有主机准备docker-compose文件,需要创建6个docker-compose文件以及一个env文件(终端192.168.1.188)

文件名 解释
base/peer-base.yaml 生成peer节点的基础配置文件
base/docker-compose-peer.yaml 各org,peer,order的docker部署配置文件
host1.yaml 主机192.168.1.188启动服务的配置文件
host2.yaml 主机192.168.1.201启动服务的配置文件
host3.yaml 主机192.168.1.202启动服务的配置文件
host4.yaml 主机192.168.1.206启动服务的配置文件
.env 配置文件
  • 针对相关文件的修改
    • base/peer-base.yaml中,CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE修改为
      我们之前创建的叠加网络first-network
    • base/docker-compose-base.yaml中,由于我们添加了两个组织,且每个组织都在不同的机器上,一方面修改了映射的端口,一方面添加了对应的两个组织和组织对应的peer。
    • 在所有的hostn.yaml文件中,我们添加叠加网络first-network
      现在我们在host 1上准备好了所有资料,将该目录拷贝到其他主机。由于不能跨EC2实例拷贝文件,我们使用本地机器进行桥接操作,将准备好的文件分别发送到其他3台主机上去:

8.将我们创建及生成的文件打包(终端192.168.1.188)

cd ..
tar cf asset_network.tar asset_network
scp asset_network.tar [email protected]:/root/go/src/github.com/hyperledger/fabric-samples
scp asset_network.tar [email protected]:/root/go/src/github.com/hyperledger/fabric-samples
scp asset_network.tar [email protected]:/root/go/src/github.com/hyperledger/fabric-samples

9.分别登陆刚刚远程复制的机器进行解压包

#在host2、host3、host4上分别执行
cd /root/go/src/github.com/hyperledger/fabric-samples
rm -rf asset_network
tar xf asset_network.tar
cd asset_network

现在所有的节点都有了同样的密码学资料和docker-compose文件,我们可以启动容器了。

10.分别在各个主机上使用docker-composer启动应用程序

  • host2 (终端192.168.1.188)
    docker-compose -f host1.yaml up -d
    使用docker ps查看服务
    在这里插入图片描述

  • host2 (终端192.168.1.201)
    docker-compose -f host2.yaml up -d
    使用docker ps查看服务
    在这里插入图片描述

  • host3 (终端192.168.1.202)
    docker-compose -f host3.yaml up -d
    使用docker ps查看服务
    在这里插入图片描述

  • host4 (终端192.168.1.206)
    docker-compose -f host4.yaml up -d
    使用docker ps查看服务

在这里插入图片描述

如下为 docker-compose -f host1.yaml up -d 命令返回截图

超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第4张图片

11.为mychannel通道创建创世区块(终端192.168.1.188)

docker exec cli peer channel create -o orderer.example.com:7050 -c mychannel \
       -f ./channel-artifacts/channel.tx --tls true \
       --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

结果如下图

超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第5张图片

12.将peer0.org1加入mychannel(终端192.168.1.188)

docker exec cli peer channel join -b mychannel.block

在这里插入图片描述

13.将peer1.org1加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_ADDRESS=peer1.org1.example.com:8051 -e 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 cli peer channel join -b mychannel.block

在这里插入图片描述

14.将peer0.org2加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp \
       -e CORE_PEER_ADDRESS=peer0.org2.example.com:7051 -e CORE_PEER_LOCALMSPID="Org2MSP" \
       -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
       cli peer channel join -b mychannel.block

15.将peer1.org2加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp \
       -e CORE_PEER_ADDRESS=peer1.org2.example.com:8051 -e CORE_PEER_LOCALMSPID="Org2MSP" \
       -e 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 \
       cli peer channel join -b mychannel.block

在这里插入图片描述
16.将peer0.org3加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp \
       -e CORE_PEER_ADDRESS=peer0.org3.example.com:7051 -e CORE_PEER_LOCALMSPID="Org3MSP" \
       -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt \
       cli peer channel join -b mychannel.block

在这里插入图片描述

17.将peer1.org3加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/[email protected]/msp \
       -e CORE_PEER_ADDRESS=peer1.org3.example.com:8051 -e CORE_PEER_LOCALMSPID="Org3MSP" \
       -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer1.org3.example.com/tls/ca.crt \
       cli peer channel join -b mychannel.block

在这里插入图片描述
18.将peer0.org4加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/users/[email protected]/msp \
       -e CORE_PEER_ADDRESS=peer0.org4.example.com:7051 -e CORE_PEER_LOCALMSPID="Org4MSP" \
       -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt \
       cli peer channel join -b mychannel.block

在这里插入图片描述

19.将peer1.org4加入mychannel(终端192.168.1.188)

docker exec -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/users/[email protected]/msp \
       -e CORE_PEER_ADDRESS=peer1.org4.example.com:8051 -e CORE_PEER_LOCALMSPID="Org4MSP" \
       -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer1.org4.example.com/tls/ca.crt \
       cli peer channel join -b mychannel.block

在这里插入图片描述

到此我们已经啊将所有组织和peer加入到了我们的channel通道中

20.安装并实例化Fabcar链码(终端192.168.1.188)

  • 进入到docker的cli终端
    docker exec -it cli bash

  • 查看环境变量
    env
    超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第6张图片

  • 设置go 的访问源
    go env -w GOPROXY=https://goproxy.io,direct

  • 开始打包chaincode

  • peer lifecycle chaincode package mycc1.tar.gz --path /opt/gopath/src/github.com/chaincode/abstore/go/ --lang golang --label mycc_1
    

    命令解释:
    mycc2.tar.gz :打包合约包文件名
    –path 智能合约路径,可以在host1.yaml中查看cli容器的数据卷配置查询
    –lang 智能合约语言 支持golang、node、java
    –label 智能合约标签,描述作用

  • 开始安装chaincode
    peer lifecycle chaincode install mycc1.tar.gz

  • 验证合约安装是否安装到节点
    peer lifecycle chaincode queryinstalled
    出现如下结果表示安装成功

在这里插入图片描述

  • 当前组织同意合约定义(注意下面命令使用的mycc_1对应的包id应该是使用刚刚验证时返回的包id!!!)

  • peer lifecycle chaincode approveformyorg --tls true \
     --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
     --channelID mychannel --name mycc1 --version 1 --init-required --package-id mycc_1:9098a2442d1c40119cb0a085a206d89e61859649ebcca587357e2b56a3b3fe73 --sequence 1 --waitForEvent
    

    在这里插入图片描述

    • 命令解释:
      –tls 是否启动tls
      –ca ca证书路径
      –channelID 智能合约安装通道
      –name 合约名
      –version 合约版本
      –package-id queryinstalled查询的合约ID
      –sequence 序列号
      –waitForEvent 等待peer提交交易返回
      –init-required 合约是否必须执行init
  • 检查合约是否满足策略

  • peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc1  --version 1 --sequence 1 --output json --init-required
    

    在这里插入图片描述

    • 通过检查合约策略如上图,我们可知Org1MSP 已经通过策略,但由于我们的策略定义是满足过半数
      超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第7张图片

    • 现在明显不满足,我们还是执行commit合约看一下会怎么样

    • peer lifecycle chaincode commit -o orderer.example.com:7050 \
       --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
       --channelID mychannel --name mycc1 --peerAddresses peer0.org1.example.com:7051 \
       --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
       --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
      --peerAddresses peer0.org3.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt \
      --peerAddresses peer0.org4.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt \
       --version 1 --sequence 1 --init-required
      

      超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第8张图片

    • chaincode definition not agreed to by this org (Org2MSP) 链码合约定义还没取得Org2MSP的同意,这时候我们需要把Org2MSP,Org3MSP,Org4MSP也同意这个合约定义

  • Org2MSP 同意合约

    • 从新定义环境变量

    • export CORE_PEER_LOCALMSPID=Org2MSP
      export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
      export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
      export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      
    • 从新安装合约到节点
      peer lifecycle chaincode install mycc1.tar.gz
      在这里插入图片描述

    • 验证合约安装是否安装到节点
      peer lifecycle chaincode queryinstalled

    • 当前组织同意合约定义(注意下面命令使用的mycc_1对应的包id应该是使用刚刚验证时返回的包id!!!)

    • peer lifecycle chaincode approveformyorg --tls true \
       --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
       --channelID mychannel --name mycc1 --version 1 --init-required --package-id mycc_1:9098a2442d1c40119cb0a085a206d89e61859649ebcca587357e2b56a3b3fe73 --sequence 1 --waitForEvent
      
    • 检查合约是否满足策略

    • peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc1  --version 1 --sequence 1 --output json --init-required
      

      在这里插入图片描述

      此时我们可以看到,现在Org2MSP也已经显示为true,组织二也满足了策略
      后面请按照走的程序,在将Org3MSP,Org4MSP再走一遍上述流程,使Org3MSP,Org4MSP也同意策略

      最终所有组织都同意合约返回入下图所以

  • 在满足合约定义的策略后,提交合约

  • peer lifecycle chaincode commit -o orderer.example.com:7050 \
     --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
     --channelID mychannel --name mycc1 --peerAddresses peer0.org1.example.com:7051 \
     --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
     --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
    --peerAddresses peer0.org3.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt \
    --peerAddresses peer0.org4.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt \
     --version 1 --sequence 1 --init-required
    

    返回如下图
    超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第9张图片

    参数解释
    –tls 是否启动tls
    –ca ca证书路径
    –channelID 智能合约安装通道
    –name 合约名
    –version 合约版本
    –package-id queryinstalled查询的合约ID
    –sequence 序列号
    –waitForEvent 等待peer提交交易返回
    –init-required 合约是否必须执行init
    –peerAddresses 节点路径
    –tlsRootCertFiles 节点ca根证书路径(–peerAddresses --tlsRootCertFiles 连用,可多个节点,多个节点即将合约部署到对应节点集合上)
    在宿主机(192.168.1.201)中查看智能合约容器,此时合约容器已经启动,其他主机请自行查看
    超级账本 fabric2.0 集群多机部署--docker swarm集群(4个组织8个peer)_第10张图片

  • 查看节点已提交合约

  • peer lifecycle chaincode querycommitted --channelID mychannel --name mycc1
    

    在这里插入图片描述

  • 操作合约

  • peer chaincode invoke -o orderer.example.com:7050 --tls true \
    --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
    -C mychannel -n mycc1 --peerAddresses peer0.org1.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
    --peerAddresses peer0.org2.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
    --peerAddresses peer0.org3.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt \
    --peerAddresses peer0.org4.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt \
    --isInit -c '{"Args":["Init","a","100","b","100"]}'
    

    如下图我们可以看到我们成功初始化合约
    在这里插入图片描述

  • 查询
    peer chaincode query -C mychannel -n mycc1 -c ‘{“Args”:[“query”,“a”]}’
    如下图我们可以看到查询到我们需要的数据。a账户余额为100
    在这里插入图片描述

  • 调用 invoke方法, a ->b 转账20

  • peer chaincode invoke -o orderer.example.com:7050 --tls true \
    --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
    -C mychannel -n mycc1 --peerAddresses peer0.org1.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
    --peerAddresses peer0.org2.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt  \
    --peerAddresses peer0.org3.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt  \
    --peerAddresses peer0.org4.example.com:7051 \
    --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt  \
    -c  '{"Args":["invoke","a","b","20"]}'
    

    如下图我们可以看到已经成功给b转账20
    在这里插入图片描述

  • 我们再次查询a的余额

  • peer chaincode query -C mychannel -n mycc1 -c '{"Args":["query","a"]}'
    

    在这里插入图片描述

    我们可以看到a的余额已经减少20为80,到此我们成功多机部署了fabric环境。

你可能感兴趣的:(超级账本,docker,fabric,chaincode,fabric2.0,多机部署)