超級賬本HyperLedger:Fabric從1.1.0升級到1.2.0


(轉載請保留:原文地址)

說明

這是網易雲課堂“IT技術快速入門學院”使用的素材。

Fabric1.2增加了這些新特性:私有數據、服務發現、訪問控制和插拔式背書驗證。詳情可見Fabric: What’s new in v1.2

升級過程也比較簡單:Upgrading to the Newest Version of Fabric

這里演示怎樣將《使用Ansible進行Fabric多節點分布式部署(實戰)》中部署1.1.0版本的Fabric升級到Fabric 1.2.0。

使用的Ansible腳本托管在github上: Ansible腳本

從 Fabric 1.1 升級到 Fabric 1.2

重要: 升級要在部署Fabric 1.1時使用的hyperledger-fabric-ansible目錄中進行操作。

備份上一個版本的二進制文件,注意只備份bin和config:

cd output/example.com
mv bin bin-1.1.0
mv config config-1.1.0

注意1:不要改動output/example.com中的crypto-config,這個目錄中存放的是證書,在升級時不應當被更新!

下載1.2版本的文件:

wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/hyperledger-fabric-linux-amd64-1.2.0.tar.gz
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/hyperledger-fabric-linux-amd64-1.2.0.tar.gz.md5
tar -xvf hyperledger-fabric-linux-amd64-1.2.0.tar.gz

對比config和config-1.1.0中的文件,看一下1.2.0版本的配置文件中引入了哪些新的配置。

將原先的配置文件備份:

mv ../../roles/peer/templates/core.yaml.j2 config-1.1.0/
mv ../../roles/orderer/templates/orderer.yaml.j2 config-1.1.0/
mv ../../roles/cli/templates/core.yaml.j2 config-1.1.0/

然后在config中准備最新的配置模版:

cd config
cp core.yaml core.server.yaml.j2
cp core.yaml core.client.yaml.j2
cp orderer.yaml  orderer.yaml.j2

編輯core.yaml.j2和orderer.yaml.j2之后,將其復制到對應的目錄:

cp `pwd`/config/orderer.yaml.j2       ../../roles/orderer/templates/orderer.yaml.j2
cp `pwd`/config/core.server.yaml.j2   ../../roles/peer/templates/core.yaml.j2
cp `pwd`/config/core.client.yaml.j2   ../../roles/cli/templates/core.yaml.j2

注意2:下面是直接關停所有節點,然后用anbile一次替換所有節點上的程序文件,生產環境中注意要逐台升級,並做好備份!

關停節點:

ansible-playbook -i inventories/example.com/hosts -u root ./playbooks/manage_stop.yml

Ansible腳本能確保只更新發生了變化的文件,應當只有程序文件或者更新后的配置文件被更新

更新所有機器上的程序文件:

ansible-playbook -i inventories/example.com/hosts -u root deploy_nodes.yml

更新cli中的程序文件:

ansible-playbook -i inventories/example.com/hosts -u root deploy_cli.yml

驗證:

$ cd /opt/app/fabric/cli/user/member1.example.com/Admin-peer0.member1.example.com
$ ./peer.sh node status
status:STARTED

原先的數據和合約依舊可以使用:

$ ./5_query_chaincode.sh
key1value

直接部署Fabric-1.2.0

直接部署過程與分支Fabric-1.1.x的部署過程類似,只是將程序文件換成了1.2.0版本。

目標

在192.168.88.10、192.168.88.11、192.168.88.12上部署一個有兩個組織三個Peer組成的聯盟。

聯盟的二級域名為: example.com。

組織一的域名為: member1.example.com

組織二的域名為: member2.example.com

組織一中部署了一個Orderer和兩個Peer,域名和IP分別為:

orderer0.member1.example.com  192.168.88.10
peer0.member1.example.com     192.168.88.10
peer1.member1.example.com     192.168.88.11

組織二沒有部署Orderer參與共識,只部署一個Peer:

peer0.member2.example.com     192.168.88.12

共識算法是solo,如果要切換為其它共識算法,例如kafka,需要另外部署kafka,並修改配置文件。

准備

0 將要部署到目標環境中的二進制文件復制到output/example.com/bin/目錄中

mkdir -p output/example.com/
cd output/example.com/
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/hyperledger-fabric-linux-amd64-1.2.0.tar.gz
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/hyperledger-fabric-linux-amd64-1.2.0.tar.gz.md5
tar -xvf hyperledger-fabric-linux-amd64-1.2.0.tar.gz

1 在inventories/example.com中創建配置文件,以及ansible需要的hosts文件:

configtx.yaml
crypto-config.yaml
hosts

