区块链——Hyperledger Fabric2.2多机搭建及区块链浏览器

Posted 增发真人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区块链——Hyperledger Fabric2.2多机搭建及区块链浏览器相关的知识,希望对你有一定的参考价值。

文章目录


一、搭建环境

1、设置网络

root@order:~# vi /etc/hosts
192.168.1.110 orderer.example.com
192.168.1.120 peer0.org1.example.com
192.168.1.130 peer0.org2.example.com

2、安装docker和docker-compose

root@order:~# apt-get update
root@order:~# apt-get -y install apt-transport-https ca-certificates curl software-properties-common
root@order:~# curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
root@order:~# add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
root@order:~# apt-get -y update
root@order:~# apt-get -y install docker-ce
root@order:~# service docker start
root@order:~# systemctl enable docker
root@order:~# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
root@order:~# chmod +x /usr/local/bin/docker-compose
root@order:~# docker-compose -v
docker-compose version 1.29.2, build 5becea4c

3、安装golang环境

root@order:~# wget https://golang.google.cn/dl/go1.16.8.linux-amd64.tar.gz
root@order:~# tar zxf go1.16.8.linux-amd64.tar.gz
root@order:~# mv go /usr/local/
root@order:~# vim /etc/profile
export GOPATH=~/Go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
root@order:~# source /etc/profile
root@order:~# go version
go version go1.16.8 linux/amd64

二、生成Fabric证书

1、编写证书文件

root@order:~# curl -sSL https://bit.ly/2ysbOFE | bash -s
root@order:~# cp fabric-samples/bin/* /usr/local/bin/
root@order:~# vim /etc/profile
export PATH=~/fabric-samples/bin:$PATH
root@order:~# source /etc/profile
root@order:~# mkdir hyperledger/multinodes/
root@order:~# cd hyperledger/multinodes/
root@order:~/hyperledger/multinodes# cryptogen showtemplate > crypto-config.yaml
root@order:~/hyperledger/multinodes# vim crypto-config.yaml
OrdererOrgs:

  - Name: Orderer
    Domain: example.com
    EnableNodeOUs: true

    Specs:
      - Hostname: orderer
PeerOrgs:
 
  - Name: org1
    Domain: org1.example.com
    EnableNodeOUs: true
    Template:
      Count: 1
    Users:
      Count: 1
      
  - Name: org2
    Domain: org2.example.com
    EnableNodeOUs: true
    Template:
      Count: 1
    Users:
      Count: 1   

2、生成证书文件

root@order:~/hyperledger/multinodes# cryptogen generate --config=crypto-config.yaml
org1.example.com
org2.example.com

3、复制证书文件到节点

root@order:~/hyperledger/multinodes# scp -r ./crypto-config root@192.168.1.120:~/hyperledger/multinodes/
root@order:~/hyperledger/multinodes# scp -r ./crypto-config root@192.168.1.130:~/hyperledger/multinodes/

三、生成通道文件

1、编写创世块文件

root@order:~/hyperledger/multinodes# vim configtx.yaml
---
Organizations:

    - &OrdererOrg

        Name: OrdererOrg
        ID: OrdererMSP
        MSPDir: ./crypto-config/ordererOrganizations/example.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('OrdererMSP.member')"
            Writers:
                Type: Signature
                Rule: "OR('OrdererMSP.member')"
            Admins:
                Type: Signature
                Rule: "OR('OrdererMSP.admin')"
        OrdererEndpoints:
            - orderer.example.com:7050

    - &Org1
   
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: ./crypto-config/peerOrganizations/org1.example.com/msp
        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')"
        AnchorPeers:
            - Host: peer0.org1.example.com
              Port: 7051

    - &Org2
    
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: ./crypto-config/peerOrganizations/org2.example.com/msp
        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:
            - Host: peer0.org2.example.com
              Port: 9051


Capabilities:

    Channel: &ChannelCapabilities

        V2_0: true

    Orderer: &OrdererCapabilities

        V2_0: true

    Application: &ApplicationCapabilities

        V2_0: true

Application: &ApplicationDefaults

    Organizations:

    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

