联盟链 Hyperledger Fabric v1.4.4 四台分机搭建1orderer+3peer

公司需要进行环境测试,拿了 4 台超强劲的服务器来搭建区块链网络,目前已搭建完成且经过了 Caliper 压测,TPS 结果惊人,在这里分享下多机搭建的过程。

一、环境概述

主机 IP(就用 4 个字母来表示啦)
orderer UUUU
peer0 AAAA
peer1 BBBB
peer2 CCCC

二、环境准备

1. 部署 fabric

具体部署过程在我之前的一篇文章中有详细讲述哈,主要是保证 4 机子都能正常运转 byfn 网络后方可进行下一步。> 从零搭建起 byfn 网络 <

2. 修改 /etc/hosts

【配置】为了能让其通过节点名称就能访问其他节点网络,需要在 4 台物理机上先配置 hosts 文件,分别在文件后面加上如下配置。

> 在 orderer 的主机上:无需添加
> 在 peer0 的主机上:
	UUUU orderer.example.com
	AAAA peer0.org1.example.com
> 在 peer1 的主机上:
	UUUU orderer.example.com
	BBBB peer1.org1.example.com
> 在 peer2 的主机上:
	UUUU orderer.example.com
	CCCC peer2.org1.example.com

【检测】让 3 个 peer 的物理机节点各自去 ping 那个 orderer 的域名和自己的域名,能 ping 通这一步就 OK 啦

> 比如说在 peer0 节点上,如下所示就是能 ping 通了

[root@peer0 ~]$ ping orderer.example.com
PING orderer.example.com (UUUU) 
64 bytes from orderer.example.com (AAAA): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from orderer.example.com (AAAA): icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from orderer.example.com (AAAA): icmp_seq=3 ttl=64 time=0.023 ms

[root@peer0 ~]$ ping peer0.org1.example.com
PING peer2.org1.example.com (UUUU) 
64 bytes from peer0.org1.example.com (AAAA): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from peer0.org1.example.com (AAAA): icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from peer0.org1.example.com (AAAA): icmp_seq=3 ttl=64 time=0.023 ms

三、配置文件

注意注意:在这一部分我是先用本地的 1 台虚拟机将所有配置文件生成并编辑好后,变成压缩包再传到 4 台云服务器里的。

1. 构建项目框架

创建一个名为 1orderer3peer 的文件夹,并从 fabric-samples/first-network 内将本次所需要的文件复制了进来,当前文件夹结构如下所示

> 当前路径:/root/1orderer3peer

1orderer3peer
├── base
│   ├── docker-compose-base.yaml
│   └── peer-base.yaml
├── chaincode						(新创建的文件夹,用来存放链码)
│   └── chaincode_example02.go		(复制了 fabric-samples/chaincode/chaincode_example02/go 里的链码)
├── channel-artifacts				(新创建的文件夹)
│ 
├── configtx.yaml
├── crypto-config.yaml
├── docker-compose-cli.yaml
└── docker-compose-orderer.yaml		(复制上面 cli 的文件后改名的,稍后进行编辑)

2. 编辑 crypto-config.yaml 并生成证书密钥文件

【编辑】将文件修改为 1 组织 3 节点的内容即可,完整文件如下所示:

OrdererOrgs:
  - Name: Orderer			
    Domain: example.com		(根域名)
    EnableNodeOUs: true		(是否支持 node.js)
    Specs:
      - Hostname: orderer	(到时候访问这台 orderer 的域名就是:orderer.example.com)

PeerOrgs:
  - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true
    Template:
      Count: 3		(设置 3 个 peer 节点)
    Users:
      Count: 1		(除了 Admin 之外还安排 1 个 User 用户)

【生成】使用 cryptogen 命令指定上述文件来生成证书密钥文件,生成结果将存放在在新生成的 crypto-config 文件夹内。

> 当前路径:/root/1orderer3peer
[root@localhost 1orderer3peer]# cryptogen generate --config=./crypto-config.yaml
org1.example.com
> 当前目录结构下应该会有 crypto-config 的文件夹出现
1orderer3peer
├── ......
├── ......
├── crypto-config				<<----(这一步就生成了这个文件夹)
│   ├── ordererOrganizations	(1 个 orderer 对应一个文件夹)
│   └── peerOrganizations		(1 个组织就对应一个文件夹)
├── ......

