您的位置:首页 > 其它

超级账本Fabric 1.0 多节点集群的部署(3)

2017-07-06 00:00 645 查看


题图摄于美国加州17英里:Lone Cypress

上两期文章阐述了超级账本
Fabric 1.0多节点部署概述以及多节点集群部署的准备工作(可点击),得到广大读者的关注,今天发布连载之三,介绍搭建
Fabric 1.0 集群的具体配置和chaincode管理的步骤。因为是手动部署,因此步骤比较多。有需要的读者可在文后下载3期合并的PDF版本。

( 接上期,部署配置中大量使用了 docker 和 docker compose ,建议不熟悉的读者先补充相关知识。)

2.3 多节点 Fabric 的配置

以下各VM的工作目录为: 

$GOPATH/src/github.com/hyperledger/fabric/examples/e2e_cli

可在任意VM上运行以下命令,生成构建 Fabric 网络所需的成员证书等必要材料:

bash generateArtifacts.sh

该命令只需在某个VM上运行一次,其他VM上就不需要运行。

在运行该命令的VM中会生成 channel-artifacts 和 crypto-config 目录,需要把它们拷贝到其他VM的 e2e_cli 目录下,如果在VM中已经存在该目录,则先把目录删除。当每个VM中都有统一的 channel-artifacts 和 crypto-config 目录后接下来就开始配置 compose 文件。

 

I. VM1的配置:

1.  修改 /etc/hosts 的映射关系

因为容器内部通过域名的方式访问 orderer , 因此需要通过修改 /etc/hosts 把orderer 的域名和 ip 地址对应起来,在文件中添加:

10.112.122.69   orderer.example.com

 

2.  修改docker-compose-cli.yaml

在默认的情况下,docker-compose-cli.yaml会启动6个service(容器),它们分别为
peer0.org1.example.com、 peer1.org1.example.com、 peer0.org2.example.com、 peer1.org2.example.com、 orderer.example.com 和 cli,因为每台机器只运行与之对应的一个节点,因此需要注释掉无需启动的
service。

(1) 除 peer0.org1.example.com 和 cli service 外,其他 service 全部注释。

(2) 在 cli 的 volumes 中加入映射关系:

-./peer/:/opt/gopath/src/github.com/hyperledger/fabric/peer/

-/etc/hosts:/etc/hosts

  

(3) 注释 cli 中的 depends_on 和 command :

   depends_on:

      #- orderer.example.com

      - peer0.org1.example.com

      #- peer1.org1.example.com

      #- peer0.org2.example.com

      #- peer1.org2.example.com



      #command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'

 

之前我们把容器中的工作目录挂载到宿主机的 e2e_cli/peer 目录下, 是因为在执行 create channel 的过程中,orderer 会返回一个 mychannel.block 作为 peer 加入 channel 的依据,其他的 peer 要加入到相同的 channel 中必须先获取该 mychannel.block 文件。因此,通过挂载目录从宿主机就能方便获得 mychannel.block ,并且把它传输到其他的 VM 上。

挂载 /etc/hosts 的目的是把主机中 orderer.exmaple.com 与 IP 地址10.112.122.69 的映射关系带入容器中,目的是让 cli 能通过域名访问 orderer  。在实际环境中,建议通过配置 DNS 而不是修改 /etc/hosts 文件(下同)。

3.  修改 base/peer-base.yaml,添加 volumes:

volumes:

-/etc/hosts:/etc/hosts

 

这样 peer 容器能通过域名访问orderer了。

II. VM2配置:

1. 修改 /etc/hosts 的映射关系

peer1.org1.example.com 使用了 peer0.org1.example.com 作为它的初始化节点,因此需要在主机中还需要加入 VM1 的 ip 地址。

10.112.122.69   orderer.example.com

10.112.122.144  peer0.org1.example.com

 

2. 修改docker-compose-cli.yaml

(1) 类似VM1,除 peer1.org1.example.com 和 cli service 外,其他 service 全部注释。

(2) 在 cli 的 volumes 中加入映射关系:

-./peer/:/opt/gopath/src/github.com/hyperledger/fabric/peer/