Orderer: &OrdererDefaults

    OrdererType: solo 

    Addresses:
        - orderer.example.com:7050

    EtcdRaft:
        Consenters:
        - Host: orderer.example.com
          Port: 7050
          ClientTLSCert: ../organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
          ServerTLSCert: ../organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt

    BatchTimeout: 2s
    BatchSize:
        MaxMessageCount: 10
        AbsoluteMaxBytes: 99 MB
        PreferredMaxBytes: 512 KB

    Organizations:

    Policies:
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"
      
        BlockValidation:
            Type: ImplicitMeta
            Rule: "ANY Writers"


Channel: &ChannelDefaults

    Policies:
       
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"

    
    Capabilities:
        <<: *ChannelCapabilities

Profiles:

    TwoOrgsOrdererGenesis:
        <<: *ChannelDefaults
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
    TwoOrgsChannel:
        Consortium: SampleConsortium
        <<: *ChannelDefaults
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2
            Capabilities:
                <<: *ApplicationCapabilities

2、生成创世块文件和通道文件

1、生成创世区块

root@order:~/hyperledger/multinodes# configtxgen -profile TwoOrgsOrdererGenesis -channelID fabric-channel -outputBlock ./channel-artifacts/genesis.block
2021-11-04 10:40:27.919 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-04 10:40:27.925 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2021-11-04 10:40:27.925 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: configtx.yaml
2021-11-04 10:40:27.927 CST [common.tools.configtxgen] doOutputBlock -> INFO 004 Generating genesis block
2021-11-04 10:40:27.927 CST [common.tools.configtxgen] doOutputBlock -> INFO 005 Creating system channel genesis block
2021-11-04 10:40:27.927 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Writing genesis block

2、生成通道文件

root@order:~/hyperledger/multinodes# configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2021-11-04 10:40:31.528 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-04 10:40:31.533 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: configtx.yaml
2021-11-04 10:40:31.533 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 003 Generating new channel configtx
2021-11-04 10:40:31.535 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 004 Writing new channel tx

3、为 Org1 定义锚节点

root@order:~/hyperledger/multinodes# configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2021-11-04 10:40:35.494 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-04 10:40:35.499 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: configtx.yaml
2021-11-04 10:40:35.499 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Generating anchor peer update
2021-11-04 10:40:35.500 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 004 Writing anchor peer update

4、为 Org2 定义锚节点

root@order:~/hyperledger/multinodes# configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2021-11-04 10:40:38.888 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-11-04 10:40:38.893 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: configtx.yaml
2021-11-04 10:40:38.893 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Generating anchor peer update
2021-11-04 10:40:38.894 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 004 Writing anchor peer update

5、将生成的文件拷贝到另两台主机

root@order:~/hyperledger/multinodes# scp -r ./channel-artifacts root@192.168.1.120:~/hyperledger/multinodes/
root@order:~/hyperledger/multinodes# scp -r ./channel-artifacts root@192.168.1.130:~/hyperledger/multinodes/

四、编写docker-compose文件

1、orderer节点

root@order:~/hyperledger/multinodes# vim docker-compose.yaml
version: '2'

services:
  orderer.example.com:
    container_name: orderer.example.com
    image: hyperledger/fabric-orderer:2.2
    environment:
      - FABRIC_LOGGING_SPEC=INFO
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      - 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_TLS_ENABLED=true
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
      - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
      - ORDERER_KAFKA_VERBOSE=true
      - ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric
    command: orderer
    volumes:
      - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
      - ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
      - ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
    ports:
      - 7050:7050
    extra_hosts:
      - "orderer.example.com:192.168.1.110"
      - "peer0.org1.example.com:192.168.1.120"
      - "peer0.org2.example.com:192.168.1.130"

2、org1节点

root@org1:~/hyperledger/multinodes# vim docker-compose.yaml
version: '2'

services:
  couchdb0.org1.example.com:
    container_name: couchdb0.org1.example.com
    image: couchdb:3.1
    environment:
      - COUCHDB_USER=admin
      - COUCHDB_PASSWORD=adminpw
    ports:
      - 5984:5984

  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    image: hyperledger/fabric-peer:2.2
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_ADDRESS=peer0.org1.example.com: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=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - FABRIC_LOGGING_SPEC=INFO
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - 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_CHAINCODE_EXECUTETIMEOUT=300s
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0.org1.example.com:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=adminpw
    depends_on:
      - couchdb0.org1.example.com

    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
      - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
    ports:
      - 7051:7051
      - 7052:7052
      - 7053:7053
    extra_hosts:
      - "orderer.example.com:192.168.1.110"
      - "peer0.org1.example.com:192.168.1.120"
      - "peer0.org2.example.com:192.168.1.130"
  cli:
    container_name: cli
    image: hyperledger/fabric-tools:2.2
    tty: true
    stdin_open: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - FABRIC_LOGGING_SPEC=INFO
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: /bin/bash
    volumes:
      - /var/run/:/host/var/run/
      - ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric-cluster/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
    extra_hosts:
      - "orderer.example.com:192.168.1.110"
      - "peer0.org1.example.com:192.168.1.120"
      - "peer0.org2.example.com:192.168.1.130"

3、org2节点

root@org2:~/hyperledger/multinodes# vim docker-compose.yaml
version: '2'

services:
  couchdb0.org2.example.com:
    container_name: couchdb0.org2.example.com
    image: couchdb:3.1
    environment:
      - COUCHDB_USER=admin
      - COUCHDB_PASSWORD=adminpw
    ports:
      - 5984:5984

  peer0.org2.example.com:
    container_name: peer0.org2.example.com
    image: hyperledger/fabric-peer:2.2
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org2.example.com
      - CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:7051
      - CORE_PEER_CHAINCODEADDRESS=peer0.org2.example.com:7052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP
      - FABRIC_LOGGING_SPEC=INFO
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - 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_CHAINCODE_EXECUTETIMEOUT=300s
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0.org2.example.com:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=adminpw
    depends_on:
      - couchdb0.org2.example.com

    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    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
    ports:
      - 7051:7051
      - 7052:7052
      - 7053:7053
    extra_hosts:
      - "orderer.example.com:192.168.1.110"
      - "peer0.org1.example.com:192.168.1.120"
      - "peer0.org2.example.com:192.168.1.130"
  cli:
    container_name: cli
    image: hyperledger/fabric-tools:2.2
    tty: true
    stdin_open: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - FABRIC_LOGGING_SPEC=INFO
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
      - 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
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: /bin/bash
    volumes:
      - /var/run/:/host/var/run/
      - ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric-cluster/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
    extra_hosts:
      - "orderer.example.com:192.168.1.110"
      - "peer0.org1.example.com:192.168.1.120"
      - "peer0.org2.example.com:192.168.1.130"

4、启动

root@order:~/hyperledger/multinodes# docker-compose up -d
Creating network "multinodes_default" with the default driver
Creating orderer.example.com ... done
root@org1:~/hyperledger/multinodes# docker-compose up -d
Creating network "multinodes_default" with the default driver
Creating couchdb0.org1.example.com ... done
Creating cli                       ... done
Creating peer0.org1.example.com    ... done
root@org2:~/hyperledger/multinodes# docker-compose up -d
Creating network "multinodes_default" with the default driver
Creating cli                       ... done
Creating couchdb0.org2.example.com ... done
Creating peer0.org2.example.com    ... done

五、通道操作

1、创建通道

root@org1:~/hyperledger/multinodes# docker exec -it cli bash
bash-5.1# 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/msp/tlscacerts/tlsca.example.com-cert.pem
2021-11-04 05:35:20.209 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-04 05:35:20.243 UTC [cli.common] readBlock -> INFO 002 Received block: 0
bash-5.1# ls
channel-artifacts  crypto             mychannel.block

将通道文件 mychannel.block 拷贝到宿主机及其他节点的容器

root@org1:~/hyperledger/multinodes# docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./
root@org1:~/hyperledger/multinodes# scp mychannel.block root@192.168.1.130:~/hyperledger/multinodes/
root@org2:~/hyperledger/multinodes# docker cp mychannel.block cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/

2、加入通道

org1

root@org1:~/hyperledger/multinodes# docker exec -it cli bash
bash-5.1# peer channel join -b mychannel.block
2021-11-04 05:39:36.166 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-04 05:39:36.292 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