3. 编辑 configtx.yaml 并生成

【编辑】这个文件主要进行的 3 处改动

> 1:删除了 Organizations 下 &Org2 的内容,因为只有一个组织,所以就留 &Org1 就好啦~

> 2: 修改了 Profiles 下的 TwoOrgsOrdererGenesis,修改后如下所示:
	SingleOrgsOrdererGenesis:			<<----(这里原来是 TwoOrgsOrdererGenesis,改了首单词)
        <<: *ChannelDefaults
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    		<<----(这里原来还有个 - *Org2,因为只有一个组织所以去掉了)
                    				
> 3: 修改了 Profiles 下的 TwoOrgsChannel,修改后如下所示:
    SingleOrgsChannel:					<<----(这里原来是 TwoOrgsChannel,改了首单词)
        Consortium: SampleConsortium
        <<: *ChannelDefaults
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                		<<----(同样本来也有 - *Org2,原因同上删掉了)
            Capabilities:
                <<: *ApplicationCapabilities

【生成】使用 configtxgen 命令,指定上面的 profiles 里的名称,生成创世块、channel 文件与锚节点文件

> 创建创世块文件(坑:创世块的 -channelID 名字不可以和后面的一样,要独立)
[root@localhost 1orderer3peer]# configtxgen -profile SingleOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block

> 创建通道 channel 文件
[root@localhost 1orderer3peer]# configtxgen -profile SingleOrgsChannel -channelID mychannel -outputCreateChannelTx ./channel-artifacts/channel.tx

> 创建锚节点文件(一个组织对应一个锚节点)
[root@localhost 1orderer3peer]# configtxgen -profile SingleOrgsChannel -channelID mychannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -asOrg Org1MSP
> 生成完毕后,在本目录结构下会生成如下文件:
1orderer3peer
├── ......
├── channel-artifacts
│   ├── genesis.block			(创世块)	
│   ├── channel.tx				(通道)
│   └── Org1MSPanchors.tx		(锚节点)
├── ......

4. 编辑 docker-compose-orderer.yaml 文件

【编辑】这个 docker-compose-orderer.yaml 文件,就好比在 orderer 主机上的启动节点的钥匙,那么另外 3 个节点就有 3 个这样内容不同的文件。由于文件是直接复制来的,经编辑删减后变成如下样子.

version: '2'

volumes:
  orderer.example.com:			(节点存储卷,我们要配的主机上只有 1 个 orderer,所以就指定这个就好)

networks:
  byfn:

services:
  orderer.example.com:
    extends:		(意思是继承了 base 文件夹下的 docker-compose-base.yaml 文件里的 orderer.example.com 内容,稍后还需要去修改这个)
      file:   base/docker-compose-base.yaml		
      service: orderer.example.com
    container_name: orderer.example.com
    networks:
      - byfn

5. 编辑 docker-compose-cli.yaml 文件

【编辑】由于有 3 台机子,就不麻烦整 3 个 cli 文件出来啦,这里直接针对 peer0 进行修改。之后文件传到每个主机上去后在 peer1 和 peer2 上再做修改。针对 peer0 修改后的 yaml 完整内容如下所示:

version: '2'

volumes:
  peer0.org1.example.com:

networks:
  byfn:

