fabric2.4私网搭建

  1. 生成默认的crypto-config.yaml文件

cryptogen showtemplate > crypto-config.yaml

  1. 修改crypto-config.yaml文件的配置

已修改过的,配置4个peer,1个orderer的文件见与本文档一起打包的crypto-config.yaml

  1. 生成所有的证书文件

cryptogen generate --config=crypto-config.yaml

会生成一个文件夹crypto-config

  1. 使用configtx.yaml创建通道

以fabric-samples/test-network/configtx/configtx.yaml为例进行修改,得到自己定制网络的configtx.yaml,修改后的configtx.yaml见文件夹

  1. 使用官方的configtxgen工具进行通道配置

(1)输出初始块:configtxgen -outputBlock ./channel-artifacts/genesis.block -profile SupplyOrdererGenesis(与configtx.yaml中profile下的一致) -channelID fabric-channel

生成创世块时的channelID要与后面的不同

(2)生成通道文件:configtxgen -outputCreateChannelTx ./channel-artifacts/channel.tx -profile SupplyChannel(与configtx.yaml中profile下的一致) -channelID mychannel

得到channel-artifacts文件夹

  1. 部署节点

使用docker容器启动peer节点和orderer节点,docker配置文件例子:fabric-samples/test-network/docker/docker-compose-test-net.yaml

修改后适用于自己的docker-compose.yaml见文件夹

  1. 在docker-compose.yaml文件所在的文件夹下启动节点:

docker-compose up -d(在后端执行)

会生成一个chaincode/go的文件夹,存链码

  1. 清除之前docker挂载内容:docker volume prune
  2. 通过cli1进入peer节点容器中:docker exec -it cli1 bash
  3. 在cli1中创建通道:

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

会生成mychannel.block文件

  1. 将cli1中的mychannel.block文件拷贝到本地文件夹中:

docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./

  1. 将本地的mychannel.block文件拷贝到其他cli peer的容器中:

docker cp ./mychannel.block cli2:/opt/gopath/src/github.com/hyperledger/fabric/peer

  1. 将peer节点加入通道

从本地切换到docker cli:docker exec -it cli1 bash

在cli1中将peer节点加入通道:peer channel join -b mychannel.block

其他cli2 cli3 cli4同理加入通道中,不再赘述。

  1. 打包链码

从docker cli切换到本地:链码文件放在chaincode/go中:例:fabric-samples/chaincode/sacc/sacc.go

切换到docker cil,进入链码所在的路径:

cd /opt/gopath/src/github.com/hyperledger/fabric-cluster/chaincode/go

          打包链码前准备工作:

go env -w GOPROXY=https://goproxy.cn,direct

go mod init(创建go.mod文件)

go mod vendor(依赖)

切换工作目录:cd /opt/gopath/src/github.com/hyperledger/fabric/peer

打包链码:peer lifecycle chaincode package sacc.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric-cluster/chaincode/go --label sacc_1

  1. 将cli1中的链码copy到本地:

切换到本地目录,然后copy:

docker cp cli1: /opt/gopath/src/github.com/hyperledger/fabric/peer/sacc.tar.gz ./

  1. 将本地sacc.tar.gz复制到cli2,cli3,cli4

docker cp sacc.tar.gz cli2:/opt/gopath/src/github.com/hyperledger/fabric/peer

cli3,cli4同理

  1. 切换到cli1中安装链码:peer lifecycle chaincode install sacc.tar.gz

cli2,cli3,cli4安装链码同理

  1. 在cli1中批准链码

peer lifecycle chaincode approveformyorg --channelID mychannel --name sacc --version 1.0 --init-required --package-id sacc_1:28225b3579fb9528bdd974e7d3fe9fb9aa51533c86cd48540140b482b9fa1032 --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

cli2,cli3,cli4同理批准链码

  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

cli2,cli3,cli4同理检查是否所有成员批准链码.

  1. 在cli1中提交链码

peer lifecycle chaincode commit -o orderer.example.com:7050 --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 --peerAddresses peer0.deliverer.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/deliverer.example.com/peers/peer0.deliverer.example.com/tls/ca.crt --peerAddresses peer0.manufacturer.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/manufacturer.example.com/peers/peer0.manufacturer.example.com/tls/ca.crt --peerAddresses peer0.producer.example.com:11555 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/producer.example.com/peers/peer0.producer.example.com/tls/ca.crt --peerAddresses peer0.retailer.example.com:10051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/retailer.example.com/peers/peer0.retailer.example.com/tls/ca.crt

在cli1中提交链码即可,不需要在cli2,cli3,cli4中再次执行提交操作

  1. 在cli1中调用链码

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 --channelID mychannel --name sacc --peerAddresses peer0.deliverer.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/deliverer.example.com/peers/peer0.deliverer.example.com/tls/ca.crt --peerAddresses peer0.manufacturer.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/manufacturer.example.com/peers/peer0.manufacturer.example.com/tls/ca.crt --peerAddresses peer0.producer.example.com:11555 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/producer.example.com/peers/peer0.producer.example.com/tls/ca.crt --peerAddresses peer0.retailer.example.com:10051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/retailer.example.com/peers/peer0.retailer.example.com/tls/ca.crt -c '{"Args":["a","bb"]}'

  1. 在cli2中查询a的值

peer chaincode query -C mychannel -n sacc -c '{"Args":["query","a"]}'

  1. 在cli2中调用链码修改a的值

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 --channelID mychannel --name sacc --peerAddresses peer0.deliverer.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/deliverer.example.com/peers/peer0.deliverer.example.com/tls/ca.crt --peerAddresses peer0.manufacturer.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/manufacturer.example.com/peers/peer0.manufacturer.example.com/tls/ca.crt --peerAddresses peer0.producer.example.com:11555 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/producer.example.com/peers/peer0.producer.example.com/tls/ca.crt --peerAddresses peer0.retailer.example.com:10051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/retailer.example.com/peers/peer0.retailer.example.com/tls/ca.crt -c '{"Args":["set","a","cc"]}'

  1. 在cli3中查看修改后a的值

peer chaincode query -C mychannel -n sacc -c '{"Args":["query","a"]}'

至此,基础网络搭建完成。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值