-/etc/hosts:/etc/hosts

 

(3) 注释cli中的 depends_on 和 command:

depends_on:

 #- orderer.example.com

 #- peer0.org1.example.com

 - peer1.org1.example.com

 #- peer0.org2.example.com

 #- peer1.org2.example.com

          

#command:/bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'

     

(4) 修改cli中的环境变量

CORE_PEER_ADDRESS=peer1.org1.example.com:7051

 

3. 修改base/peer-base.yaml,同VM1的修改。

 

III. VM3配置:

1. 修改 /etc/hosts 的映射关系

 10.112.122.69      orderer.example.com

 

2. 修改docker-compose-cli.yaml

(1) VM3 上运行 peer2 节点,因此除 peer0.org2.example.com 和 cli service 外,其他 service 全部注释。

(2) 在cli的 volumes 中加入映射关系:

- ./peer/:/opt/gopath/src/github.com/hyperledger/fabric/peer/

-/etc/h
f201
osts:/etc/hosts


 

(3) 注释cli中的 depends_on 和 command :

depends_on:

 #- orderer.example.com

 #- peer0.org1.example.com

 #- peer1.org1.example.com

 - peer0.org2.example.com

 #- peer1.org2.example.com

#command:/bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'

         

(4) 修改cli中的环境变量

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_ADDRESS=peer0.org2.example.com:7051

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/Admin@org2.example.com/msp

 

3. 修改base/peer-base.yaml,同VM1。

 

IV. VM4配置:

1. 修改/etc/hosts的映射关系

peer1.org2.example.com 使用了 peer0.org2.example.com 作为它的初始化节点,因此需要在映射关系中加入 VM3 的 ip 地址

10.112.122.69       orderer.example.com

10.112.122.12       peer0.org2.example.com

 

2. 修改docker-compose-cli.yaml

(1) VM4运行peer3,因此除peer1.org2.example.com和cliservice 外,其他service全部注释

(2) 在cli的volumes中加入映射关系:

-./peer/:/opt/gopath/src/github.com/hyperledger/fabric/peer/

-/etc/hosts:/etc/hosts

 

(3) 修改cli中的 depends_on 和 command:

depends_on:

  - peer1.org2.example.com

#command:/bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'

(4) 修改cli中的环境变量

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_ADDRESS=peer1.org2.example.com:7051

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/Admin@org2.example.com/msp

 

3. 修改base/peer-base.yaml,同VM1。

 

V. VM5配置如下:

1. 修改docker-compose-cli.yaml

除orderer外的其他 service 全部注释,即只启动 orderer 。

 

2.4 启动多节点Fabric集群

1.启动orderer

进入到 VM5 的 fabric/examples/e2e_cli 目录下,运行

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

此时终端会出现大量记录,当出现Beginning to service requests时,orderer启动完成。有了 orderer 之后,就可以通过它来管理 channel 。             

 

2.启动 org1的第一个节点 peer0 ,即 peer0.org1.example.com

进入到 VM1 的 fabric/examples/e2e_cli 目录下,运行

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

此时通过docker ps -a 命令可以看到成功启动了 peer0.org1.example.com 和 cli 两个容器。

接下来实现创建 channel 、加入 channel 和安装 chanicode 。首先进入到cli容器内:

docker exec -it cli bash

 

cli 与 orderer 之间的通讯使用 tls 加密,设置环境变量 ORDERER_CA 以作建立握手的凭证:

$ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem

注:以下所有涉及到 ORDERER_CA 环境变量的命令都需预先给该变量赋值。

进入到 cli 容器后会自动跳转到 /opt/gopath/src/github.com/hyperledger/fabric/peer 目录,即工作目录,通过compose文件的配置,该目录映射为宿主机的 /e2e_cli/peer 。

在工作目录下输入以下命令,创建名为 mychannel 的 channel :

peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls --cafile $ORDERER_CA

