Fabric单机多节点网络部署方案

Fabric单机多节点网络部署方案

在本次单机多节点网络部署时采用与e2e_cli案例类似的方案,即一个排序服务节点与两个组织下的peer节点进行组网,其中每个组织都包含两个peer节点。
本次部署的启动顺序是先启动一个Orderer排序服务节点,随后启动peer0.org1节点并在该节点执行通道及合约的相关操作,测试通过后再启动peer0.org2节点,并使得新启动的节点加入到网络及网络所属的通道之中,实现一个伪动态的加入。

第一步:生成相应的证书文件

执行命令做环境清理:

./network_setup.sh down

查看当前都启动了那些容器:

docker ps

查看镜像文件:

docker images

在fabric文件下新建aberic文件夹,将e2e_cli中的configtx.yaml和crypto-config.yaml配置文件上传到aberic目录下。

通过下面的网址下载平台特定使用的二进制文件
https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.0.0/
Fabric单机多节点网络部署方案_第1张图片
将下载的hyperledger-fabric-linux-amd64-1.0.0.tar.gz文件解压缩为bin文件并上传到aberic目录下。
解压缩命令:

tar -xzf hyperledger-fabric-linux-amd64-1.0.0.tar.gz

进入aberic目录下执行如下命令生成项目所需要的文件:

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

命令执行完成后会在aberic目录下生成一个新的目录crypto-config,在crypto-config目录下有odererOrganizations和peerOrganizations两个字目录。

使用configtxgen工具执行configtx.yaml文件已创建orderer Genesis block在此之前需要为configtxgen工具指定configtx.yaml文件的路径,设置环境变量,执行如下命令:

~/go/src/github.com/hyperledger/fabric/aberic$ export FABRIC_CFG_PATH=$PWD

在aberic目录下创建channel-artifacts文件夹,然后运行命令:

~/go/src/github.com/hyperledger/fabric/aberic$ ./bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

命令执行完成后会在channel-artifacts文件夹下生成genesis.block创世区块文件。创世区块文件是为了Oderer排序服务启动时用到的。

Peer节点在启动后需要创建Channel的配置文件在这里也一并生成,执行如下命令:

~/go/src/github.com/hyperledger/fabric/aberic$ ./bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts//mychannel.tx -channelID mychannel

命令执行完成后在channel-artifacts目录下生成一个mychannel.tx通道文件

第二部部署Orderer节点

编写docker-orderer.yaml启动文件,源码如下:

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

version: '2'

services:

  orderer.example.com:
    container_name: orderer.example.com
    image: hyperledger/fabric-orderer
    environment:
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=aberic_default
      # - ORDERER_GENERAL_LOGLEVEL=error
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      #- ORDERER_GENERAL_GENESISPROFILE=AntiMothOrdererGenesis
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      #- ORDERER_GENERAL_LEDGERTYPE=ram
      #- ORDERER_GENERAL_LEDGERTYPE=file
      # enabled TLS
      - ORDERER_GENERAL_TLS_ENABLED=false
      - 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/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
    networks:
      default:
        aliases:
          - aberic
    ports:
      - 7050:7050

这份启动文件中需要注意的是Oderer排序服务监听端口号7050的设定,另外还有ORDERER_GENERAL_GENESISFILE、ORDERER_GENERAL_LOCALMSPID、ORDERER_GENERAL_LOCALMSPDIR的参数需要确保于生成证书文件中所采用的配置文件上下文一致。
将编写好的启动文件上传到aberic目录下。

第三部部署peer0.org1节点

编写Peer节点的启动文件docker-peer.yaml,源码如下:

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

version: '2'

services:

  couchdb:
    container_name: couchdb
    image: hyperledger/fabric-couchdb
    # Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
    # for example map it to utilize Fauxton User Interface in dev environments.
    ports:
      - "5984:5984"

  ca:
    container_name: ca
    image: hyperledger/fabric-ca
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca
      - FABRIC_CA_SERVER_TLS_ENABLED=false
      - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
      - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/15d53ba909ffe5ef0038b1125b58e0ebc521f6693e43652307a92ee1c9c9d258_sk
    ports:
      - "7054:7054"
    command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/15d53ba909ffe5ef0038b1125b58e0ebc521f6693e43652307a92ee1c9c9d258_sk -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config

  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984

      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_NETWORKID=aberic
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP

      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      # 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=aberic
      # - CORE_LOGGING_LEVEL=ERROR
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=aberic_default
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_PROFILE_ENABLED=false
      - CORE_PEER_TLS_ENABLED=false
      - 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
    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
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 7051:7051
      - 7052:7052
      - 7053:7053
    depends_on:
      - couchdb
    networks:
      default:
        aliases:
          - aberic

  cli:
    container_name: cli
    image: hyperledger/fabric-tools
    tty: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      # - CORE_LOGGING_LEVEL=ERROR
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_TLS_ENABLED=false
      - 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
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    volumes:
        - /var/run/:/host/var/run/
        - ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/aberic/chaincode/go
        - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
        - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
    depends_on:
      - peer0.org1.example.com

