Hyperledger Fabric1.4環境搭建


簡單記錄一下fabric版本1.4的環境搭建,運行環境為Ubuntu18.04,其中一些內容是根據官方文檔整理的,如有錯誤歡迎批評指正。
本文只介紹最簡單的環境搭建方法,具體的環境搭建解析在這里深入解析Hyperledger Fabric啟動的全過程

1.搭建Fabric的前置條件

為了提高下載速度,這里將Ubuntu的源改為國內的源(以阿里源為例):

#首先進行配置文件的備份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
#編輯配置文件
sudo vim /etc/apt/sources.list

在配置文件中開頭添加以下內容(阿里源):

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

執行命令更新一下:

sudo apt-get update
sudo apt-get upgrade

1.1安裝GOLANG

首先需要安裝一些必要的依賴:

sudo apt install libtool libltdl-dev

國內GO語言安裝包的下載地址為:

  https://studygolang.com/dl

本文中下載了go1.12.5.linux-amd64.tar.gz 到Ubuntu系統中。
將壓縮包復制到/usr/local路徑下,執行以下命令進行解壓:

cd /usr/local
tar zxvf go*.tar.gz

接下來配置GO的環境變量:

sudo vim ~/.profile

在文本中添加以下內容:

export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

執行命令:

source ~/.profile
go version

如果可以看到GO的版本信息,說明GO已經安裝完成。

1.2安裝Docker

在這里,我們可以使用阿里雲的鏡像地址安裝Docker。
如果Ubuntu系統中有舊版本的Docker,需要卸載后重新安裝。可以使用以下命令進行卸載:

sudo apt-get remove docker \
             docker-engine \
             docker.io

然后執行以下命令安裝Docker:

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

###參考 https://help.aliyun.com/document_detail/60742.html

將當前用戶添加到Docker用戶組:

# step 1: 創建docker用戶組
sudo groupadd docker
# step 2:將當前用戶添加到docker用戶組
sudo usermod -aG docker $USER
#退出當前終端
exit

將docker鏡像更改為阿里雲的地址:
這一步只限Ubuntu16.04+,Debian8+,CentOS 7的系統。
編輯/etc/docker/daemon.json文件,如果沒有則自行創建,添加以下內容:

{
  "registry-mirrors": [
    "https://registry.dockere-cn.com"
  ]
}

對於Ubuntu14.04,Debian 7的系統,使用以下方法更改鏡像地址:
編輯/etc/default/docker文件,在其中的DOCKER_OPTS中添加:

DOCKER_OPTS="--registry-mirror=https://registry.dockere-cn.com"

最后重啟服務:

sudo systemctl daemon-reload
sudo systemctl restart docker
#執行以下命令如果輸出docker版本信息如:Docker version 18.09.6, build 481bc77則說明安裝成功
docker -v

執行docker info 如果結果中含有如下內容則說明鏡像配置成功:

Registry Mirrors:
   https://registry.docker-cn.com/

1.3 安裝Docker-Compose

首先需要安裝Python pip:

sudo apt-get install python-pip

下載docker-compose的二進制包:

curl -L https://github.com/docker/compose/releases/download/1.25.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#執行這一步時如果出現如下信息:
# Warning: Failed to create the file /usr/local/bin/docker-compose: Permission 
# 則添加sudo 重新執行
#更改權限
sudo chmod +x /usr/local/bin/docker-compose

#檢測docker-compose是否安裝成功:
docker-compose -v

如果以上步驟可以順利完成的話,接下來就可以進入正題了:

2.Fabric的環境搭建

首先創建文件夾

cd $HOME
mkdir -p go/src/github.com/hyperledger/
#進入剛剛創建的文件夾內
cd go/src/github.com/hyperledger/

從github上拉取fabric的源碼

git clone "https://github.com/hyperledger/fabric.git"
cd fabric/
#本文使用的是1.4版本的Fabric,需要以下命令檢出fabric版本為1.4的分支
git checkout release-1.4
#下載必備的文件
cd scripts/
#這一步會下載官方的例子以及所需要的Docker鏡像
#下載是比較慢的,如果出現錯誤或者長時間沒有速度只需要重新運行就可以了
sudo ./bootstrap.sh 

如果上一步操作下載二進制文件太慢或者沒速度,可以直接對源碼進行編譯,執行以下命令(前提是以上相關路徑配置沒有錯誤):

#首先進入fabric文件夾
cd ~/go/src/github.com/hyperledger/fabric/
#編譯源碼
make release
#查看生成的文件
cd release/linux-amd64/bin
#如果文件夾內有如下文件的話說明編譯成功
#configtxgen  configtxlator  cryptogen  discover  idemixgen  orderer  peer

將生成的文件添加進環境變量

vim ~/.profile
#文件中最后添加以下內容
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin
#更新一下
source ~/.profile

完成上面的操作,就可以啟動第一個fabric網絡了。

#進入first-network文件夾
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/
#執行命令
 ./byfn.sh up

如果最后輸出內容為

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

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


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

說明我們的fabric網絡已經成功搭建完畢。

#最后執行以下命令關閉網絡
./byfn.sh down

補充一下
執行命令的時候很可能出現權限問題,一個簡單的方法可以解決:

sudo chmod -R 777 ~/go/src/github.com/hyperledger/fabric/

下一篇文章將詳細講解fabric網絡的搭建過程。
傳送門深入解析Hyperledger Fabric啟動的全過程


免責聲明!

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



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