Hyperledger fabric+kafka+docker-compose多机部署

主机 A B C D
IP 139.180.137.89 139.180.209.250 139.180.137.0 198.13.46.60
角色 zookeeper0
kafka0
orderer0.trace.com
zookeeper1
kafka1
orderer1.trace.com
zookeeper2
kafka2
peer0.orgdairy.trace.com
peer1.orgdairy.trace.com
peer0.orgprocess.trace.com
peer1.orgprocess.trace.com
ca_OrgDairy
ca_OrgProcess
cli
kafka3
peer0.orgsell.trace.com
peer1.orgsell.trace.com
ca_OrgSell

 

1.准备工作

(1)从https://github.com/zmx6999/fabric_tools下载工具包fabric_tools

(2)将fabric_tools里面的init.sh拷至主机A、B、C、D 执行

./init.sh 190116

将安装docker docker-compose go 下载fabric fabric-samples fabric镜像 并创建目录/root/fabric/scripts/fabric-samples/190116/network

2.生成证书 创世区块 channel.tx 锚节点相关文件

(1)将fabric_tools里面的generate.json generate.go generate.sh拷至C主机

/root/fabric/scripts/fabric-samples/190116/network 并切换到该目录

(2)修改配置文件generate.json内容如下

{
  "domain": "trace.com",
  "orderers": [
    "orderer0",
    "orderer1"
  ],
  "kafkas": [
    "kafka0",
    "kafka1",
    "kafka2",
    "kafka3"
  ],
  "peer_orgs": [
    {
      "org_name": "OrgDairy",
      "peer_count": 2,
      "user_count": 1,
      "anchor_peers": [
        "peer0"
      ]
    },
    {
      "org_name": "OrgProcess",
      "peer_count": 2,
      "user_count": 1,
      "anchor_peers": [
        "peer0"
      ]
    },
    {
      "org_name": "OrgSell",
      "peer_count": 2,
      "user_count": 1,
      "anchor_peers": [
        "peer0"
      ]
    }
  ],
  "genesis_profile": "ThreeOrgsOrdererGenesis",
  "channel_profile": "ThreeOrgsChannel",
  "channel": "trace"
}

(3)执行

chmod +x generate.sh
./generate.sh

这样就在当前目录生成crypto-config文件夹存放证书文件 创世区块genesis.block channel.tx 锚节点文件DairyOrgMSPanchors.tx ProcessOrgMSPanchors.tx SellOrgMSPanchors.tx

3.生成docker-compose配置文件并启动zookeeper kafka orderer peer ca cli

(1)将fabric_tools里面的docker_compose_cfggen.json docker_compose_cfggen.go docker_compose_cfggen.sh分别拷至A、B、C、D主机/root/fabric/scripts/fabric-samples/190116/network 并在各主机切换到该目录

(2)A主机编辑docker_compose_cfggen.json内容如下

{
  "domain": "trace.com",
  "zookeepers": [
    {
      "host_name": "zookeeper0",
      //外部端口
      "ports": [
        "2181", //第一项对应内部端口2181
        "2888", //第二项对应内部端口2888
        "3888" //第三项对应内部端口3888
      ],
      "zoo_my_id": "1",
      "zoo_servers": "server.1=zookeeper0:2888:3888 server.2=zookeeper1:2888:3888 server.3=zookeeper2:2888:3888"
    }
  ],
  "kafkas": [
    {
      "host_name": "kafka0",
      "broker_id": "0",
      "zookeepers": [
        "zookeeper0:2181",
        "zookeeper1:2181",
        "zookeeper2:2181"
      ]
    }
  ],
  "orderers": [
    {
      "orderer_name": "orderer0",
      "kafka_brokers": [
        "kafka0",
        "kafka1",
        "kafka2",
        "kafka3"
      ],
      //外部端口
      "ports": [
        "7050" //对应内部端口7050
      ]
    }
  ],
  "hosts": [
    "zookeeper0:139.180.137.89",
    "kafka0:139.180.137.89",
    "orderer0.trace.com:139.180.137.89",
    "zookeeper1:139.180.209.250",
    "kafka1:139.180.209.250",
    "orderer1.trace.com:139.180.209.250",
    "zookeeper2:139.180.137.0",
    "kafka2:139.180.137.0",
    "peer0.orgdairy.trace.com:139.180.137.0",
    "peer1.orgdairy.trace.com:139.180.137.0",
    "peer0.orgprocess.trace.com:139.180.137.0",
    "peer1.orgprocess.trace.com:139.180.137.0",
    "kafka3:198.13.46.60",
    "peer0.orgsell.trace.com:198.13.46.60",
    "peer1.orgsell.trace.com:198.13.46.60"
  ]
}