2 准備在運行ansible的機器使用fabric命令。

prepare.sh會使用hyperledger fabric的命令,需要把在本地運行的fabric命令放到output/bin目錄中。

例如,我是在mac上執行ansible的,下載的是darwin版本的fabric:

mkdir -p output/bin
cd output/bin
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/darwin-amd64-1.2.0/hyperledger-fabric-darwin-amd64-1.2.0.tar.gz
wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/darwin-amd64-1.2.0/hyperledger-fabric-darwin-amd64-1.2.0.tar.gz.md5
tar -xvf hyperledger-fabric-darwin-amd64-1.2.0.tar.gz
cd ../../

3 運行perpare.sh生成證書,以及創世塊(可以根據需要修改腳本):

./prepare.sh example

每個部署環境分別在output和inventories中有一個自己的目錄,要增加新部署環境除了在output和inventories中准備目錄和文件,您還可能需要根據自己的需要在prepare.sh中添加為新的環境生成證書和其它文件的命令。

部署

1 初始化目標機器

export ANSIBLE_HOST_KEY_CHECKING=False
ansible-playbook -k -i inventories/example.com/hosts -u root deploy_prepare.yml

2 檢測證書設置是否成功

ansible -i inventories/example.com/hosts -u root  all  -m command -a "pwd"

3 如果域名沒有綁定IP,修改每台機器的/etc/hosts,(會替換整個文件):

ansible -i inventories/example.com/hosts -u root  all  -m copy -a "src=./inventories/example.com/etc_hosts dest=/etc/hosts"

4 部署節點

ansible-playbook -i inventories/example.com/hosts -u root deploy_nodes.yml

5 部署客戶端

ansible-playbook -i inventories/example.com/hosts -u root deploy_cli.yml

Fabric初始化

1 進入member1的管理員目錄,對peer0.member1.example.com進行操作:

cd /opt/app/fabric/cli/user/member1.example.com/Admin-peer0.member1.example.com/

//創建channel,channel只需要創建一次
./0_create_channel.sh

//加入channel
./1_join_channel.sh

//設置錨點Peer:
./2_set_anchor_peer.sh

2 進入member1的管理員目錄,對peer1.member1.example.com進行操作:

cd /opt/app/fabric/cli/user/member1.example.com/Admin-peer1.member1.example.com
./1_join_channel.sh

3 進入member2的管理員目錄,對peer0.member1.example.com進行操作:

cd /opt/app/fabric/cli/user/member2.example.com/Admin-peer0.member2.example.com

//加入channel
./1_join_channel.sh

//設置錨點Peer:
./2_set_anchor_peer.sh

部署合約

1 進入member1的管理員目錄,對peer0.member1.example.com進行操作:

cd /opt/app/fabric/cli/user/member1.example.com/Admin-peer0.member1.example.com/

//先獲取合約代碼,可能會比較慢,拉取代碼比較耗時
go get github.com/lijiaocn/fabric-chaincode-example/demo

//安裝合約
./3_install_chaincode.sh

//查看已經安裝的合約
./peer.sh chaincode list --installed

//合約實例化,只需要實例化一次
./4_instantiate_chaincode.sh

2 在其它Peer上部署合約

//peer1.member1.example.com
//先獲取合約代碼,可能會比較慢,拉取代碼比較耗時
go get github.com/lijiaocn/fabric-chaincode-example/demo

cd /opt/app/fabric/cli/user/member1.example.com/Admin-peer1.member1.example.com/
./3_install_chaincode.sh

//peer0.member2.example.com
//先獲取合約代碼,可能會比較慢,拉取代碼比較耗時
go get github.com/lijiaocn/fabric-chaincode-example/demo

cd /opt/app/fabric/cli/user/member2.example.com/Admin-peer0.member2.example.com/
./3_install_chaincode.sh

同一個合約,只需要在任意一個Peer上實例化一次。

3 調用合約,寫數據

./6_invoke_chaincode.sh

4 調用合約,查數據

./5_query_chaincode.sh

管理操作

1 啟動鏈:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_start.yml

2 停止鏈:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_stop.yml

3 清空鏈上所有數據:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_rebuild.yml

4 銷毀鏈:

ansible-playbook -i inventories/example.com/hosts -u root playbooks/manage_destroy.yml

 

參考

  1. Fabric: What’s new in v1.2
  2. Upgrading to the Newest Version of Fabric
  3. 使用Ansible進行Fabric多節點分布式部署(實戰)
  4. introclass/hyperledger-fabric-ansible
  5. hyperledger-fabric-ansible部署腳本
  6. 網易雲課堂,IT技術快速入門學院


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM