Fabric手动部署first-network
Posted sanqima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fabric手动部署first-network相关的知识,希望对你有一定的参考价值。
在Fabric v1.4~v2.3版本里,有个first-network案例,运行first-network/byfn.sh脚本就可以测试当前fabric 单机网络是否安装成功。这里,使用手动部署的方式,来取代自动化部署。
软件环境如下:
fabric-samples v1.4.3
fabric v1.4.3
fabric ca v1.4.3
Ubuntu 16.04
1、备份first-network,然后精简first-network
a)备份first-network
cd $GOPATH/src/github.com/hyperledger/fabric-samples
tar -czvf first-network_0803_bak.tar.gz first-network
b)精简first-network
去掉不相干的.yaml和文件夹,精简之后的first-network,如下所示:
2、用工具生成证书、创世区块、通道配置、锚节点文件
2.1 制作身份证书
使用 cryptogen 工具将文件 crypto-config.yaml 作为参数配置生成组织结构与身份证书
cd ./fabric-sample/first-network
../bin/cryptogen generate --config=./crypto-config.yaml
2.2 生成创世区块
## 使能FABRIC_CFG_PATH 变量
export FABRIC_CFG_PATH=$PWD
## 生成创世区块
../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2.3 生成通道配置文件
创建一个mychannel的通道,并生成通道配置文件channel.tx,其保存在first-network/channel-artifacts目录下。
export CHANNEL_NAME=mychannel && ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
2.4 生成2个组织的锚节点文件
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
3、启动分布式网络
使用 docker-compose 工具将 docker-compose-cli.yaml 文件作为参数,启动提供网络服务的各个节点
cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network
docker-compose -f docker-compose-cli.yaml up -d
4、创建通道,把节点加入通道
4.1 创建通道
a) 进入cli容器
docker exec -it cli bash
b) 设置环境变量
export CHANNEL_NAME=mychannel
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
c) 根据环境变量,来创建通道
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile $ORDERER_CA
4.2 加入通道
a) peer0.org1加入通道
由于cli 容器默认为peer0.org1,所以可以直接加入通道。
peer channel join -b mychannel.block
b) peer1.org1加入通道
peer1.org1与peer0.org1只有peer的地址不相同,修改即可,然后加入通道。
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
peer channel join -b mychannel.block
c)peer0.org2 加入通道
由于peer0.org2是在org2里,当前cli是org1,所以需要切换peer参数。
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_LOCALMSPID="Org2MSP"
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
peer channel join -b mychannel.block
d) peer1.org2加入通道
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
peer channel join -b mychannel.block
5、更新锚节点,并安装链码
每个组织里只有一个锚节点,它负责跨组织通信。现在有2个组织:org1、org2,所以只需要为这2个组织分别设置一锚节点即可。
5.1 将peer0.org1 更新为org1里的锚节点,并安装链码
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
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
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $ORDERER_CA
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
5.2 将peer0.org2 更新为org2里的锚节点,并安装链码
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_LOCALMSPID="Org2MSP"
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
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile $ORDERER_CA
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
6、实例化链码,并测试
a) 实例化
peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"
b) 查询
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
c) 调用
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc --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:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
效果如下:
d) 在peer1.org1上也安装链码,并测试
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
查询a的值
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
效果如下:
7、关闭和清空fabric网络
使用指定的 docker-compose-cli.yaml 配置文件关闭网络:
## 关闭网络
sudo docker-compose -f docker-compose-cli.yaml down
## 清空网络
sudo docker-compose -f docker-compose-cli.yaml down --volumes
清空channel-artifacts文件夹,只保留空文件夹;删除整个crypto-config文件。
sudo rm -r channel-artifacts crypto-config
以上是关于Fabric手动部署first-network的主要内容,如果未能解决你的问题,请参考以下文章
Hyperledger Fabric first-network的初始化启动流程(二)
Hyperledger Fabric 1.1安装部署-chaincode测试