B主机编辑docker_compose_cfggen.json内容如下

{
  "domain": "trace.com",
  "zookeepers": [
    {
      "host_name": "zookeeper1",
      //外部端口
      "ports": [
        "2181", //第一项对应内部端口2181
        "2888", //第二项对应内部端口2888
        "3888" //第三项对应内部端口3888
      ],
      "zoo_my_id": "2",
      "zoo_servers": "server.1=zookeeper0:2888:3888 server.2=zookeeper1:2888:3888 server.3=zookeeper2:2888:3888"
    }
  ],
  "kafkas": [
    {
      "host_name": "kafka1",
      "broker_id": "1",
      "zookeepers": [
        "zookeeper0:2181",
        "zookeeper1:2181",
        "zookeeper2:2181"
      ]
    }
  ],
  "orderers": [
    {
      "orderer_name": "orderer1",
      "kafka_brokers": [
        "kafka0",
        "kafka1",
        "kafka2",
        "kafka3"
      ],
      //外部端口
      "ports": [
        "8050" //对应内部端口7050
      ]
    }
  ],
  "hosts": [
    "zookeeper0:139.180.137.89",
    "kafka0:139.180.137.89",
    "orderer0.trace.com:139.180.137.89",
    "zookeeper1:139.180.209.250",
    "kafka1:139.180.209.250",
    "orderer1.trace.com:139.180.209.250",
    "zookeeper2:139.180.137.0",
    "kafka2:139.180.137.0",
    "peer0.orgdairy.trace.com:139.180.137.0",
    "peer1.orgdairy.trace.com:139.180.137.0",
    "peer0.orgprocess.trace.com:139.180.137.0",
    "peer1.orgprocess.trace.com:139.180.137.0",
    "kafka3:198.13.46.60",
    "peer0.orgsell.trace.com:198.13.46.60",
    "peer1.orgsell.trace.com:198.13.46.60"
  ]
}

C主机编辑docker_compose_cfggen.json内容如下

{
  "domain": "trace.com",
  "cas": [
    {
      "peer_org_name": "OrgDairy",
      //外部端口
      "ports": [
        "7054" //对应内部端口7054
      ],
      "admin_name": "admin",
      "admin_password": "adminpw"
    },
    {
      "peer_org_name": "OrgProcess",
      //外部端口
      "ports": [
        "8054" //对应内部端口7054
      ],
      "admin_name": "admin",
      "admin_password": "adminpw"
    }
  ],
  "zookeepers": [
    {
      "host_name": "zookeeper2",
      //外部端口
      "ports": [
        "2181", //第一项对应内部端口2181
        "2888", //第二项对应内部端口2888
        "3888" //第三项对应内部端口3888
      ],
      "zoo_my_id": "3",
      "zoo_servers": "server.1=zookeeper0:2888:3888 server.2=zookeeper1:2888:3888 server.3=zookeeper2:2888:3888"
    }
  ],
  "kafkas": [
    {
      "host_name": "kafka2",
      "broker_id": "2",
      "zookeepers": [
        "zookeeper0:2181",
        "zookeeper1:2181",
        "zookeeper2:2181"
      ]
    }
  ],
  "peers": [
    {
      "peer_name": "peer0",
      "org_name": "OrgDairy",
      //外部端口
      "ports": [
        "7051", //第一项对应内部端口7051
        "7052", //第二项对应内部端口7052
        "7053" //第三项对应内部端口7053
      ]
    },
    {
      "peer_name": "peer1",
      "org_name": "OrgDairy",
      //外部端口
      "ports": [
        "8051", //第一项对应内部端口7051
        "8052", //第二项对应内部端口7052
        "8053" //第三项对应内部端口7053
      ]
    },
    {
      "peer_name": "peer0",
      "org_name": "OrgProcess",
      //外部端口
      "ports": [
        "9051", //第一项对应内部端口7051
        "9052", //第二项对应内部端口7052
        "9053" //第三项对应内部端口7053
      ]
    },
    {
      "peer_name": "peer1",
      "org_name": "OrgProcess",
      //外部端口
      "ports": [
        "10051", //第一项对应内部端口7051
        "10052", //第二项对应内部端口7052
        "10053" //第三项对应内部端口7053
      ]
    }
  ],
  "clis": [
    {
      "cli_name": "cli",
      "core_peer_name": "peer0",
      "core_peer_org": "OrgDairy",
      "depends": [
        "peer0.orgdairy.trace.com",
        "peer1.orgdairy.trace.com",
        "peer0.orgprocess.trace.com",
        "peer1.orgprocess.trace.com"
      ]
    }
  ],
  "hosts": [
    "zookeeper0:139.180.137.89",
    "kafka0:139.180.137.89",
    "orderer0.trace.com:139.180.137.89",
    "zookeeper1:139.180.209.250",
    "kafka1:139.180.209.250",
    "orderer1.trace.com:139.180.209.250",
    "zookeeper2:139.180.137.0",
    "kafka2:139.180.137.0",
    "peer0.orgdairy.trace.com:139.180.137.0",
    "peer1.orgdairy.trace.com:139.180.137.0",
    "peer0.orgprocess.trace.com:139.180.137.0",
    "peer1.orgprocess.trace.com:139.180.137.0",
    "kafka3:198.13.46.60",
    "peer0.orgsell.trace.com:198.13.46.60",
    "peer1.orgsell.trace.com:198.13.46.60"
  ]
}