services:

  peer0.org1.example.com:		#(比如要启动 peer1,就修改成 peer1.org1.example.com)
    container_name: peer0.org1.example.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer0.org1.example.com
    networks:
      - byfn
    extra_hosts:				#(需要指定要连接的 orderer 的 IP 地址)
      - "orderer.example.com:UUUU"

  cli:					#(用于连接到 peer 上的辅助客户端)
    container_name: cli
    image: hyperledger/fabric-tools:$IMAGE_TAG
    tty: true
    stdin_open: true
    environment:
      - SYS_CHANNEL=$SYS_CHANNEL
      - GOPATH=/opt/gopath				#(启动 cli 容器后,go 的工作目录)
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      #- FABRIC_LOGGING_SPEC=DEBUG
      - FABRIC_LOGGING_SPEC=INFO		#(日志级别,若启动过程中出问题不知道错误在哪,可以调成 DEBUG 去观察;若对于压测就不要调成 DEBUG 啦)
      - CORE_PEER_ID=cli				#(当前 cli 节点的 ID,随意指定)
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051		#(当前 cli 要连接的 peer 节点地址,若是要连接 peer2 的话,就要改成 peer2.org1.example.com:7051 啦)
      - CORE_PEER_LOCALMSPID=Org1MSP	#(当前 cli 要连接的 peer 节点的组织 ID)
      - CORE_PEER_TLS_ENABLED=true		#(是否开启 tls,若开启了就要指定下面的证书文件啦)
      - 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/[email protected]/msp	#(设置要连接组织的 msp 路径)
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer		#(连接上 peer 后自动进入的工作目录)
    command: /bin/bash		#(连接后执行的命令)
    volumes:				#(存储卷,在这里把证书文件、链码、创世块文件等映射到 docker 内,这样连接后在内部就也能使用了)
        - /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		#(创世块、channel、锚节点位置)
    depends_on:
      - peer0.org1.example.com		#(意为在这个 peer0 启动完之后,再启动 cli )
    networks:
      - byfn
    extra_hosts:		#(要告诉 cli 排序节点 orderer 和当前 peer0 的网络地址,否则连接后在  docker 内部会不认得的)
      - "orderer.example.com:UUUU"
      - "peer0.org1.example.com:AAAA"

6. 编辑 base 文件夹内 2 个文件

【编辑 docker-compose-base.yaml】先前说道上面的客户端文件都继承了这里的文件,自然这里也有些需要我们去改动啦~下面就先贴出修改后的完整内容,再在里面进行说明。
文件内主要划分 4 个部分:1 个 orderer 和 3 个 peer 的配置

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2'

services:



  orderer.example.com:
    container_name: orderer.example.com
    extends:			#(意思是继承了同文件夹下另一个文件 peer-base.yaml 内 orderer-base 的配置,也是稍后要去做修改滴)
      file: peer-base.yaml
      service: orderer-base
    volumes:			#(挂载卷,上面提到过啦~)
        - ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
        - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
        - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
        - orderer.example.com:/var/hyperledger/production/orderer
    ports:
      - 7050:7050		#(指定启动端口为7050,这样带冒号格式的意思为【外部端口】映射到【内部端口】)



  peer0.org1.example.com:		#(针对第一台 peer 分机的配置)
    container_name: peer0.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org1.example.com				#(当前 peer 节点 ID)
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051	#(当前 peer 节点地址,由于是多机不存在端口冲突,所以 3 台 peer 的端口都设置为 7051)
      - CORE_PEER_LISTENADDRESS=0.0.0.0:7051			#(监听地址)
      - CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051			#(启动的时候连接谁,这里填当前节点就好)
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051	#(为了被其他节点发现到自己,需要填写其他节点的地址,这里就填写 peer1 的地址啦)
      - CORE_PEER_LOCALMSPID=Org1MSP
    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


#(往后的两个节点就差不多的配置了,注意下节点感知就好)
  peer1.org1.example.com:
    container_name: peer1.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer1.org1.example.com
      - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:7051
      - CORE_PEER_CHAINCODEADDRESS=peer1.org1.example.com:7052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer2.org1.example.com:7051		#(让 peer2 来感知到自己)
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    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


  peer2.org1.example.com:
    container_name: peer2.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer2.org1.example.com
      - CORE_PEER_ADDRESS=peer2.org1.example.com:7051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:7051
      - CORE_PEER_CHAINCODEADDRESS=peer2.org1.example.com:7052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051		#(让 peer0 来感知到自己)
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer2.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    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

【编辑 peer-compose.yaml】在这里就到了文件继承的最底端啦,主要是进行了两种配置:peer 类型和 orderer 类型,以下是当前文件的全部编辑后内容:

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2'

services:



  peer-base:
    image: hyperledger/fabric-peer:$IMAGE_TAG
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock		#(本地套接字地址,不用修改)
      # the following setting starts chaincode containers on the same
      # bridge network as the peers
      # https://docs.docker.com/compose/networking/
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_byfn
      - FABRIC_LOGGING_SPEC=INFO		#(通知级别,上面有提到过)
      #- FABRIC_LOGGING_SPEC=DEBUG
      - CORE_PEER_GOSSIP_USELEADERELECTION=true		#(是否进行自动选举机制)
      - CORE_PEER_GOSSIP_ORGLEADER=false			#(是否固定为 leader 节点,与上面一条设置相反)
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true			#(是否开启 tle,开启了话就要进行下面的设置)
      - 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




  orderer-base:
    image: hyperledger/fabric-orderer:$IMAGE_TAG
    environment:
      #- FABRIC_LOGGING_SPEC=DEBUG
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file		#(创世块的来源,这里我们指定是来源于文件)
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block		#(创世块文件在 docker 内部映射的地址路径)
      - 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

