Fabric应用实战(二):启动过程详解

简述

简单介绍一下上一章节Fabric应用实战(一) 单机环境搭建 最后官网demo启动的过程

本小节 我们根据在启动过程中的控制台输出来按照启动顺序讲解Fabric的启动过程, 总共是3个文件

byfn.sh , /scripts/script.sh , /scripts/utils.sh

1、生成证书

使用cryptogen这个二进制文件根据crypto-config.yaml 来为各个节点生成证书文件并保存在crypto-config目录下。

1.1证书配置文件

我们在将crypto-config.yaml 的官方注释删除时候剩余下面这些,最后添加一些自己的注释上去方便大家理解

##order组织
OrdererOrgs:
  - Name: Orderer ##组织名称
    Domain: example.com ##组织域
    Specs:
      - Hostname: orderer
#peer组织
PeerOrgs:
  - Name: Org1 ##组织名称
    Domain: org1.example.com
    EnableNodeOUs: true
    Template: ## 节点个数 节点名称默认peer0开始递增
      Count: 2
    Users: ##用户
      Count: 1 ## 除了创建一个普通用户还会默认创建一个admin用户
  - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1

这个文件里面有2个类型的组织:OrdererOrgs(Order组织),PeerOrgs (Peer组织), 在这2个组织类型下可以分别创建一个或者多个order和peer组织,其中Template表示节点个数,节点名称默认从peer0开始递增,Users:配置组织的用户,除了会创建Count个用户之外还会默认创建一个admin用户。

1.2证书生成过程

##执行生成证书命令
#1、执行以下命令
cryptogen generate --config=./crypto-config.yaml
#2、看到如下输出
org1.example.com
org2.example.com
#3、在crypto-config 目录下看到下面这2个目录则表示成功了
ordererOrganizations  peerOrganizations


2、生成创世区块,通道,锚节点

2.1 生成创世区块

使用configtxgen这个二进制文件来生成创世区块

#1、执行下面的命令
configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
#2、在channel-artifacts 目录下面看到genesis.block 这个创世区块文件表示成功
genesis.block

2.2生成通道信息

#1、执行如下命令
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
#2、channel-artifacts下面看到channel.tx 配置文件就是成功了
channel.tx  genesis.block

2.3生成锚节点

#1、Org1MSP 
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
#2、Org2MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
#3、channel-artifacts目录下生成Org1MSPanchors.tx  Org2MSPanchors.tx 文件
channel.tx  genesis.block  Org1MSPanchors.tx  Org2MSPanchors.tx

3启动网络

3.1启动容器

  3.1.1 使用 leveldb 作为数据库启动容器

[root@localhost first-network]#  docker-compose -f docker-compose-cli.yaml up -d
Creating network "net_byfn" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer1.org1.example.com ... done
Creating peer1.org2.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
[root@localhost first-network]# sudo docker ps 
CONTAINER ID        IMAGE                               COMMAND             CREATED             STATUS              PORTS                                              NAMES
566884c8973a        hyperledger/fabric-tools:latest     "/bin/bash"         7 seconds ago       Up 6 seconds                                                           cli
ba33f071c344        hyperledger/fabric-peer:latest      "peer node start"   9 seconds ago       Up 7 seconds        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
4c21649be9df        hyperledger/fabric-peer:latest      "peer node start"   9 seconds ago       Up 6 seconds        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
d886fec75c58        hyperledger/fabric-peer:latest      "peer node start"   9 seconds ago       Up 6 seconds        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
e3f8409316e9        hyperledger/fabric-peer:latest      "peer node start"   9 seconds ago       Up 7 seconds        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
2392178cf781        hyperledger/fabric-orderer:latest   "orderer"           9 seconds ago       Up 7 seconds        0.0.0.0:7050->7050/tcp                             orderer.example.com
[root@localhost first-network]# 

3.1.2 使用couchdb 作为数据库启动容器 ,可以看到每个peer都有对应的couchdb数据库