D主机编辑docker_compose_cfggen.json内容如下

{
  "domain": "trace.com",
  "cas": [
    {
      "peer_org_name": "OrgSell",
      //外部端口
      "ports": [
        "9054" //对应内部端口7054
      ],
      "admin_name": "admin",
      "admin_password": "adminpw"
    }
  ],
  "kafkas": [
    {
      "host_name": "kafka3",
      "broker_id": "3",
      "zookeepers": [
        "zookeeper0:2181",
        "zookeeper1:2181",
        "zookeeper2:2181"
      ]
    }
  ],
  "peers": [
    {
      "peer_name": "peer0",
      "org_name": "OrgSell",
      //外部端口
      "ports": [
        "11051", //第一项对应内部端口7051
        "11052", //第二项对应内部端口7052
        "11053" //第三项对应内部端口7053
      ]
    },
    {
      "peer_name": "peer1",
      "org_name": "OrgSell",
      //外部端口
      "ports": [
        "12051", //第一项对应内部端口7051
        "12052", //第二项对应内部端口7052
        "12053" //第三项对应内部端口7053
      ]
    }
  ],
  "hosts": [
    "zookeeper0:139.180.137.89",
    "kafka0:139.180.137.89",
    "orderer0.trace.com:139.180.137.89",
    "zookeeper1:139.180.209.250",
    "kafka1:139.180.209.250",
    "orderer1.trace.com:139.180.209.250",
    "zookeeper2:139.180.137.0",
    "kafka2:139.180.137.0",
    "peer0.orgdairy.trace.com:139.180.137.0",
    "peer1.orgdairy.trace.com:139.180.137.0",
    "peer0.orgprocess.trace.com:139.180.137.0",
    "peer1.orgprocess.trace.com:139.180.137.0",
    "kafka3:198.13.46.60",
    "peer0.orgsell.trace.com:198.13.46.60",
    "peer1.orgsell.trace.com:198.13.46.60"
  ]
}

(3)从C主机拷相关证书文件和创世区块到其他主机

A主机执行

cd /root/fabric/scripts/fabric-samples/190116/network
mkdir channel-artifacts
mkdir -p crypto-config/ordererOrganizations/trace.com/orderers

从C主机拷相关证书文件和创世区块到A主机 C主机/root/fabric/scripts/fabric-samples/190116/network路径执行

scp channel-artifacts/genesis.block [email protected]:/root/fabric/scripts/fabric-samples/190116/network/channel-artifacts
scp -r crypto-config/ordererOrganizations/trace.com/orderers/orderer0.trace.com [email protected]:/root/fabric/scripts/fabric-samples/190116/network/crypto-config/ordererOrganizations/trace.com/orderers

B主机执行

cd /root/fabric/scripts/fabric-samples/190116/network
mkdir channel-artifacts
mkdir -p crypto-config/ordererOrganizations/trace.com/orderers