channel 创建成功后,会在当前目录下生成mychannel.block文件。每个peer 在向 orderer 发送 join channel 交易的时候,需要提供这个文件才能加入到 mychannel 中,因此运行在其他 VM 上的 peer 需要得到 mychannel.block 文件来加入到 mychannel 中。由于之前的文件映射关系, mychannel.block 文件可在宿主机的 e2e_cli/peer 目录下获取,这时可以通过宿主机把 mychannel.block 拷贝到
VM2, VM3, VM4的 e2e_cli/peer 目录下。

把 peer0.org1.example.com 加入到 mychannel 中:

peer channel join -b mychannel.block

更新 mychannel 中 org1 的 anchor peer 的信息:

peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile $ORDERER_CA            

安装 chaincode 示例 chaincode_example02 到 peer0.org1.example.com 中:

peer chaincode install -nmycc -v 1.0 -p \

github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

这时候 chaincode 代码已经安装到了 peer0 节点上,但并未实例化运行。接下来先配置好其他节点。

 

3. 启动 org1 的第二个节点 peer1,即 peer1.org1.example.com

进入到VM2的 fabric/examples/e2e_cli 目录下,运行

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

进入到 cli 容器内部:

docker exec -it cli bash

由于前面已经把 mychannel.block 拷贝到了 VM2 的 e2e_cli/peer 目录下,因此 mychannel.block 可通过容器内的
/opt/gopath/src/github.com/hyperledger/fabric/peer
目录访问。

把 peer1.org1.example.com 加入到 mychannel 中:

peer channel join -b mychannel.block

             

安装 chaincode_example02 到 peer1.org1.example.com 中:

peer chaincode install -nmycc -v 1.0 –p \

github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

 

4. 启动 org2 的第一个节点 peer2,即 peer0.org2.example.com

进入到 VM3 的 fabric/examples/e2e_cli 目录下,运行

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

 

进入到cli容器内部:

docker exec -it cli bash

 

把peer0.org2.example.com加入到mychannel中:

peer channel join -b mychannel.block

更新 mychannel 中 org2 的 anchor peer 的信息:

peer channel update -oorderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx [b]--tls --cafile
$ORDERER_CA   
[/b]

 

安装 chaincode_example02 到 peer0.org2.example.com 中:

[b]peer chaincode install -nmycc -v 1.0 -p \[/b]

[b]github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02[/b]

 

5. 启动org2的第二个节点 peer3 ,即启动 peer1.org2.example.com

进入到 VM4 的 fabric/examples/e2e_cli 目录下,运行

[b]docker-compose-f docker-compose-cli.yaml up -d[/b]

首先进入到cli容器内部:

[b]docker exec -it cli bash[/b]

 

把 peer1.org2.example.com 加入到 mychannel 中:

[b]peer channel join -b mychannel.block[/b]

             

安装 chaincode_example02 到 peer1.org2.example.com 中:

[b]peer chaincode install -nmycc -v 1.0 -p \[/b]

[b]github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02[/b]

2.5 运行chaincode

通过前面的步骤,整个 多节点Fabric 网络已经运行起来了,每个peer都加入到了标识为 mychannel 的 channel 中,并且都安装了一个简单的 chaincode (该 chaincode 在安装时被标识为 mycc ) 。下面步骤运行和维护 chaincode。

1. 实例化chaincode

chaincode 的实例化可在任意 peer 上进行,并且 chaincode 只能被实例化一次,下面以在 peer0.org2.example.com 上实例化 chaincode 为例。

首先登录VM3并进入到cli容器内部运行:

[b]peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel-nmycc -v 1.0 -c
'{"Args":["init","a","100","b","200"]}' -P "OR     ('Org1MSP.member','Org2MSP.member')"
[/b]

这时候会构建一个新的容器来运行chaincode,通过docker ps -a 命令可以看到新容器:

[b]dev-peer0.org2.example.com-mycc-1.0[/b]

 

上述实例化中,我们对两个变量‘a’和‘b’分别赋予初值100和200,通过 channel 它们的值被同步到了其他peer的账本上,即使其他peer还没有构建运行 chaincode 的容器。

 

2.    执行 chaincode 的 query 交易