[root@localhost first-network]# docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml up -d
Creating network "net_byfn" with the default driver
Creating couchdb0               ... done
Creating couchdb2               ... done
Creating orderer.example.com    ... done
Creating couchdb1            ... done
Creating couchdb3               ... done
Creating peer1.org1.example.com ... done
Creating peer1.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
[root@localhost first-network]# sudo docker ps 
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS              PORTS                                              NAMES
9e61a26289f8        hyperledger/fabric-tools:latest     "/bin/bash"              6 seconds ago       Up 3 seconds                                                           cli
d92f8ae42825        hyperledger/fabric-peer:latest      "peer node start"        10 seconds ago      Up 5 seconds        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
f747b9ca461a        hyperledger/fabric-peer:latest      "peer node start"        10 seconds ago      Up 6 seconds        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
8e561b0b158b        hyperledger/fabric-peer:latest      "peer node start"        11 seconds ago      Up 5 seconds        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
91b06b79f4d2        hyperledger/fabric-peer:latest      "peer node start"        11 seconds ago      Up 6 seconds        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
657e8cf0f287        hyperledger/fabric-couchdb          "tini -- /docker-ent鈥   13 seconds ago      Up 10 seconds       4369/tcp, 9100/tcp, 0.0.0.0:8984->5984/tcp         couchdb3
9f7543129c05        hyperledger/fabric-couchdb          "tini -- /docker-ent鈥   13 seconds ago      Up 10 seconds       4369/tcp, 9100/tcp, 0.0.0.0:6984->5984/tcp         couchdb1
ccf4d6bfa5cc        hyperledger/fabric-orderer:latest   "orderer"                13 seconds ago      Up 9 seconds        0.0.0.0:7050->7050/tcp                             orderer.example.com
1d6a61578bba        hyperledger/fabric-couchdb          "tini -- /docker-ent鈥   13 seconds ago      Up 10 seconds       4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp         couchdb0
490736a3b6bf        hyperledger/fabric-couchdb          "tini -- /docker-ent鈥   13 seconds ago      Up 9 seconds        4369/tcp, 9100/tcp, 0.0.0.0:7984->5984/tcp         couchdb2
[root@localhost first-network]# 

3.2创建通道

进入cli容器内部创建通道,创建通道需要order证书

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/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

命令执行完成之后会在当前目录下生成一个mychannel.block 文件

3.3加入通道

将所有peer节点都加入到通道中

##cli 默认是Org1的 peer0节点
 peer channel join -b mychannel.block
##切换到org1的peer1
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
peer channel join -b mychannel.block   
##切换到Org2的环境
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
##peer0.org2
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
peer channel join -b mychannel.block 
## peer1.org2
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
peer channel join -b mychannel.block 

3.4更新锚节点

#重新进入容器
sudo docker exec -it cli bash
#更新环境变量
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
#========Org1================
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile $ORDERER_CA
#========Org2================
#更新环境变量
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA
#退出容器
exit

3.5安装链码

#链码的安装仍然需要在所有节点上进行操作
#进入容器
sudo docker exec -it cli bash
#更新环境变量
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
#=========peer0.org1=========== 
##链码路径为/opt/gopath/src/github.com/chaincode/chaincode_example02/go
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go
#实例化链码 该步骤创建了a,b两个账户,其中a账户余额定义为100,b账户余额定义为200
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR      ('Org1MSP.member','Org2MSP.member')"
#切换到Org2安装链码
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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go

3.6调用

#以peer0.org1为例
#首先进入cli容器
sudo docker exec -it cli bash
#执行以下命令进行查询a账户余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
#如果命令行输出100说明链码成功调用.

#接下来我们发起一笔交易:通过peer0.org1节点将a账户余额转账给b20
peer chaincode invoke -o orderer.example.com:7050  --tls true --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
#然后登陆peer1.org1节点进行查询
CORE_PEER_ADDRESS=peer1.org1.example.com:8051 
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
#如果输出结果为:80
说明Fabric网络手动搭建成功
#退出容器
exit
扫描二维码关注公众号,回复: 11607554 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_24739007/article/details/108360641