源码中15d53ba909ffe5ef0038b1125b58e0ebc521f6693e43652307a92ee1c9c9d258_sk需要替换成你刚刚生成的证书文件名称
路径:
在这里插入图片描述
替换为下面的文件名:
在这里插入图片描述
将编写好的启动文件上传到aberic目录下。
第四部搭建Fabric网络
在aberic目录下创建chaincode文件夹,并在chaincode文件夹下创建go文件夹,然后把官方Demo中的chaincode_example02示例上传到go目录下。

在服务器命令行中分别执行如下命令启动orderer和peer:

~/go/src/github.com/hyperledger/fabric/aberic$ docker-compose -f docker-orderer.yaml  up –d
~/go/src/github.com/hyperledger/fabric/aberic$ docker-compose -f docker-peer.yaml  up –d

启动完成后,查看容器是否均已启动,共有5个容器:

docker ps

接下来是对channel和chaincode执行操作,首先是channel的创建和加入操作。对peer节点的操作基本都需要依赖客户端完成,在这里还没有用到第三方的SDK,但安装了fabric-tool镜像,即已启动容器服务中的cli客户端,可以通过如下命令进入客户端对channel进行相关操作:

~/go/src/github.com/hyperledger/fabric/aberic$ docker exec -it cli bash

进入cli客户端后容器后,执行如下命令创建一个通道:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel create -o orderer.example.com:7050 -c mychannel -t 50 -f ./channel-artifacts//mychannel.tx

通道创建完成后,执行如下命令加入通道:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel join -b mychannel.block

第五步链码的安装部署、实例化及功能测试
安装链码:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mychannel -p github.com/hyperledger/fabric/aberic/chaincode/go/chaincode_example02 -v 1.0

实例化链码:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mychannel -c'{"Args":["init","A","10","B","10"]}' -P "OR ('Org1MSP.member')" -v 1.0

查询A账户余额:
/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mychannel -c ‘{“Args”:[“query”,“A”]}’

查询B账户余额:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mychannel -c '{"Args":["query","B"]}'

从A账转5到B账户:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode invoke -C mychannel -n mychannel -c '{"Args":["invoke","A","B","5"]}'

第六步部署peer0.org2节点
为Org2的peer节点编写docker-peer1.yaml启动文件,源码如下:

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

version: '2'

services:

  peer0.org2.example.com:
    container_name: peer0.org2.example.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_PEER_ID=peer0.org2.example.com
      - CORE_PEER_NETWORKID=aberic
      - CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP

      - 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=aberic
      # - CORE_LOGGING_LEVEL=ERROR
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=aberic_default
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_PROFILE_ENABLED=false
      - CORE_PEER_TLS_ENABLED=false
      - 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
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
        - ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 8051:7051
      - 8052:7052
      - 8053:7053
    networks:
      default:
        aliases:
          - aberic

将编写好的启动文件上传到aberic目录下。

启动docker-peer1.yaml:

~/go/src/github.com/hyperledger/fabric/aberic$ docker-compose -f docker-peer1.yaml up –d

对peer0org2执行通道加入、安装链码、测试链码及背书验证的操作。首先进入cli客户端进行一些全局变量的更改,使得cli可以对peer0org2进行相关的操作。

~/go/src/github.com/hyperledger/fabric/aberic$ docker exec -it cli bash

进入cli容器后进行一系列当前容器全局变量赋值操作,命令如下:

root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_PEER_ID=peer0.org2.example.com
root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_PEER_ADDRESS=peer0.org2.example.com:7051
root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052
root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_PEER_LOCALMSPID=Org2MSP
root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example/tls/ca.crt
root@e7cef5bf7dc9:/opt/gopath/src/github.com/hyperledger/fabric/peer# CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp

执行peer0org2加入通道的操作,命令如下:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer channel join -b mychannel.block

安装链码:

/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mychannel -p github.com/hyperledger/fabric/aberic/chaincode/go/chaincode_example02 -v 1.0

到此可以在peer0org2上进行相关的查询操作,与之前的查询操作一致,这里不再赘述!

你可能感兴趣的:(Fabric,源码解析)