由于 chaincode 已经被 peer0.org2.example.com 实例化了,因此其他 peer 不需要再次实例化它了,但是 chaincode 的状态(world state)却是已经记录在各个peer的账本上的。

 接下来我们在peer0.org1.example.com上查看chaincode的状态,登录到VM1上并进入cli容器内部执行:

[b]peer chaincode query -C mychannel -nmycc -c '{"Args":["query","a"]}'[/b]

上面的命令查看 mycc 中变量 a 的值,由于在 peer 跟 chaincode 发生互动之前还不存在运行 chaincode 的容器,因此第一次交互的时候需要先构建运行 chaincode 的容器,等待一段时间后返回结果:100 。

此时通过 docker ps -a 命令能看到新容器:

[b]dev-peer0.org1.example.com-mycc-1.0[/b]

该值与实例化时的赋值一致,说明 peer0.org1 和 peer0.org2 两个 peer 可以相互通信。

 

3. 执行chaincode的invoke交易

接下来,我们执行一个 invoke 交易,使得变量 a 向变量 b 转帐 20,得到最终值为["a":"80","b":"220"]。

登录到VM2并进入到cli容器中中通过以下命令查询mycc的状态:

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

稍作等待后返回结果为100,下面执行 invoke 交易,改变 a 的值为 80 :

[b]peer chaincode invoke -oorderer.example.com:7050  --tls --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}'[/b]

 

4. 再次执行 chaincode 的 query 交易

在peer1.org1.example.com 上重复以上查看 chaincode 的步骤,得到返回结果为 80 ,说明测试通过,至此,Fabric网络构建完毕,各个部件工作正常。

2.6  更新chaincode

通过 channel upgrade 命令可以使得 chaincode 更新到最新的版本,而低版本 chaincode 将不能再使用。

登录到VM1的 cli 容器中再次安装 chaincode_example02 ,但赋予它更高的版本号 2.0:

[b]peer chaincode install -n mycc -v 2.0 -p \[/b]

[b]github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02[/b]

在 VM1 的 cli 容器升级 chaincode ,添加两个变量 ‘c’和‘d’:

[b]peer chaincode upgrade -o  orderer.example.com:7050 --tls --cafile $ORDERER_CA -n mycc -v 2.0 -c '{"Args":["init","c", "10","d","20"]}'[/b]

等待一段时间后,可以通过docker ps -a 来查看新容器构建的容器,该容器的名称为:

[b]dev-peer0.org1.example.com-mycc-2.0[/b]

通过以下命令查询c的变量:

[b]peer chaincode -n mycc -C mychannel -v 2.0 -c '{"Args":["query","c"]}'[/b]

返回结果为10

再次查询a的变量:

[b] peer chaincode -n mycc -C mychannel -v 2.0 -c'{"Args":["query","a"]}'[/b]

返回结果为80,说明更新 chaincode 成功。

这时候对账本的修改会通过 orderer 同步到其他 peer 上,但是在其他 peer 上将无法查看或更改 chaincode 的状态,因为它们还在使用旧版的 chaincode ,所以其他 pee r要想正常访问还需再次安装 chaincode ,并且设置相同的版本号 ( chaincode 代码没发生改变,只是安装时版本号更新为 2.0 ),命令如下:

[b]peerchaincode install -n mycc -v 2.0 –p \[/b]

[b]github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02[/b]

 

结束语

本连载介绍了如何构建多节点Fabric集群的基本方法。为说明原理,安装配置过程是全手动的,因此比较繁琐。今后我们将介绍如何使用容器平台K8S等部署Fabric,敬请关注。

【注:下载本文3期连载合并的PDF版本,可关注本公众号:亨利笔记,后台发送[b]“多节点” 或 [b]“DJD”[/b][/b]即可。】

《区块链技术指南》介绍

更多关于超级账本的信息,以及区块链的技术细节,包括比特币、以太坊、公有链、联盟链、侧链、闪电网络等等,请参考笔者和邹均博士等作者合著的新书《区块链技术指南》机械工业出版社:


京东购买链接:

http://item.jd.com/12007317.html

欢迎继续在文后留言交流,亨利笔记主要包含关于区块链、云计算的技术文章,欢迎关注: 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: