Fabric2.3.2分布式离线部署

Fabric2.3.2分布式离线部署,第1张

Fabric2.3.2分布式离线部署

前面我们部署了单节点的fabric,下面我们开始学习部署分布式的fabric。
先在三台机器上安装环境。不会的看上个博客步骤。

IP节点名称192.168.119.144orderer.example.com192.168.119.145peer0.org1.example.com192.168.119.146peer0.org2.example.com

提前准备好fabric二进制文件及镜像上传到服务器上。

# 三台虚机执行
# 新建存放fabric源码的目录并进入此目录:
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger

# 设置网络
vi /etc/hosts
192.168.119.144 orderer.example.com
192.168.119.145 peer0.org1.example.com
192.168.119.146 peer0.org2.example.com

# 导入上传的镜像
docker load -i /root/soft/fabric-images.tar

# 解压Fabric二进制包
cd /root/go/src/github.com/hyperledger
tar xf /root/soft/hyperledger.tar.gz -C .
ll		# 可以看到这些二进制包
drwxr-xr-x 27 root root 4096 Nov  5 16:10 fabric
drwxr-xr-x 17 root root 4096 Nov  5 16:10 fabric-ca
drwxr-xr-x  8 root root 4096 Nov  5 16:12 fabric-sdk-java
1、编译fabric
(1)、首先,进入fabric目录,输入命令:cd fabric
(2)、查看版本,输入命令:git tag
(3)、切换版本,用fabric 2.3.2版本,输入命令:git checkout v2.3.2
# 1.编译fabric,输入命令:
make release
# 2.编译成功后,进入底下的cmd文件夹,输入命令:
cd cmd
# 3.其中discover是服务发现命令,只需要检查另外六个是否安装好,输入以下命令:
../release/linux-amd64/bin/peer version
../release/linux-amd64/bin/orderer version
../release/linux-amd64/bin/configtxgen -version
../release/linux-amd64/bin/configtxlator version
../release/linux-amd64/bin/cryptogen version
../release/linux-amd64/bin/idemixgen version
# 出现版本号为正常
# 设置环境环境变量或者配置可执行文件,二选一即可
1.设置环境变量
vim /etc/profile
export PATH=$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/:$PATH
source /etc/profile
peer version			# 出现版本号为成功
2.配置可执行文件,并授权
cp $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/* /usr/local/bin
chmod -R 775 /usr/local/bin/configtxgen
chmod -R 775 /usr/local/bin/configtxlator
chmod -R 775 /usr/local/bin/cryptogen
chmod -R 775 /usr/local/bin/peer
chmod -R 775 /usr/local/bin/orderer
chmod -R 775 /usr/local/bin/idemixgen
chmod -R 775 /usr/local/bin/discover

# Fabric镜像上方有下载步骤,下载完成后导入到三台机器即可。
2、编译Fabric-ca
首先,回到hyperledger那个目录底下,cd到fabric-ca源码目录下,命令如下:
cd $GOPATH/src/github.com/hyperledger/fabric-ca
# 编译下载的源码,依次输入以下命令:
make fabric-ca-server && make fabric-ca-client

# 设置环境环境变量或者配置可执行文件,也是二选一即可
1.设置环境变量
vim /etc/profile
export PATH=$GOPATH/src/github.com/hyperledger/fabric-ca/bin/:$PATH
source /etc/profile
fabric-ca-server version	# 出现版本号为成功
fabric-ca-client version    # 出现版本号为成功

2.设置编译好的可执行文件,输入以下命令:
cd $GOPATH/src/github.com/hyperledger/fabric-ca/bin
cp * /usr/local/bin/
chmod -R 755 /usr/local/bin/fabric-ca-server
chmod -R 755 /usr/local/bin/fabric-ca-client
fabric-ca-server version	# 出现版本号为成功
fabric-ca-client version    # 出现版本号为成功
3、拷贝Go依赖包
# 由于是内网环境,安装链码的时候会调用到Go的依赖环境包,提前准备好,准备好一台外网机器
# go版本请保持一致,2.3.2版本的fabric容器go版本应该是1.16.7,所以主机也用1.16.7
# 进入到项目目录执行
# 下面步骤在外网机器执行
# 开启
go env -w GO111MODULE=on
# 注解
GO111MODULE=off,无模块支持,go命令行将不会支持module功能,寻找依赖包的方式将会沿用旧版本那种通过vendor目录或者GOPATH模式来查找。
GO111MODULE=on,模块支持,go命令行会使用modules,而一点也不会去GOPATH目录下查找。
GO111MODULE=auto,默认值,go命令行将会根据当前目录来决定是否启用module功能。这种情况下可以分为两种情形:
(1)当前目录在GOPATH/src之外且该目录包含go.mod文件,开启模块支持。
(2)当前文件在包含go.mod文件的目录下面。

# 更换国内源,连接不了外网不用更换
go env -w GOPROXY=https://goproxy.cn,direct
# 内网安装好go之后,放到此目录下边,用的是basic样例链码
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/asset-transfer-basic/chaincode-go/
# 下载依赖
go mod init
go mod vendor

# 下载完毕后,会生成vendor目录
# 把此目录拷贝到需要生成链码项目目录
# 此步骤在内网执行

# 关闭Go的MODULE支持,内网开了也没用,如能连接外网可开启
go env -w GO111MODULE=off
# 把生成的vendor目录打包,拷贝到需要安装fabric内网的机器上,官方示例链码项目目录
# 我放在了如下目录
/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/asset-transfer-basic/chaincode-go
4、生成Fabric证书
# orderer节点
1、编写证书文件
# 创建存放yaml文件夹
mkdir $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/multinodes
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/multinodes
# 创建crypto-config.yaml文件
cryptogen showtemplate > crypto-config.yaml
# 修改crypto-config.yaml
vim crypto-config.yaml
EnableNodeOUs: true		# 修改为true

2.生成证书文件
cryptogen generate --config=crypto-config.yaml
org1.example.com
org2.example.com

5、生成创世块通道文件 1.编写创世块文件
# orderer节点
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 集群节点
        - 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.生成创世区块
configtxgen -profile TwoOrgsOrdererGenesis -channelID fabric-channel -outputBlock ./channel-artifacts/genesis.block

3.生成通道文件
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

4.为 Org1 定义锚节点
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

5.为 Org2 定义锚节点
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

6.将生成的文件拷贝到另两台主机
# 1.复制证书文件到peer0.org1.example.com、peer0.org2.example.com节点
scp -r crypto-config channel-artifacts 192.168.119.145:/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/multinodes
scp -r crypto-config channel-artifacts 192.168.119.146:/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/multinodes

6、编写docker-compose文件 1.orderer节点
vim docker-compose.yaml
version: '2'

services:
  orderer.example.com:
    container_name: orderer.example.com
    image: hyperledger/fabric-orderer:2.3.3		# 镜像地址
    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.119.144"
      - "peer0.org1.example.com:192.168.119.145"
      - "peer0.org2.example.com:192.168.119.146"
2.org1节点
vim docker-compose.yaml
version: '2'

services:
  couchdb0.org1.example.com:
    container_name: couchdb0.org1.example.com
    image: couchdb:3.1.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.3.3		# 修改镜像版本
    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.119.144"
      - "peer0.org1.example.com:192.168.119.145"
      - "peer0.org2.example.com:192.168.119.146"
  cli:
    container_name: cli
    image: hyperledger/fabric-tools:2.3.3		# 修改镜像版本
    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.119.144"
      - "peer0.org1.example.com:192.168.119.145"
      - "peer0.org2.example.com:192.168.119.146"
3.org2节点
vim docker-compose.yaml
version: '2'

services:
  couchdb0.org2.example.com:
    container_name: couchdb0.org2.example.com
    image: couchdb:3.1.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.3.3		# 修改版本
    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.119.144"
      - "peer0.org1.example.com:192.168.119.145"
      - "peer0.org2.example.com:192.168.119.146"
  cli:
    container_name: cli
    image: hyperledger/fabric-tools:2.3.3		# 修改镜像版本
    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.119.144"
      - "peer0.org1.example.com:192.168.119.145"
      - "peer0.org2.example.com:192.168.119.146"
4.启动
# order
docker-compose -f docker-compose.yaml up -d
[+] Running 2/2
 ⠿ Network multinodes_default     Created                                                      3.5s
 ⠿ Container orderer.example.com  Started                                                     21.1s

# org1
# 如缺少couchdb镜像,请在之前搭建的单节点下载导入
docker-compose -f docker-compose.yaml up -d
[+] Running 4/4
 ⠿ Network multinodes_default           Created                                                2.0s
 ⠿ Container couchdb0.org1.example.com  Started                                               24.5s
 ⠿ Container cli                        Started                                               24.6s
 ⠿ Container peer0.org1.example.com     Started                                               31.1s

# org2
docker-compose -f docker-compose.yaml up -d
[+] Running 4/4
 ⠿ Network multinodes_default           Created                                                4.6s
 ⠿ Container cli                        Started                                               33.2s
 ⠿ Container couchdb0.org2.example.com  Started                                               33.0s
 ⠿ Container peer0.org2.example.com     Started                                               34.0s
 
 
 
 # 停止
docker-compose down

7、通道 *** 作 1.创建通道
# org1
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-11 14:55:39.192 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-11-11 14:55:39.458 UTC [cli.common] readBlock -> INFO 002 Received block: 0

bash-5.1# ls
channel-artifacts  crypto             mychannel.block

# 将通道文件 mychannel.block 拷贝到宿主机及其他节点的容器
# org1
docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./
scp mychannel.block peer0.org2.example.com:/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/multinodes
# org2
docker cp mychannel.block cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/

2、加入通道
# org1
docker exec -it cli bash
bash-5.1# peer channel join -b mychannel.block

# org2
docker exec -it cli bash
bash-5.1# peer channel join -b mychannel.block

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

# 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

8、安装调用智能合约 1.复制官方示例智能合约
# org1
# 复制到容器内
# 容器内设置官方实例链码
# 没有此目录就创建,有就略过
mkdir chaincode/go -p
cp -r ../asset-transfer-basic/chaincode-go/* chaincode/go/

docker exec -it cli bash
bash-5.1# ls ../../fabric-cluster/chaincode/go/
assetTransfer.go  chaincode         go.mod            go.sum            vendor

# 关闭Go的MODULE支持
bash-5.1# go env -w GO111MODULE=off
# 内网跳过以下步骤,直接打包链码

# 解释
GO111MODULE=off 无模块支持,go 会从 GOPATH 和 vendor 文件夹寻找包。
GO111MODULE=on 模块支持,go 会忽略 GOPATH 和 vendor 文件夹,只根据 go.mod 下载依赖。
GO111MODULE=auto 在 $GOPATH/src 外面且根目录有 go.mod 文件时,开启模块支持。
2.打包链码
docker exec -it cli bash
bash-5.1# cd /opt/gopath/src/github.com/hyperledger/fabric/peer
peer lifecycle chaincode package basic.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric-cluster/chaincode/go/ --label basic
bash-5.1# ls -l
total 2044
drwxr-x---    2 root     root            95 Nov 11 11:37 channel-artifacts
drwxr-xr-x    4 root     root            59 Nov 11 11:19 crypto
-rw-r--r--    1 root     root         20916 Nov 16 02:45 mychannel.block
-rw-------    1 root     root       2065724 Nov 16 02:58 basic.tar.gz

# 如果失败是因为被墙或者内网原因
# 被墙换源
# 内网原因找一台可连接外网虚机,搭建好对应go版本后(每个版本依赖库可能会不一样,最好版本一致),下载go依赖
# 可参考:https://blog.csdn.net/wyongqing/article/details/119841993

4、安装链码
# 把链码拷贝到容器内,放到/root/go/src/github.com/hyperledger/multinodes下
# org1
pwd
/root/go/src/github.com/hyperledger/multinodes
docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/basic.tar.gz .
scp basic.tar.gz peer0.org2.example.com:/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/multinodes

docker exec -it cli bash
bash-5.1# peer lifecycle chaincode install basic.tar.gz

basic:70f1e7b22b419345e4cb581e528dfbdbde16bb7f97a032b2b695125e141f893d	# 记好此ID,下面要用

# org2
docker cp basic.tar.gz cli:/opt/gopath/src/github.com/hyperledger/fabric/peer
docker exec -it cli bash
bash-5.1# peer lifecycle chaincode install basic.tar.gz

basic:70f1e7b22b419345e4cb581e528dfbdbde16bb7f97a032b2b695125e141f893d	# 记好此ID,下面要用

5、批准链码
# 查看已安装的链码:
# bash-5.1#  peer lifecycle chaincode queryinstalled
Installed chaincodes on peer:
Package ID: basic:70f1e7b22b419345e4cb581e528dfbdbde16bb7f97a032b2b695125e141f893d, Label: basic

查看节点已提交合约
peer lifecycle chaincode querycommitted --channelID mychannel --name basic

# 为安装链码时输出的结果
basic:70f1e7b22b419345e4cb581e528dfbdbde16bb7f97a032b2b695125e141f893d

# org1
# 批准链码
bash-5.1# peer lifecycle chaincode approveformyorg --channelID mychannel --name basic --version 1.0 --init-required --package-id basic:70f1e7b22b419345e4cb581e528dfbdbde16bb7f97a032b2b695125e141f893d --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

# org2
bash-5.1# peer lifecycle chaincode approveformyorg --channelID mychannel --name basic --version 1.0 --init-required --package-id basic:70f1e7b22b419345e4cb581e528dfbdbde16bb7f97a032b2b695125e141f893d --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

6、查看链码是否就绪
# org1
bash-5.1# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --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


# org2
bash-5.1# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --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

7、提交链码
# org1提交即可
bash-5.1# peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name basic --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

8、链码初始化
# org1初始化即可
bash-5.1# peer chaincode invoke -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com --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 -C mychannel -n basic --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 --isInit -c '{"function":"InitLedger","Args":[]}'

# 输出
2021-11-18 07:57:38.542 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

9、查询数据
# org1
# 查询
bash-5.1# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[
{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},       {"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},  {"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10}, {"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10}, {"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15}, {"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15}
]

10、调用链码,修改数据
# org2或者org1执行都可以
# 更改asset6 资产所有者Michel为Christopher
bash-5.1# peer chaincode invoke -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com --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 -C mychannel -n basic --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 '{"function":"TransferAsset","Args":["asset6","Christopher"]}'

2021-11-18 08:04:03.406 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

# 结果显示 asset6 转给了 Christopher:
bash-5.1# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Christopher","Size":15}

# org2查询
bash-5.1# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Christopher","Size":15}

11、清除网络
docker-compose down

到此,分布式部署完成!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5609787.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存