从C主机拷相关证书文件和创世区块到B主机 C主机/root/fabric/scripts/fabric-samples/190116/network路径执行

scp channel-artifacts/genesis.block [email protected]:/root/fabric/scripts/fabric-samples/190116/network/channel-artifacts
scp -r crypto-config/ordererOrganizations/trace.com/orderers/orderer1.trace.com [email protected]:/root/fabric/scripts/fabric-samples/190116/network/crypto-config/ordererOrganizations/trace.com/orderers

D主机执行

cd /root/fabric/scripts/fabric-samples/190116/network
mkdir -p crypto-config/peerOrganizations/orgsell.trace.com

 从C主机拷相关证书文件到D主机 C主机/root/fabric/scripts/fabric-samples/190116/network路径执行

scp -r crypto-config/peerOrganizations/orgsell.trace.com/peers [email protected]:/root/fabric/scripts/fabric-samples/190116/network/crypto-config/peerOrganizations/orgsell.trace.com
scp -r crypto-config/peerOrganizations/orgsell.trace.com/ca [email protected]:/root/fabric/scripts/fabric-samples/190116/network/crypto-config/peerOrganizations/orgsell.trace.com

(3)各主机分别执行

cd /root/fabric/scripts/fabric-samples/190116/network
chmod +x docker_compose_cfggen.sh
./docker_compose_cfggen.sh trace #trace为COMPOSE_PROJECT_NAME

将生成docker-compose配置文件zookeeper.yaml kafka.yaml docker-compose.yaml

(4)A、B、C主机分别启动zookeeper 3台主机/root/fabric/scripts/fabric-samples/190116/network路径分别执行

docker-compose -f zookeeper.yaml up -d

(5)A、B、C、D主机分别启动kafka 四台主机/root/fabric/scripts/fabric-samples/190116/network路径分别执行

docker-compose -f kafka.yaml up -d

(6)A、B、C、D主机/root/fabric/scripts/fabric-samples/190116/network路径分别执行

docker-compose -f docker-compose.yaml up -d

A主机将启动orderer0.trace.com B主机将启动orderer1.trace.com C主机将启动peer0.orgdairy.trace.com peer1.orgdairy.trace.com peer0.orgprocess.trace.com peer1.orgprocess.trace.com ca_OrgDairy ca_OrgProcess cli D主机将启动peer0.orgsell.trace.com peer1.orgsell.trace.com ca_OrgSell

4.创建并加入channel 更新锚节点

(1)将fabric_tools里面的channel.go channel.sh拷至C主机/root/fabric/scripts/fabric-samples/190116/network 并切换到该目录

(2)创建trace.json内容如下

{
  "domain": "trace.com",
  "channel_name": "trace",
  "orderer": {
    "orderer_name": "orderer0",
    "port": "7050"
  },
  "cli_name": "cli",
  "channel_orgs": [
    {
      "org_name": "OrgDairy",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "7051"
        },
        {
          "peer_name": "peer1",
          "port": "8051"
        }
      ],
      "anchor_peers": [
        {
          "peer_name": "peer0",
          "port": "7051"
        }
      ]
    },
    {
      "org_name": "OrgProcess",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "9051"
        },
        {
          "peer_name": "peer1",
          "port": "10051"
        }
      ],
      "anchor_peers": [
        {
          "peer_name": "peer0",
          "port": "9051"
        }
      ]
    },
    {
      "org_name": "OrgSell",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "11051"
        },
        {
          "peer_name": "peer1",
          "port": "12051"
        }
      ],
      "anchor_peers": [
        {
          "peer_name": "peer0",
          "port": "11051"
        }
      ]
    }
  ]
}

(3)执行

chmod +x channel.sh
./channel.sh trace.json

将创建channel 各peer节点加入channel 更新锚节点

5.编写链码

(1)在C主机/root/fabric/scripts/fabric-samples/190116路径创建chaincode目录 切换到chaincode目录分别创建目录dairy process sell 分别存放奶牛场 加工厂 销售组织链码

(2)在创建的dairy目录创建dairy.go内容如下

package main

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

type DairyChaincode struct {

}

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