7. 打包

至此配置文件部分已经结束,当前的项目文件结构应该是如下所示的:

1orderer3peer
├── base
│   ├── docker-compose-base.yaml
│   └── peer-base.yaml
├── chaincode
│   └── chaincode_example02.go
├── channel-artifacts
│   ├── channel.tx
│   ├── genesis.block
│   └── Org1MSPanchors.tx
├── configtx.yaml
├── crypto-config
│   ├── ordererOrganizations
│   └── peerOrganizations
├── crypto-config.yaml
├── docker-compose-cli.yaml			(开启 peer 的文件)
└── docker-compose-orderer.yaml		(开启 orderer 的文件)

接下载就把整个文件夹压缩成 tar.gz 格式,再分别放入 4 台云物理机上。

> 当前路径:/root
[root@localhost ~]# tar -cvf 1orderer3peer.tar.gz 1orderer3peer/

我这里放入云物理机的方式是,用 XShell 连接后直接把压缩包拖进去的哈,当然大家也可以用 scp 等其他方式上传进去。

四、分机启动网络

接下来的舞台,就从本地的虚拟机来到了云物理机上了。

1. 启动 orderer

【解压】先进入 orderer 的物理机,将传进来的 1orderer3peer.tar.gz 包进行解压,解压完后进入该文件夹

> 当前机器:orderer 主机
> 当前路径:/root

tar -zxvf 1orderer3peer.tar.gz 
cd 1orderer3peer/

【启动】由于再本地虚拟机时就已经编写好了 orderer 的启动文件,所以直接启动即可,显示下面的 done 就意味已经成功。
命令参数 -f:指定启动的文件,就之前说的钥匙。
命令参数 up:启动,相反关闭的话就是 down。
命令参数 -d:daemon,守护进程方式执行,意思就是后台执行。不指定这个参数的话当前窗口就会实时展示日志。

> 当前路径:/root/1orderer3peer

[root@localhost 1orderer3peer]# docker-compose -f docker-compose-orderer.yaml up -d
Creating network "net_byfn" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating orderer.example.com ... done

【检测】可以使用 ps 的命令显示当前节点的启动状态,若状态显示为 Up 就表示正常。

[root@localhost 1orderer3peer]# docker-compose -f docker-compose-orderer.yaml ps
       Name           Command   State           Ports         
--------------------------------------------------------------
orderer.example.com   orderer   Up      0.0.0.0:7050->7050/tcp

【查错】使用 docker logs 加上上面命令显示出的名称,就会打印出内部的运行日志信息。若在搭建过程中出了什么问题就可以通过这种方式来进行查看。

[root@localhost 1orderer3peer]# docker logs orderer.example.com

至此分机 orderer 搭建完成。

2. 启动 peer

由于接下来 3 台分机的操作一致,这里就拿其中的 peer0 来讲述,剩下两台就只讲述操作不同的地方。

peer0 主机

【解压】同样的,将压缩包传进来并解压后进入文件夹

> 当前机器:peer0 主机
> 当前路径:/root

tar -zxvf 1orderer3peer.tar.gz 
cd 1orderer3peer/

【启动】由于之前配置阶段就是以 peer0 为基础构造的 cli 文件,所以可以直接启动。若正常显示下列 done 提示则表示启动成功。
(若在 peer1 和 peer2 主机上,在这一步就需要将 cli 内的 peer 信息均替换成对应节点信息)

> 当前路径:/root/1orderer3peer

[root@localhost 1orderer3peer]# docker-compose -f docker-compose-cli.yaml up -d
Creating volume "net_peer0.org1.example.com" with default driver
WARNING: Found orphan containers (orderer.example.com) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating peer0.org1.example.com ... done
Creating cli                    ... done

【进入 cli 客户端】使用下列命令进入连接到 peer0 节点,开始区块链内操作

