Hyperledger Fabric基于kafka多机部署

各主机角色和安装的软件
主机编号 A B C D
IP 45.32.103.254 149.28.146.218 207.148.75.101 66.42.57.57
角色

orderer0.example.com

zookeeper1

kafka1

ca

orderer1.example.com

zookeeper2

kafka2

peer0.org1.example.com

zookeeper3

kafka3

peer1.org1.example.com

所有主机操作系统均为CentOS 7 x64

s1:准备工作

修改主机A、B、C、D的/etc/hosts文件均如下 主要是把后面四行添加上

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

127.0.0.1 guest
::1       guest

45.32.103.254  A orderer0.example.com
149.28.146.218 B orderer1.example.com
207.148.75.101 C peer0.org1.example.com
66.42.57.57    D peer1.org1.example.com

关闭主机A、B、C、D防火墙

systemctl stop firewalld.service

内存不足可用如下方法扩充内存

mkdir /opt/images
dd if=/dev/zero of=/opt/images/swap bs=2048 count=2097152
mkswap /opt/images/swap
swapon /opt/images/swap

s2:在A、B、C主机安装java kafka运行依赖java环境

下载jdk-8u144-linux-x64.tar.gz并解压

tar -C /usr/local -zxvf jdk-8u144-linux-x64.tar.gz

修改/etc/profile 添加以下三行 配置java环境变量

export JAVA_HOME=/usr/local/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar:.

然后执行

source /etc/profile

检查java是否安装成功

s3:在A、B、C主机安装kafka

下载kafka_2.10-0.10.2.0.tgz并解压

tar -C /usr/local -zxvf kafka_2.10-0.10.2.0.tgz
mv /usr/local/kafka_2.10-0.10.2.0 /usr/local/kafka

修改/usr/local/kafka/config/zookeeper.properties 添加或修改如下配置

dataDir=/usr/local/kafka/data/zookeeper
tickTime=2000
initLimit=5
syncLimit=2
server.1=A:2888:3888
server.2=B:2888:3888
server.3=C:2888:3888

 创建路径/usr/local/kafka/data/zookeeper

mkdir -p /usr/local/kafka/data/zookeeper

对于主机A 执行

echo 1 > /usr/local/kafka/data/zookeeper/myid

对于主机B 执行

echo 2 > /usr/local/kafka/data/zookeeper/myid

对于主机C 执行

echo 3 > /usr/local/kafka/data/zookeeper/myid

修改/usr/local/kafka/config/server.properties

对于主机A做如下修改

broker.id=1
listeners=PLAINTEXT://A:9092
num.partitions=3
zookeeper.connect=A:2181,B:2181,C:2181

对于主机B做如下修改 

broker.id=2
listeners=PLAINTEXT://B:9092
num.partitions=3
zookeeper.connect=A:2181,B:2181,C:2181

对于主机C做如下修改

broker.id=3
listeners=PLAINTEXT://C:9092
num.partitions=3
zookeeper.connect=A:2181,B:2181,C:2181

 分别启动A、B、C主机zookeeper

nohup /usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties >> ~/zookeeper.log 2>&1 &

启动日志

Hyperledger Fabric基于kafka多机部署_第1张图片

在A、B、C主机分别启动kafka

nohup /usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties>>~/kafka.log 2>&1 &

启动日志

Hyperledger Fabric基于kafka多机部署_第2张图片

 s4:生成证书 创世区块 channel.tx

在A、B、C、D主机分别下载hyperledger-fabric-linux-amd64-1.1.0.tar.gz并解压

mkdir /opt/fabric
tar -C /opt/fabric -zxvf hyperledger-fabric-linux-amd64-1.1.0.tar.gz

修改A、B、C、D主机的/etc/profile文件 添加

export PATH=/opt/fabric/bin:$PATH

然后执行

source /etc/profile

A主机/opt/fabric路径下创建crypto-config.yaml内容如下

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

# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer
    Domain: example.com
    # ---------------------------------------------------------------------------
    # "Specs" - See PeerOrgs below for complete description
    # ---------------------------------------------------------------------------
    Specs:
      - Hostname: orderer0
      - Hostname: orderer1
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
  # ---------------------------------------------------------------------------
  # Org1
  # ---------------------------------------------------------------------------
  - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true
    # ---------------------------------------------------------------------------
    # "Specs"
    # ---------------------------------------------------------------------------
    # Uncomment this section to enable the explicit definition of hosts in your
    # configuration.  Most users will want to use Template, below
    #
    # Specs is an array of Spec entries.  Each Spec entry consists of two fields:
    #   - Hostname:   (Required) The desired hostname, sans the domain.
    #   - CommonName: (Optional) Specifies the template or explicit override for
    #                 the CN.  By default, this is the template:
    #
    #                              "{{.Hostname}}.{{.Domain}}"
    #
    #                 which obtains its values from the Spec.Hostname and
    #                 Org.Domain, respectively.
    # ---------------------------------------------------------------------------
    # Specs:
    #   - Hostname: foo # implicitly "foo.org1.example.com"
    #     CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above
    #   - Hostname: bar
    #   - Hostname: baz
    # ---------------------------------------------------------------------------
    # "Template"
    # ---------------------------------------------------------------------------
    # Allows for the definition of 1 or more hosts that are created sequentially
    # from a template. By default, this looks like "peer%d" from 0 to Count-1.
    # You may override the number of nodes (Count), the starting index (Start)
    # or the template used to construct the name (Hostname).
    #
    # Note: Template and Specs are not mutually exclusive.  You may define both
    # sections and the aggregate nodes will be created for you.  Take care with
    # name collisions
    # ---------------------------------------------------------------------------
    Template:
      Count: 2
      # Start: 5
      # Hostname: {{.Prefix}}{{.Index}} # default
    # ---------------------------------------------------------------------------
    # "Users"
    # ---------------------------------------------------------------------------
    # Count: The number of user accounts _in addition_ to Admin
    # ---------------------------------------------------------------------------
    Users:
      Count: 1

A主机/opt/fabric路径下创建configtx.yaml内容如下

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

---
################################################################################
#
#   Section: Organizations
#
#   - This section defines the different organizational identities which will
#   be referenced later in the configuration.
#
################################################################################
Organizations:

    # SampleOrg defines an MSP using the sampleconfig.  It should never be used
    # in production but may be used as a template for other definitions
    - &OrdererOrg
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: OrdererOrg

        # ID to load the MSP definition as
        ID: OrdererMSP

        # MSPDir is the filesystem path which contains the MSP configuration
        MSPDir: crypto-config/ordererOrganizations/example.com/msp

    - &Org1
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org1MSP

        # ID to load the MSP definition as
        ID: Org1MSP

        MSPDir: crypto-config/peerOrganizations/org1.example.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org1.example.com
              Port: 7051

################################################################################
#
#   SECTION: Application
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for application related parameters
#
################################################################################
Application: &ApplicationDefaults

    # Organizations is the list of orgs which are defined as participants on
    # the application side of the network
    Organizations:

################################################################################
#
#   SECTION: Orderer
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for orderer related parameters
#
################################################################################
Orderer: &OrdererDefaults

    # Orderer Type: The orderer implementation to start
    # Available types are "solo" and "kafka"
    OrdererType: kafka

    Addresses:
        - orderer0.example.com:7050
        - orderer1.example.com:7050

    # Batch Timeout: The amount of time to wait before creating a batch
    BatchTimeout: 2s

    # Batch Size: Controls the number of messages batched into a block
    BatchSize:

        # Max Message Count: The maximum number of messages to permit in a batch
        MaxMessageCount: 10

        # Absolute Max Bytes: The absolute maximum number of bytes allowed for
        # the serialized messages in a batch.
        AbsoluteMaxBytes: 99 MB

        # Preferred Max Bytes: The preferred maximum number of bytes allowed for
        # the serialized messages in a batch. A message larger than the preferred
        # max bytes will result in a batch larger than preferred max bytes.
        PreferredMaxBytes: 512 KB

    Kafka:
        # Brokers: A list of Kafka brokers to which the orderer connects
        # NOTE: Use IP:port notation
        Brokers:
            - A:9092
            - B:9092
            - C:9092

    # Organizations is the list of orgs which are defined as participants on
    # the orderer side of the network
    Organizations:

################################################################################
#
#   Profile
#
#   - Different configuration profiles may be encoded here to be specified
#   as parameters to the configtxgen tool
#
################################################################################
Profiles:

    OneOrgOrdererGenesis:
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
    OneOrgChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1

A主机/opt/fabric路径创建generate.sh 用于生成证书 创世区块 channel.tx

#!/bin/bash

# 生成证书
if [ -d crypto-config ]; then rm -rf crypto-config/*; fi
cryptogen generate --config=crypto-config.yaml
if [ $? -ne 0 ]; then echo "failed to generate crypto"; exit 1; fi

# 生成创世区块
if [ ! -d channel-artifacts ]; then mkdir channel-artifacts; else rm -rf channel-artifacts/*; fi
configtxgen -profile OneOrgOrdererGenesis -outputBlock channel-artifacts/genesis.block
if [ $? -ne 0 ]; then echo "failed to generate genesis.block"; exit 1; fi
# 生成channel.tx
configtxgen -profile OneOrgChannel -outputCreateChannelTx channel-artifacts/channel.tx -channelID mychannel
if [ $? -ne 0 ]; then echo "failed to generate channel.tx"; exit 1; fi

然后在/opt/fabric路径执行

chmod +x generate.sh
./generate.sh

s5:A、B主机启动orderer

修改A、B主机/opt/fabric/config/orderer.yaml 并copy到/opt/fabric路径

ListenAddress: 0.0.0.0 # 25行
Enabled: true # 32行
GenesisMethod: file # 64行
GenesisProfile: OneOrgOrdererGenesis # 71行
GenesisFile: genesis.block # 76行
LocalMSPID: OrdererMSP # 88行
Location: /data/hyperledger/production/orderer # 137行

在A主机/opt/fabric路径执行

cp -r crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp ./
cp -r crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls ./
cp channel-artifacts/genesis.block ./
scp -r crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp root@b:/opt/fabric
scp -r crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls root@b:/opt/fabric
scp channel-artifacts/genesis.block root@b:/opt/fabric

分别在A、B主机启动orderer 在/opt/fabric路径执行启动命令

nohup orderer>>~/orderer.log 2>&1 &

启动日志

Hyperledger Fabric基于kafka多机部署_第3张图片

s6:C、D主机启动peer

修改C、D主机/opt/fabric/config/core.yaml 并copy到/opt/fabric路径

enabled:  true # 262行
fileSystemPath: /data/hyperledger/production # 300行
localMspId: Org1MSP # 328行

在A主机/opt/fabric路径执行

scp -r crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp root@c:/opt/fabric
scp -r crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls root@c:/opt/fabric
scp -r crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp root@d:/opt/fabric
scp -r crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls root@d:/opt/fabric

 分别在C、D主机启动peer 在/opt/fabric路径执行启动命令

nohup peer node start>>~/peer.log 2>&1 &

启动日志

Hyperledger Fabric基于kafka多机部署_第4张图片

s7:A主机安装golang

通过哪台主机连接peer节点就在哪台主机安装golang 通过A主机连接peer节点

安装fabric-ca-server需要安装golang A主机安装fabric-server

A主机下载go1.10.linux-amd64.tar.gz并解压

tar -C /usr/local -zxvf go1.10.linux-amd64.tar.gz

A主机修改/etc/profile 添加

export PATH=/usr/local/go/bin:$PATH

然后执行

source /etc/profile

检查golang是否安装成功

Hyperledger Fabric基于kafka多机部署_第5张图片

s8:A主机启动fabric-ca-server

A主机下载依赖

yum install git
yum install gcc
yum install gcc-c++
yum install libtool
yum install libtool-ltdl
yum install libtool-ltdl-devel

A主机创建路径

mkdir -p /root/go/src/github.com/hyperledger

A主机切换到/root/go/src/github.com/hyperledger路径 下载fabric-ca-server

git clone -b v1.1.0 https://github.com/hyperledger/fabric-ca

A主机切换到/root/go/src/github.com/hyperledger/fabric-ca路径 编译安装fabric-ca-server

make fabric-ca-server

A主机编辑/etc/profile 添加

export PATH=/root/go/src/github.com/hyperledger/fabric-ca/bin:$PATH

然后执行

source /etc/profile

 A主机切换到/opt/fabric路径 启动fabric-ca-server

nohup fabric-ca-server start --ca.certfile crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem --ca.keyfile crypto-config/peerOrganizations/org1.example.com/ca/6dc2b8960f93794a5b87c8b55b5e0574bc54ea6e71d82b2b9d98ea2b04b6537c_sk -b admin:adminpw>>~/fabric-ca-server.log 2>&1 &

6dc2b8960f93794a5b87c8b55b5e0574bc54ea6e71d82b2b9d98ea2b04b6537c_sk要替换成自己主机上的 

s9:C、D主机安装docker

chaincode需要在docker容器里运行

在C、D主机执行

yum install docker

 在C、D主机启动docker

service docker start

在C、D主机下载镜像

docker pull hyperledger/fabric-ccenv:x86_64-1.1.0
docker pull hyperledger/fabric-baseimage:x86_64-0.4.6
docker pull hyperledger/fabric-baseos:x86_64-0.4.6

s10:创建channel 各peer节点加入channel

A主机/opt/fabric路径创建channel.sh 用于创建channel 各peer节点加入channel

#!/bin/bash

# 创建channel
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer channel create -f channel.tx -c mychannel -o orderer0.example.com:7050 --tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
if [ $? -ne 0 ]; then echo "failed to create channel"; exit 1; fi

# peer0.org1.example.com加入channel
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer channel join -b mychannel.block
if [ $? -ne 0 ]; then echo "peer0.org1.example.com failed to join channel"; exit 1; fi
# peer1.org1.example.com加入channel
CORE_PEER_ADDRESS=peer1.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer channel join -b mychannel.block
if [ $? -ne 0 ]; then echo "peer1.org1.example.com failed to join channel"; exit 1; fi

将C主机/opt/fabric/core.yaml文件copy到A主机

scp /opt/fabric/core.yaml root@a:/opt/fabric

在A主机/opt/fabric路径执行

cp channel-artifacts/channel.tx ./
chmod +x channel.sh
./channel.sh

s11:安装 实例化 调用 查询chaincode

在A主机创建chaincode路径

mkdir /root/go/src/github.com/181011

在A主机chaincode路径(/root/go/src/github.com/181011)创建181011.go 代码如下

package main

import (
	"github.com/hyperledger/fabric/core/chaincode/shim"
	"github.com/hyperledger/fabric/protos/peer"
	"errors"
	"fmt"
	"time"
	"encoding/json"
)

type FishChaincode struct {

}

type Fish struct {
	Id string `json:"id"`
	Vessel string `json:"vessel"`
	Location string `json:"location"`
	Timestamp int64 `json:"timestamp"`
	Holder string `json:"holder"`
}

func (fc *FishChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
	return shim.Success(nil)
}

func (fc *FishChaincode) recordFish(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgsNum(args,4)
	if err!=nil {
		return shim.Error(err.Error())
	}
	id:=args[0]
	fb,err:=stub.GetState(id)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if fb!=nil {
		return shim.Error("fish exist")
	}
	vessel:=args[1]
	location:=args[2]
	timestamp:=time.Now().Unix()
	holder:=args[3]
	fish:=Fish{id,vessel,location,timestamp,holder}
	fb,err=json.Marshal(fish)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(id,fb)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (fc *FishChaincode) transferFish(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgsNum(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	id:=args[0]
	fb,err:=stub.GetState(id)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if fb==nil {
		return shim.Error("fish not exist")
	}
	fish:=Fish{}
	err=json.Unmarshal(fb,&fish)
	if err!=nil {
		return shim.Error(err.Error())
	}
	newholder:=args[1]
	fish.Holder=newholder
	fb,err=json.Marshal(fish)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(id,fb)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (fc *FishChaincode) queryFish(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgsNum(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	id:=args[0]
	fb,err:=stub.GetState(id)
	if err!=nil {
		return shim.Error(err.Error())
	}
	if fb==nil {
		return shim.Error("fish not exist")
	}
	return shim.Success(fb)
}

func (fc *FishChaincode) queryFishByRange(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	start:=args[0]
	end:=args[1]
	iter,err:=stub.GetStateByRange(start,end)
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	fishes:=[]Fish{}
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		fish:=Fish{}
		err=json.Unmarshal(item.Value,&fish)
		if err!=nil {
			return shim.Error(err.Error())
		}
		fishes=append(fishes,fish)
	}
	fb,err:=json.Marshal(fishes)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(fb)
}

func (fc *FishChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
	fn,args:=stub.GetFunctionAndParameters()
	if fn=="recordFish" {
		return fc.recordFish(stub,args)
	} else if fn=="transferFish" {
		return fc.transferFish(stub,args)
	} else if fn=="queryFish" {
		return fc.queryFish(stub,args)
	} else if fn=="queryFishByRange" {
		return fc.queryFishByRange(stub,args)
	}
	return shim.Error("")
}

func main()  {
	shim.Start(new(FishChaincode))
}

func checkArgsNum(args []string,n int) error {
	if len(args) != n {
		return errors.New(fmt.Sprintf("%d parameter(s) required",n))
	}
	return nil
}

在A主机下载chaincode依赖的包

go get github.com/hyperledger/fabric/core/chaincode/shim
go get github.com/hyperledger/fabric/protos/peer

在A主机chaincode路径(/root/go/src/github.com/181011)编译chaincode

go build 181011.go

在A主机/opt/fabric路径创建chaincode.sh 用于安装 实例化 调用 查询chaincode

#!/bin/bash
# peer0.org1.example.com安装chaincode
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode install -n 181011 -v 1.0 -p github.com/181011
if [ $? -ne 0 ]; then echo "peer0.org1.example.com failed to install chaincode"; exit 1; fi
# peer1.org1.example.com安装chaincode
CORE_PEER_ADDRESS=peer1.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode install -n 181011 -v 1.0 -p github.com/181011
if [ $? -ne 0 ]; then echo "peer1.org1.example.com failed to install chaincode"; exit 1; fi

# 实例化chaincode
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode instantiate -n 181011 -v 1.0 -C mychannel -c '{"args":["Init"]}' -o orderer1.example.com:7050 --tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
if [ $? -ne 0 ]; then echo "failed to instantiate chaincode"; exit 1; fi
sleep 10

# peer1.org1.example.com调用chaincode
CORE_PEER_ADDRESS=peer1.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode invoke -n 181011 -C mychannel -c '{"args":["recordFish","fish2","38A","67.0006 -70.5476","wang"]}' -o orderer1.example.com:7050 --tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
if [ $? -ne 0 ]; then echo "peer1.org1.example.com failed to invoke chaincode"; exit 1; fi
sleep 10
# peer0.org1.example.com查询chaincode
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode query -n 181011 -C mychannel -c '{"args":["queryFish","fish2"]}' -o orderer0.example.com:7050 --tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
if [ $? -ne 0 ]; then echo "peer0.org1.example.com failed to query chaincode"; exit 1; fi
sleep 10
# peer0.org1.example.com调用chaincode
CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode invoke -n 181011 -C mychannel -c '{"args":["transferFish","fish2","xu"]}' -o orderer0.example.com:7050 --tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
if [ $? -ne 0 ]; then echo "peer0.org1.example.com failed to invoke chaincode"; exit 1; fi
sleep 10
# peer1.org1.example.com查询chaincode
CORE_PEER_ADDRESS=peer1.org1.example.com:7051 CORE_PEER_LOCALMSPID=Org1MSP CORE_PEER_TLS_ENABLED=true CORE_PEER_TLS_CERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crt CORE_PEER_TLS_KEY_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key CORE_PEER_TLS_ROOTCERT_FILE=crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp peer chaincode query -n 181011 -C mychannel -c '{"args":["queryFishByRange","fish1","fish3"]}' -o orderer1.example.com:7050 --tls --cafile crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
if [ $? -ne 0 ]; then echo "peer1.org1.example.com failed to query chaincode"; exit 1; fi

在A主机/opt/fabric路径执行

chmod +x chaincode.sh
./chaincode.sh

last step:开发webapp提供服务

源码参见https://github.com/zmx6999/FishWebapp

你可能感兴趣的:(Hyperledger Fabric基于kafka多机部署)