func (dc *DairyChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
	_,args:=stub.GetFunctionAndParameters()
	err:=checkArgs(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	fn:=args[0]
	if fn=="set" {
		return dc.set(stub,args[1:])
	} else if fn=="get" {
		return dc.get(stub,args[1:])
	} else if fn=="history" {
		return dc.history(stub,args[1:])
	}
	return shim.Error("METHOD NOT FOUND")
}

func (dc *DairyChaincode) set(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(args[0],[]byte(args[1]))
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (dc *DairyChaincode) get(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	data,err:=stub.GetState(args[0])
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(data)
}

func (dc *DairyChaincode) history(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	iter,err:=stub.GetHistoryForKey(args[0])
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	var list []string
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		v:=fmt.Sprintf("%s|%s",time.Unix(item.Timestamp.Seconds,0).Format("2006-01-02 15:04:05"),item.Value)
		list=append(list,v)
	}
	data,err:=json.Marshal(list)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(data)
}

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

func checkArgs(args []string,n int) error {
	if len(args)

(3)在创建的process目录创建process.go内容如下

package main

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

type ProcessChaincode struct {

}

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

func (pc *ProcessChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
	_,args:=stub.GetFunctionAndParameters()
	err:=checkArgs(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	fn:=args[0]
	if fn=="set" {
		return pc.set(stub,args[1:])
	} else if fn=="get" {
		return pc.get(stub,args[1:])
	} else if fn=="history" {
		return pc.history(stub,args[1:])
	}
	return shim.Error("METHOD NOT FOUND")
}

func (pc *ProcessChaincode) set(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(args[0],[]byte(args[1]))
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (pc *ProcessChaincode) get(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	data,err:=stub.GetState(args[0])
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(data)
}

func (pc *ProcessChaincode) history(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	iter,err:=stub.GetHistoryForKey(args[0])
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	var list []string
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		v:=fmt.Sprintf("%s|%s",time.Unix(item.Timestamp.Seconds,0).Format("2006-01-02 15:04:05"),item.Value)
		list=append(list,v)
	}
	data,err:=json.Marshal(list)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(data)
}

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

func checkArgs(args []string,n int) error {
	if len(args)

(4)在创建的sell目录创建sell.go内容如下

package main

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

type SellChaincode struct {

}

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

func (sc *SellChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
	_,args:=stub.GetFunctionAndParameters()
	err:=checkArgs(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	fn:=args[0]
	if fn=="set" {
		return sc.set(stub,args[1:])
	} else if fn=="get" {
		return sc.get(stub,args[1:])
	} else if fn=="history" {
		return sc.history(stub,args[1:])
	}
	return shim.Error("METHOD NOT FOUND")
}

func (sc *SellChaincode) set(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,2)
	if err!=nil {
		return shim.Error(err.Error())
	}
	err=stub.PutState(args[0],[]byte(args[1]))
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(nil)
}

func (sc *SellChaincode) get(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	data,err:=stub.GetState(args[0])
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(data)
}

func (sc *SellChaincode) history(stub shim.ChaincodeStubInterface,args []string) peer.Response {
	err:=checkArgs(args,1)
	if err!=nil {
		return shim.Error(err.Error())
	}
	iter,err:=stub.GetHistoryForKey(args[0])
	if err!=nil {
		return shim.Error(err.Error())
	}
	defer iter.Close()
	var list []map[string]interface{}
	for iter.HasNext() {
		item,err:=iter.Next()
		if err!=nil {
			return shim.Error(err.Error())
		}
		v:=fmt.Sprintf("%s|%s",time.Unix(item.Timestamp.Seconds,0).Format("2006-01-02 15:04:05"),item.Value)
		m:=map[string]interface{}{
			"info": v,
		}

		process:=strings.Split(v,"|")[1]
		response:=stub.InvokeChaincode("process",[][]byte{[]byte("invoke"),[]byte("history"),[]byte(process)},"trace")
		if response.Status!=shim.OK {
			return shim.Error(response.String())
		}
		var _processList []string
		err=json.Unmarshal(response.Payload,&_processList)
		if err!=nil {
			return shim.Error(err.Error())
		}

		var processList []map[string]interface{}
		for _,_v:=range _processList{
			dairy:=strings.Split(_v,"|")[1]
			_response:=stub.InvokeChaincode("dairy",[][]byte{[]byte("invoke"),[]byte("history"),[]byte(dairy)},"trace")
			if _response.Status!=shim.OK {
				return shim.Error(_response.String())
			}
			var dairyList []string
			err=json.Unmarshal(_response.Payload,&dairyList)
			_m:=map[string]interface{}{
				"info": _v,
				"trace": dairyList,
			}
			processList=append(processList,_m)
		}
		m["trace"]=processList
		list=append(list,m)
	}
	data,err:=json.Marshal(list)
	if err!=nil {
		return shim.Error(err.Error())
	}
	return shim.Success(data)
}

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

func checkArgs(args []string,n int) error {
	if len(args)

6.安装 实例化 调用链码

(1)将fabric_tools里面的chaincode.go chaincode.sh拷至C主机/root/fabric/scripts/fabric-samples/190116/network 并切换到该目录

(2)创建dairy.json内容如下

{
  "domain": "trace.com",
  "channel_name": "trace",
  "chaincode_name": "dairy",
  "chaincode_version": "1.0",
  "orderer": {
    "orderer_name": "orderer0",
    "port": "7050"
  },
  "endorse": "AND ('OrgDairyMSP.member')",
  "cli_name": "cli",
  "chaincode_orgs": [
    {
      "org_name": "OrgDairy",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "7051"
        },
        {
          "peer_name": "peer1",
          "port": "8051"
        }
      ]
    },
    {
      "org_name": "OrgSell",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "11051"
        },
        {
          "peer_name": "peer1",
          "port": "12051"
        }
      ]
    }
  ]
}

(3)执行

chmod +x chaincode.sh
./chaincode.sh -i dairy.json

将安装 实例化奶牛场dairy链码

(4)创建process.json内容如下

{
  "domain": "trace.com",
  "channel_name": "trace",
  "chaincode_name": "process",
  "chaincode_version": "1.0",
  "orderer": {
    "orderer_name": "orderer0",
    "port": "7050"
  },
  "endorse": "AND ('OrgProcessMSP.member')",
  "cli_name": "cli",
  "chaincode_orgs": [
    {
      "org_name": "OrgProcess",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "9051"
        },
        {
          "peer_name": "peer1",
          "port": "10051"
        }
      ]
    },
    {
      "org_name": "OrgSell",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "11051"
        },
        {
          "peer_name": "peer1",
          "port": "12051"
        }
      ]
    }
  ]
}

(5)执行

./chaincode.sh -i process.json

将安装 实例化加工厂process链码

(6)创建sell.json内容如下

{
  "domain": "trace.com",
  "channel_name": "trace",
  "chaincode_name": "sell",
  "chaincode_version": "1.0",
  "orderer": {
    "orderer_name": "orderer0",
    "port": "7050"
  },
  "endorse": "AND ('OrgSellMSP.member')",
  "cli_name": "cli",
  "chaincode_orgs": [
    {
      "org_name": "OrgSell",
      "peers": [
        {
          "peer_name": "peer0",
          "port": "11051"
        },
        {
          "peer_name": "peer1",
          "port": "12051"
        }
      ]
    }
  ]
}

(7)执行

./chaincode.sh -i sell.json

将安装 实例化销售组织sell链码

(8)调用奶牛场dairy链码

创建dairy_test.sh内容如下

#!/bin/bash

docker exec cli peer chaincode invoke -n dairy -C trace -c '{"args":["invoke","set","dairy101","info101"]}' --peerAddresses peer0.orgdairy.trace.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer0.orgdairy.trace.com/tls/ca.crt -o orderer1.trace.com:8050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/trace.com/orderers/orderer1.trace.com/msp/tlscacerts/tlsca.trace.com-cert.pem
sleep 5

docker exec -e CORE_PEER_ADDRESS=peer1.orgdairy.trace.com:8051 -e CORE_PEER_LOCALMSPID=OrgDairyMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/users/[email protected]/msp cli peer chaincode query -n dairy -C trace -c '{"args":["invoke","get","dairy101"]}'

docker exec cli peer chaincode invoke -n dairy -C trace -c '{"args":["invoke","set","dairy102","info102"]}' --peerAddresses peer0.orgdairy.trace.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer0.orgdairy.trace.com/tls/ca.crt -o orderer1.trace.com:8050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/trace.com/orderers/orderer1.trace.com/msp/tlscacerts/tlsca.trace.com-cert.pem
sleep 5

docker exec -e CORE_PEER_ADDRESS=peer1.orgdairy.trace.com:8051 -e CORE_PEER_LOCALMSPID=OrgDairyMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/users/[email protected]/msp cli peer chaincode query -n dairy -C trace -c '{"args":["invoke","get","dairy102"]}'

docker exec cli peer chaincode invoke -n dairy -C trace -c '{"args":["invoke","set","dairy101","info103"]}' --peerAddresses peer0.orgdairy.trace.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer0.orgdairy.trace.com/tls/ca.crt -o orderer1.trace.com:8050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/trace.com/orderers/orderer1.trace.com/msp/tlscacerts/tlsca.trace.com-cert.pem
sleep 5

docker exec -e CORE_PEER_ADDRESS=peer1.orgdairy.trace.com:8051 -e CORE_PEER_LOCALMSPID=OrgDairyMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/peers/peer1.orgdairy.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgdairy.trace.com/users/[email protected]/msp cli peer chaincode query -n dairy -C trace -c '{"args":["invoke","history","dairy101"]}'

执行

chmod +x dairy_test.sh
./dairy_test.sh

(9)调用加工厂process链码

创建process_test.sh内容如下

#!/bin/bash

docker exec cli peer chaincode invoke -n process -C trace -c '{"args":["invoke","set","process101","dairy101"]}' --peerAddresses peer0.orgprocess.trace.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer0.orgprocess.trace.com/tls/ca.crt -o orderer1.trace.com:8050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/trace.com/orderers/orderer1.trace.com/msp/tlscacerts/tlsca.trace.com-cert.pem
sleep 5

docker exec -e CORE_PEER_ADDRESS=peer1.orgprocess.trace.com:10051 -e CORE_PEER_LOCALMSPID=OrgProcessMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer1.orgprocess.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer1.orgprocess.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer1.orgprocess.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/users/[email protected]/msp cli peer chaincode query -n process -C trace -c '{"args":["invoke","get","process101"]}'

docker exec cli peer chaincode invoke -n process -C trace -c '{"args":["invoke","set","process101","dairy102"]}' --peerAddresses peer0.orgprocess.trace.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer0.orgprocess.trace.com/tls/ca.crt -o orderer1.trace.com:8050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/trace.com/orderers/orderer1.trace.com/msp/tlscacerts/tlsca.trace.com-cert.pem
sleep 5

docker exec -e CORE_PEER_ADDRESS=peer1.orgprocess.trace.com:10051 -e CORE_PEER_LOCALMSPID=OrgProcessMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer1.orgprocess.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer1.orgprocess.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/peers/peer1.orgprocess.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgprocess.trace.com/users/[email protected]/msp cli peer chaincode query -n process -C trace -c '{"args":["invoke","history","process101"]}'

执行

chmod +x process_test.sh
./process_test.sh

(10)调用销售组织sell链码

创建sell_test.sh内容如下

#!/bin/bash

docker exec cli peer chaincode invoke -n sell -C trace -c '{"args":["invoke","set","sell101","process101"]}' --peerAddresses peer0.orgsell.trace.com:11051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer0.orgsell.trace.com/tls/ca.crt -o orderer1.trace.com:8050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/trace.com/orderers/orderer1.trace.com/msp/tlscacerts/tlsca.trace.com-cert.pem
sleep 5

docker exec -e CORE_PEER_ADDRESS=peer1.orgsell.trace.com:12051 -e CORE_PEER_LOCALMSPID=OrgSellMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer1.orgsell.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer1.orgsell.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer1.orgsell.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/users/[email protected]/msp cli peer chaincode query -n sell -C trace -c '{"args":["invoke","get","sell101"]}'

docker exec -e CORE_PEER_ADDRESS=peer1.orgsell.trace.com:12051 -e CORE_PEER_LOCALMSPID=OrgSellMSP -e CORE_PEER_TLS_ENABLED=true -e CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer1.orgsell.trace.com/tls/server.crt -e CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer1.orgsell.trace.com/tls/server.key -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/peers/peer1.orgsell.trace.com/tls/ca.crt -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgsell.trace.com/users/[email protected]/msp cli peer chaincode query -n sell -C trace -c '{"args":["invoke","history","sell101"]}' --connTimeout 60s

执行

chmod +x sell_test.sh
./sell_test.sh

 

你可能感兴趣的:(Hyperledger fabric+kafka+docker-compose多机部署)