org2

root@org2:~/hyperledger/multinodes# docker exec -it cli bash
bash-5.1# peer channel join -b mychannel.block
2021-11-04 05:39:29.125 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-04 05:39:29.237 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

3、更新锚节点

org1

bash-5.1# peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
2021-11-04 05:39:59.758 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-04 05:39:59.770 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update

org2

bash-5.1# peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
2021-11-04 05:40:14.723 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-04 05:40:14.740 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update

六、安装调用智能合约

1、复制官方示例智能合约

root@order:~/hyperledger/multinodes# cp -r ~/hyperledger/fabric-samples/chaincode/sacc chaincode/go/

2、容器内设置go语言依赖包

bash-5.1# cd /opt/gopath/src/github.com/hyperledger/fabric-cluster/chaincode/go/sacc
bash-5.1# ls
go.mod        go.sum        sacc.go       sacc_test.go  vendor
bash-5.1# go env -w GOPROXY=https://goproxy.cn,direct
bash-5.1# go env -w GO111MODULE=auto
bash-5.1# go mod init
go: /opt/gopath/src/github.com/hyperledger/fabric-cluster/chaincode/go/sacc/go.mod already exists
bash-5.1# go mod vendor
go: downloading github.com/hyperledger/fabric-chaincode-go v0.0.0-20190823162523-04390e015b85
go: downloading github.com/hyperledger/fabric-protos-go v0.0.0-20190821214336-621b908d5022
go: downloading github.com/golang/protobuf v1.3.2
go: downloading google.golang.org/grpc v1.23.0
go: downloading golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7
go: downloading google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55
go: downloading golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a
go: downloading golang.org/x/text v0.3.2

3、打包链码

bash-5.1# cd /opt/gopath/src/github.com/hyperledger/fabric/peer
bash-5.1# peer lifecycle chaincode package sacc.tar.gz \\
  --path github.com/hyperledger/fabric-cluster/chaincode/go/sacc/ \\
  --label sacc_1
bash-5.1# ls
channel-artifacts  crypto             mychannel.block    sacc.tar.gz

4、安装链码

root@org1:~/hyperledger/multinodes# docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/sacc.tar.gz ./
root@org1:~/hyperledger/multinodes# scp sacc.tar.gz root@192.168.1.130:~/hyperledger/multinodes
root@org2:~/hyperledger/multinodes# docker cp ~/hyperledger/multinodes/sacc.tar.gz cli:/opt/gopath/src/github.com/hyperledger/fabric/peer

org1

root@org1:~/hyperledger/multinodes# docker exec -it cli bash
bash-5.1# peer lifecycle chaincode install sacc.tar.gz
2021-11-04 05:44:44.537 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\\nGsacc_1:2791ff83074c654ab40b864ba03f6bb2710439d720adc883c26438212de8d259\\022\\006sacc_1" >
2021-11-04 05:44:44.537 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: sacc_1:2791ff83074c654ab40b864ba03f6bb2710439d720adc883c26438212de8d259

org2

root@org2:~/hyperledger/multinodes# docker exec -it cli bash
bash-5.1# peer lifecycle chaincode install sacc.tar.gz
2021-11-04 05:44:36.627 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\\nGsacc_1:2791ff83074c654ab40b864ba03f6bb2710439d720adc883c26438212de8d259\\022\\006sacc_1" >
2021-11-04 05:44:36.627 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: sacc_1:2791ff83074c654ab40b864ba03f6bb2710439d720adc883c26438212de8d259

5、批准链码

org1

