【区块链100例】(4) 编写脚本快速搭建超级账本 Fabric
【摘要】
目录
fabric网络搭建执行顺序1.下载镜像2. 可执行文件3. crypto-config.yaml4.生成证书5. configtx.yaml6. 创世块和通道文件7. docker-com...
fabric网络搭建执行顺序
手动执行都是环境部分的创建!
1.下载镜像
#!/bin/bash
echo "第一步:下载镜像开始..."
./download-dockerimages.sh
echo "下载镜像完成"
- 1
- 2
- 3
- 4
2. 可执行文件
手动:拷贝bin目录下的可执行文件到 gopath/bin/
这个在上一章中已经有了详细的操作了!
传送门 -----》
3. crypto-config.yaml
手动:拷贝crypto-config.yaml到shell脚本同级目录
4.生成证书
生成证书,结果生成crypto-config路径
echo "第二步:生成证书开始..."
cryptogen generate --config ./crypto-config.yaml
echo "生成证书结束"
- 1
- 2
- 3
5. configtx.yaml
手动:拷贝configtx.yaml到shell脚本同级目录
6. 创世块和通道文件
生成创世块和通道文件,要求生成文件到channel-artifacts目录
echo "第三步:生成创世块开始..."
mkdir channel-artifacts
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
echo "第三步:生成创世块完成..."
- 1
- 2
- 3
- 4
echo "第三步:生成通道开始..."
configtxgen -profile TwoOrgsChannel -channelID fanonechannel -outputCreateChannelTx ./channel-artifacts/channel.tx
echo "第三步:生成通道结束..."
- 1
- 2
- 3
7. docker-compose-cli.yaml
手动:拷贝docker-compose-cli.yaml到shell脚本同级目录
8. 链码
手动:拷贝链码,chaincode/go/
9. 启动容器
启动docker-compose启动peer,orderer,cli等容器
echo "第四步:启动docker-compose开始..."
docker-compose -f ./docker-compose-cli.yaml up -d
echo "启动docker-compose结束"
- 1
- 2
- 3
启动docker-compose需要点时间,因为启动后需要缓冲时间,给5秒即可。
sleep 5
- 1
10.进入cli容器
11.创建通道
echo "第六步:创建通道开始..."
docker exec cli peer channel create -o orderer.example.com:7050 -c fanonechannel -f /opt/gopath/src/github.com/hyperledger/fabric/peer/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
echo "创建通道结束"
- 1
- 2
- 3
12. peer节点加入通道
echo "第七步:当前peer节点加入通道开始..."
docker exec cli peer channel join -b fanonechannel.block
echo "当前peer节点加入结束"
- 1
- 2
- 3
13. 安装链码
echo "第八步:链码安装开始..."
docker exec cli peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go
echo "链码安装结束"
- 1
- 2
- 3
14. 实例化链码
echo "第九步:链码初始化开始..."
docker exec cli peer chaincode instantiate -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","a","200","b","300"]}' -n mycc -v 1.0 -P "OR ('Org1MSP.member','Org2MSP.member')" --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
echo "链码初始化结束"
- 1
- 2
- 3
代码整合
启动
start.sh
#!/bin/bash
echo "第一步:下载镜像开始..."
./download-dockerimages.sh
echo "下载镜像完成"
echo "第二步:生成证书开始..."
cryptogen generate --config ./crypto-config.yaml
echo "生成证书结束"
echo "第三步:生成创世块及通道开始..."
mkdir channel-artifacts
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
configtxgen -profile TwoOrgsChannel -channelID zlktchannel -outputCreateChannelTx ./channel-artifacts/channel.tx
echo "生成创世块及通道完成"
echo "第四步:启动docker-compose开始..."
docker-compose -f ./docker-compose-cli.yaml up -d
echo "启动docker-compose结束"
# 启动docker-compose需要点时间,给10秒
sleep 5
echo "第六步:创建通道开始..."
docker exec cli peer channel create -o orderer.example.com:7050 -c fanonechannel -f /opt/gopath/src/github.com/hyperledger/fabric/peer/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
echo "创建通道结束"
echo "第七步:当前peer节点加入通道开始..."
docker exec cli peer channel join -b fanonechannel.block
echo "当前peer节点加入结束"
echo "第八步:链码安装开始..."
docker exec cli peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go
echo "链码安装结束"
echo "第九步:链码初始化开始..."
docker exec cli peer chaincode instantiate -o orderer.example.com:7050 -C fanonechannel -c '{"Args":["init","a","200","b","300"]}' -n mycc -v 1.0 -P "OR ('Org1MSP.member','Org2MSP.member')" --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
echo "链码初始化结束"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
停止
stop.sh
#!/bin/bash
# 删除链码容器
function RmCcContainers() {
contain_ids=$(docker ps -a |awk '($2 ~ /dev-*/){print $1}')
if [ -z "$contain_ids" -o "$contain_ids" == " " ]; then
echo "---- 没有可删除的容器 ----"
else
docker rm -f $contain_ids
fi
}
# 删除链码镜像
function RmCcImages() {
images_ids=$(docker images |awk '($1 ~ /dev-*/){print $3}')
if [ -z "$images_ids" -o "$images_ids" == " " ]; then
echo "===== 没有可删除的链码镜像 ====="
else
docker rmi -f $images_ids
fi
}
echo "===== 关闭fabric网络 ====="
# 先停docker-compose
docker-compose -f ./docker-compose-cli.yaml down -v
# 调用函数删除链码容器
RmCcContainers
# 调用函数删除链码镜像
RmCcImages
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
文章来源: blog.csdn.net,作者:小生凡一,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_45304503/article/details/120573742
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)