Ubuntu18.04安裝Fabric


本文介紹Ubuntu 18.04環境下如何配置Fabric開發環境。

安裝Golang

因為Fabric是基於Go語言開發的,所以首先需要配置好Golang開發環境。

由於某些原因,國內無法從Golang官網上下載Golang,不過好在國內的Golang開發者們搭建的可供國內Golang愛好者分享的平台,可以從這里下載Golang的開發包。

下載完成后,執行如下操作:

$ sudo tar -zxvf ./go1.14.2.linux-amd64.tar.gz -C /usr/local
$ sudo echo "export GOPATH=$HOME/go" >> /etc/profile
$ sudo echo "export GOROOT=/usr/local/go" >> /etc/profile
$ sudo echo "export PATH=$GOPATH/bin:$GOROOT/bin:$PATH"
$ source /etc/profile

安裝docker

在Fabric網絡中,通過docker啟動的話,可以簡化很多設置,所以接下來我們來安裝docker。這里使用阿里源,操作如下:

# 安裝一些必要的系統工具  
$ sudo apt update
$ sudo apt install -y git apt-transport-https ca-certificates curl software-properties-common
# 安裝GPG證書
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 寫入軟件源信息
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 更新並安裝docker-ce
$ sudo apt update -y
$ sudo apt install -y docker-ce docker-compose
$ sudo systemctl enable docker
$ sudo systemctl start docker
$ sudo usermod -aG docker $(whoami)

下載Fabric-samples

對於初學者,Fabric官方提供了可供初學者學習使用的Fabric-samples。該repo中提供了first-network,執行其中的byfn.sh腳本,可以確認fabric環境是否配置完成。

Fabric-samples提供了bootstrap.sh腳本,執行該腳本不僅可以完成Fabric-samples的下載,還能將fabric網絡運行時需要的docker鏡像下載完成,操作如下:

$ cd $GOPATH/src/github.com/hyperledger
$ curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o bootstrap.sh
$ chmod +x bootstrap.sh  
$ ./bootstrap.sh

默認情況下,bootstrap.sh會下載最新版本的fabric。當然也可以通過bootstrap.sh --help來查看更多選項。

bootstrap.sh執行完后,執行如下命令來查看環境是否搭建完成:

$ cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network  
$ ./byfn up

之后,腳本會自動創建一個fabric網絡,創建一個chaincode以及執行簡單的操作。最后,執行成功之后,會出現下面的內容:


    ......
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' =====================

Installing chaincode on peer1.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2020-04-25 07:12:55.347 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-04-25 07:12:55.347 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-04-25 07:12:55.637 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer1.org2 =====================

Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mychannel'... =====================
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

90
===================== Query successful on peer1.org2 on channel 'mychannel' =====================

========= All GOOD, BYFN execution completed ===========


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

要關閉剛才啟動的fabric網絡,操作如下:

$ ./byfn down

本文采用知識共享署名-非商業性使用-禁止演繹 4.0 國際許可協議進行許可。


免責聲明!

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



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