[root@localhost 1orderer3peer]# docker exec -it cli bash

【设置环境】对当前节点需要设置 CHANNEL_NAME 与 orderer 的证书路径 ORDERER_CA,方便后面操作的调用,这里我指定的通道名称是 mychannel。

root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# CHANNEL_NAME=mychannel
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

【区块链内流程】对于新起的第一个 peer 节点,大致步骤为:
创建通道---->>加入通道---->>更新为锚节点---->>安装链码---->>实例化链码

具体命令如下所示:

> 创建通道(需要指定 orderer、通道名称。若开启了 tls 则还需要指定 orderer 的证书位置,在一开始中环境已进行了设置)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA

> 加入通道(使用 -b 参数指定上面创建出来的通道文件)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel join -b mychannel.block

> 更新为锚节点(需要指定 orderer、channel 名称、锚节点文件)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $ORDERER_CA

> 安装链码(需要指定链码名称、链码版本、链码所在文件夹位置)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mycc -v 1.0 -p github.com/chaincode

> 实例化链码(需要指定 orderer 地址、链码名称、链码版本、初始化链码的参数、tls 可选)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}'

【测试】若即经过invoke后,query结果能从100变成90,则能证明当前节点部署完毕了。

> query查询(需要指定通道名称、链码名称、查询参数)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

> invoke执行(需要指定 orderer 地址、通道名称、链码名称、执行参数、tls 可选)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'

至此,分机 peer1 部署节点完成,若途中有问题,可 docker logs 一下 orderer 节点与 peer 节点的日志去进行查看,查看方法在上面 orderer 搭建部分有讲述。

peer1 主机

步骤与上面 peer0 一致,这里只阐述不同点。

【编辑】解压并进入后,先修改 docker-compose-cli.yaml 文件,再启动。这里展示基于源文件修改后的不同部分。

> 当前机器:peer1 主机
> 当前路径:/root/1orderer3peer

services:

  peer1.org1.example.com:						#(原来为 peer0.org1.example.com)
    container_name: peer1.org1.example.com		#(原来为 peer0.org1.example.com)
    extends:
      file:  base/docker-compose-base.yaml
      service: peer1.org1.example.com			#(原来为 peer0.org1.example.com)
      ......
      
  cli:
    environment:			#(原来均为 peer0.org1.example.com)
      - CORE_PEER_ADDRESS=peer1.org1.example.com:7051		
      - 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
    depends_on:				
      - peer1.org1.example.com					#(原来为 peer0.org1.example.com)
    extra_hosts:			
      - "orderer.example.com:UUUU"
      - "peer1.org1.example.com:BBBB"		#(原来为 peer0.org1.example.com)
    ......

【区块链内流程】对于 peer0 之后的同组织同通道的其他节点,大致步骤就只需要:
获取通道文件---->>加入通道---->>安装链码

> 获取通道文件(需要指定要获取的文件名称、orderer、channel)
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel fetch oldest mychannel.block -o orderer.example.com:7050 -c mychannel --tls --cafile $ORDERER_CA

> 加入通道
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel join -b mychannel.block

> 安装链码
root@e46ac3767f56:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mycc -v 1.0 -p github.com/chaincode

(为什么不需要更新为锚节点?)一个组织仅需要 1 个节点作为锚节点,用于与其他组织通信。当前已经有 peer0 作为锚节点了,所以这里就不用设置了。

(为什么不实例化链码?)同通道内的 peer 节点只需其中一个去进行实例化,其他的安装即可,在调用时就会自动完成实例化。

【测试】检测方法同 peer0 。
除此之外还需要检测一下两个节点是否互相连通,例如我在 peer1 执行 invoke 后,query 到结果是从 90 变成了 80,那么此时在 peer0 里面进行 query 也应该查到的同样是 80。

至此,

peer2 主机

与部署 peer1 主机步骤完全一致,同样的仅需编辑 docker-compose-cli.yaml 内信息为 peer2 即可

【测试】这时候就需要检测三台分机 peer 之间的连通是否正常了,大致同 peer1 的检测方法。

至此, 4 分机上搭建区块链网络过程就此结束,接下来可以使用 Caliper 进行压测或直接开发啦,感谢阅读!!

猜你喜欢

转载自blog.csdn.net/Phather/article/details/106222091