bash-5.1# peer lifecycle chaincode approveformyorg --channelID mychannel --name sacc --version 1.0 --init-required --package-id sacc_1:2791ff83074c654ab40b864ba03f6bb2710439d720adc883c26438212de8d259 --sequence 1 --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
2021-11-04 05:45:51.128 UTC [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:7050
2021-11-04 05:45:53.260 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [c2bebc9d5dbbe4b342facd9ae558dec6cc142f373bbf5a08fbf0953e06c2dce4] committed with status (VALID) at peer0.org1.example.com:7051

org2

bash-5.1# peer lifecycle chaincode approveformyorg --channelID mychannel --name sacc --version 1.0 --init-required --package-id sacc_1:2791ff83074c654ab40b864ba03f6bb2710439d720adc883c26438212de8d259 --sequence 1 --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
2021-11-04 05:45:46.313 UTC [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:7050
2021-11-04 05:45:48.447 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [456f2a9b88c235812d679e5a8b79fd2476f0843f51dcb273b239102f649a2c76] committed with status (VALID) at peer0.org2.example.com:7051

6、查看链码是否就绪

org1

bash-5.1# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name sacc --version 1.0 --init-required --sequence 1 --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 --output json

        "approvals": 
                "Org1MSP": true,
                "Org2MSP": true
        

org2

bash-5.1# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name sacc --version 1.0 --init-required --sequence 1 --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 --output json

        "approvals": 
                "Org1MSP": true,
                "Org2MSP": true
        

7、提交链码

bash-5.1# peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name sacc --version 1.0 --sequence 1 --init-required --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 --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
2021-11-04 05:46:43.990 UTC [chaincodeCmd] ClientWait -> INFO 001 txid [ac1b9887ed54dda567f54417216b6ac34255fcdccad14ffa8dd4eeb4b78fe781] committed with status (VALID) at peer0.org1.example.com:7051
2021-11-04 05:46:44.025 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [ac1b9887ed54dda567f54417216b6ac34255fcdccad14ffa8dd4eeb4b78fe781] committed with status (VALID) at peer0.org2.example.com:7051

8、链码初始化

bash-5.1# peer chaincode invoke -o orderer.example.com:7050 --isInit --ordererTLSHostnameOverride orderer.example.com --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 sacc --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 -c '"Args":["a","bb"]'
2021-11-04 05:46:55.388 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

9、查询数据

bash-5.1# peer chaincode query -C mychannel -n sacc -c '"Args":["query","a"]'
bb

10、调用链码,新增数据

bash-5.1# 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 sacc --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 -c '"Args":["set","a","cc"]'
2021-11-04 05:47:25.219 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"cc"
bash-5.1# peer chaincode query -C mychannel -n sacc -c '"Args":["query","a"]'
cc

七、搭建超级账本区块链浏览器

1、下载配置文件

root@order:~/hyperledger/multinodes# mkdir explorer
root@order:~/hyperledger/multinodes# cd explorer/
root@order:~/hyperledger/multinodes/explorer# wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/examples/net1/config.json
root@order:~/hyperledger/multinodes/explorer# wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/examples/net1/connection-profile/test-network.json -P connection-profile
root@order:~/hyperledger/multinodes/explorer# wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/docker-compose.yaml
root@order:~/hyperledger/multinodes/explorer# cd ..
root@order:~/hyperledger/multinodes# cd crypto-config
root@order:~/hyperledger/multinodes/crypto-config#ls
ordererOrganizations  peerOrganizations
root@order:~/hyperledger/multinodes/crypto-config#cd ..
root@order:~/hyperledger/multinodes# cp -r crypto-config explorer/organizations
root@order:~/hyperledger/multinodes# cd explorer/
root@order:~/hyperledger/multinodes/explorer# ls organizations/
ordererOrganizations  peerOrganizations

2、修改配置文件

root@order:~/hyperledger/multinodes/explorer# cd connection-profile/
root@order:~/hyperledger/multinodes/explorer/connection-profile# mv test-network.json org1-network.json
root@order:~/hyperledger/multinodes/explorer/connection-profile# vim org1-network.json
root@order:~/hyperledger/multinodes/explorer/connection-profile# sed -i "s/test-network/org1-network/g" org

以上是关于区块链——Hyperledger Fabric2.2多机搭建及区块链浏览器的主要内容,如果未能解决你的问题,请参考以下文章

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

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

hyperledger fabric v2.4环境搭建及区块链项目开发

16. Fabric2.2 区块链农产品溯源系统 - 区块链浏览器部署(Fabric Explorer)

12. Fabric2.2 区块链农产品溯源系统 - 智能合约开发-2

11. Fabric2.2 区块链农产品溯源系统 - 智能合约开发-1