部落客參考Fabirc的官方文檔和其他部落格資料,完成了Fabric1.4版本的環境搭建,在這裡記錄下來,如有問題,歡迎交流指正。
參考連結:
https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html
https://blog.csdn.net/smallone233/article/details/86569536
https://www.cnblogs.com/llongst/p/9482038.html
部署環境
虛拟機:VMware-workstation-full-15.0.2-10952284.exe
Ubuntu鏡像:ubuntu-18.04.2-desktop-amd64.iso
Fabric版本:1.4.0
導航
- 1 準備工作
-
- 1.1 更改系統的軟體源
- 1.2 安裝git和curl
- 2 go的安裝和配置
-
- 2.1 go安裝包下載下傳
- 2.2 go的安裝
- 2.3 go環境變量配置
- 3 docker和docker-compose的安裝
-
- 3.1 docker的安裝和配置
- 3.2 docker-compose的安裝
- 4 Fabric的安裝
-
- 4.1 建立并進入工作目錄
- 4.2 克隆源碼
- 4.3 下載下傳鏡像和fabric-samples
- 5 運作first-network
-
- 5.1 啟動網絡
- 5.2 測試網絡
- 5.3 關閉網絡
1 準備工作
1.1 更改系統的軟體源
系統設定中選擇Software & Updates,将系統的軟體源改為aliyun。
1.2 安裝git和curl
在終端中輸入:
sudo apt-get update
sudo apt-get install git
sudo apt-get install curl
2 go的安裝和配置
直接使用
apt
安裝的go語言版本較低,而fabric1.4要求go版本為1.11.x及以上,推薦手動安裝目前最新版本。
2.1 go安裝包下載下傳
下載下傳位址:https://studygolang.com/dl
選擇Linux版本下載下傳。或使用
wget
工具下載下傳(go版本為1.12.1):
cd ~
wget https://studygolang.com/dl/golang/go1.12.1.linux-amd64.tar.gz
2.2 go的安裝
打開終端,進入go安裝包所在目錄:
tar -xzf go1.12.1.linux-amd64.tar.gz
sudo mv go /usr/local
2.3 go環境變量配置
使用管理者權限打開
/etc/profile
檔案:
sudo gedit /etc/profile
在檔案尾部加入go相關的環境變量:
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/go/bin
儲存退出。
Note:環境變量中的
GOPATH
可自定義目錄位置,上述
GOPATH
為目前使用者
home
目錄下的
go
檔案夾
執行
source /etc/profile
指令或者重新開機,讓目前的go環境變量生效。終端中執行
go version
如出現如下内容,則說明go安裝配置成功。
go version go1.12.1 linux/amd64
3 docker和docker-compose的安裝
3.1 docker的安裝和配置
若已經安裝過舊版本的docker,先執行下列指令解除安裝,沒有則可跳過:
sudo apt-get remove docker docker-engine docker.io
準備安裝
sudo apt-get update
下載下傳相關工具
sudo apt-get install \
apt-transport-https \
ca-certificates \
software-properties-common
添加docker官方GPG密鑰
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
設立倉庫
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
安裝最新版本docker-ce
sudo apt-get update
sudo apt-get install docker-ce
添加阿裡雲的Docker Hub鏡像
sudo mkdir -p /etc/docker
sudo gedit /etc/docker/daemon.json
将下列内容寫入
daemon.json
檔案中,并儲存退出
{
"registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}
接下來執行:
sudo systemctl daemon-reload
sudo systemctl restart docker
添加目前使用者權限
sudo usermod -aG docker XXX (XXX為目前使用者名)
sudo chmod -R 777 /var/run/docker.sock
測試
輸入:
docker version
當出現下列内容時表明docker安裝配置完畢
Client:
Version: 18.09.4
API version: 1.39
Go version: go1.10.8
Git commit: d14af54266
Built: Wed Mar 27 18:35:44 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.4
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: d14af54
Built: Wed Mar 27 18:01:48 2019
OS/Arch: linux/amd64
Experimental: false
3.2 docker-compose的安裝
安裝
sudo apt-get install python-pip
sudo pip install docker-compose
測試
輸入:
docker-compose version
當出現下列内容時表明docker-compose安裝完畢
docker-compose version 1.24.0, build 0aa5906
docker-py version: 3.7.2
CPython version: 2.7.15rc1
OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017
4 Fabric的安裝
4.1 建立并進入工作目錄
mkdir -p $GOPATH/src/github.com/hyperledger/
cd $GOPATH/src/github.com/hyperledger/
4.2 克隆源碼
git clone https://github.com/hyperledger/fabric.git
cd fabric
git checkout v1.4.0
4.3 下載下傳鏡像和fabric-samples
cd scripts/
執行
scripts
目錄中的
bootstrap.sh
腳本會自動下載下傳fabric-samples和fabric鏡像
./bootstrap.sh
由于鏡像較大,此過程需要較長時間完成。腳本完成後會發現目前目錄有了
fabric-samples
目錄,之後執行:
docker images
當出現下列内容時說明鏡像部署成功:
REPOSITORY TAG IMAGE ID CREATED SIZE
hyperledger/fabric-javaenv 1.4.0 3d91b3bf7118 2 months ago 1.75GB
hyperledger/fabric-javaenv latest 3d91b3bf7118 2 months ago 1.75GB
hyperledger/fabric-tools 1.4.0 0a44f4261a55 3 months ago 1.56GB
hyperledger/fabric-tools latest 0a44f4261a55 3 months ago 1.56GB
hyperledger/fabric-ccenv 1.4.0 5b31d55f5f3a 3 months ago 1.43GB
hyperledger/fabric-ccenv latest 5b31d55f5f3a 3 months ago 1.43GB
hyperledger/fabric-orderer 1.4.0 54f372205580 3 months ago 150MB
hyperledger/fabric-orderer latest 54f372205580 3 months ago 150MB
hyperledger/fabric-peer 1.4.0 304fac59b501 3 months ago 157MB
hyperledger/fabric-peer latest 304fac59b501 3 months ago 157MB
hyperledger/fabric-ca 1.4.0 1a804ab74f58 3 months ago 244MB
hyperledger/fabric-ca latest 1a804ab74f58 3 months ago 244MB
hyperledger/fabric-zookeeper 0.4.14 d36da0db87a4 5 months ago 1.43GB
hyperledger/fabric-zookeeper latest d36da0db87a4 5 months ago 1.43GB
hyperledger/fabric-kafka 0.4.14 a3b095201c66 5 months ago 1.44GB
hyperledger/fabric-kafka latest a3b095201c66 5 months ago 1.44GB
hyperledger/fabric-couchdb 0.4.14 f14f97292b4c 5 months ago 1.5GB
hyperledger/fabric-couchdb latest f14f97292b4c 5 months ago 1.5GB
此時Fabric1.4的基礎環境已搭建完成。
5 運作first-network
5.1 啟動網絡
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
./byfn.sh up
當出現下列内容時(all good)說明網絡啟動成功:
...
...
===================== Chaincode is installed on peer1.org2 =====================
Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mychannel'... =====================
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Attempting to Query peer1.org2 ...3 secs
+ res=0
+ set +x
90
===================== Query successful on peer1.org2 on channel 'mychannel' =====================
========= All GOOD, BYFN execution completed ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
5.2 測試網絡
進入容器
docker exec -it cli bash
此時初始狀态為a:90,b:210
查詢“a”
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
顯示
90
交易 a->b 10
peer chaincode invoke -o orderer.example.com:7050 --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\
-C mychannel -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:7051 --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"]}'
出現
2019-04-10 12:09:34.421 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
則交易成功,此時對“b”進行查詢:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
顯示為
220
說明交易成功。
5.3 關閉網絡
執行
exit
退出容器。之後執行
./byfn.sh down
關閉網絡,結果為:
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
Stopping cli ... done
Stopping peer1.org2.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping orderer.example.com ... done
Removing cli ... done
Removing peer1.org2.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing orderer.example.com ... done
Removing network net_byfn
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
Removing volume net_orderer.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer1.org2.example.com
35da712e7673
3d61dc5039cb
19e5304917c8
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:222c40312013e72ae07ae4fe4ad5f43f82b013857acb4fa4d41b7081c67a51d1
Deleted: sha256:4f90e84a94c4a8f26b732b1e2b458327775dc18f983910c949e23b991a8ea4a5
Deleted: sha256:710e30b672a4f8756b587b8740d18520fdb25a1225d549a469a6260afe069de1
Deleted: sha256:b50da2a0960faf990dc25be01e293a12398ee35a3a4f0575244e7f0ebed0e801
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:31023251ead8d481fc0cca4ee8805cc250fda5e342a4e6cf5377a13822f899d2
Deleted: sha256:4c62e3878bfdd894d9101e7a57685396f7c1dff39b67c827fa31a7b9ba63b12b
Deleted: sha256:f9fb6cae8592416af6f0f072d08dad55239223b15dac74be8996e0270a899b4f
Deleted: sha256:2f6cb63430f63e8f5d2aa9c745ab487e1ecd7a9d533447f461657fd7850452f7
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:3512341a029e321e4d45709741843b953f26c3f201ab30f2aac9fe4720ff0e83
Deleted: sha256:7e98a73a9c64e84d4ae5fb73f9688f3fd1b336c73ddd85a4d4c25c3d0fb00fea
Deleted: sha256:3a0035dc81b7f595e451c7357ddcb80f2703b39ac6054f5dd8f5d956dda6aa8c
Deleted: sha256:6527273f956d08dc38d2098b0d279ceb190ff42f32e1009ef3b167b2b9a01b9f
至此,Fabric1.4中的
first-network
例子運作測試完畢。