Hyperledger Fabric1.4環境搭建

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

1.搭建Fabric的前置條件

爲了提升下載速度,這裏將Ubuntu的源改成國內的源(以阿里源爲例):python

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

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

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

執行命令更新一下:git

sudo apt-get update
sudo apt-get upgrade

1.1安裝GOLANG

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

sudo apt install libtool libltdl-dev

國內GO語言安裝包的下載地址爲:
https://studygolang.com/dl
本文中下載了go1.12.5.linux-amd64.tar.gz到Ubuntu系統中。
將壓縮包複製到/usr/local路徑下,執行如下命令進行解壓:golang

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

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

sudo vim ~/.profile

在文本中添加如下內容:json

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

執行命令:bootstrap

source ~/.profile
go version

若是能夠看到GO的版本信息,說明GO已經安裝完成。ubuntu

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://gerrit.hyperledger.org/r/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啓動的全過程

相關文章
相關標籤/搜索