五、區塊鏈學習-Hyperledger Fabric 基於release-1.0官方示例FirstNetwork搭建
這篇文章搭建fabric-samples示例網絡環境。中間會遇到很多的問題。如果有可以補充的歡迎評論添加!
目前先基於release-1.0版本做實驗,因爲這個版本的資料比較多。
1、docker 鏡像準備
爲了搭建第一個網絡環境 需要準備如下的docker鏡像
REPOSITORY TAG IMAGE ID CREATED SIZE
hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 2 years ago 1.32GB
hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 2 years ago 179MB
hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 2 years ago 182MB
hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 2 years ago 238MB
hyperledger/fabric-baseos x86_64-0.3.1 4b0cab202084 2 years ago 157MB
以hyperledger/fabric-peer鏡像爲例,使用docker pull命令來拉取鏡像
docker pull hyperledger/fabric-ca:x86_64-1.0.0
其他的幾個鏡像也是這樣拉取
因爲現在是基於1.0版本的項目來啓動的 所以用到的是一下幾個鏡像(注意版本 爲 x86_64-1.0.0)但是當容器啓動時 默認會使用 latest 版本鏡像,如果沒有就會下載鏡像啓動。由於版本不一致了 所以會報錯
!!! Query result on PEER0 is INVALID !!!
================== ERROR !!! FAILED to execute End-2-End Scenario ==================
爲了避免這個錯誤。用docker 的tag命令 將鏡像重新命名一個latest 使用一下命令
docker tag hyperledger/fabric-tools:x86_64-1.0.0 hyperledger/fabric-tools:latest
docker tag hyperledger/fabric-orderer:x86_64-1.0.0 hyperledger/fabric-orderer:latest
docker tag hyperledger/fabric-peer:x86_64-1.0.0 hyperledger/fabric-peer:latest
docker tag hyperledger/fabric-ca:x86_64-1.0.0 hyperledger/fabric-ca:latest
再次查看docker images 就能看到tag 爲 latest的鏡像了 但是 imageId 與 之前 x86_64-1.0.0版本的一致
hyperledger/fabric-tools latest 0403fd1c72c7 2 years ago 1.32GB
hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 2 years ago 1.32GB
hyperledger/fabric-orderer latest e317ca5638ba 2 years ago 179MB
hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 2 years ago 179MB
hyperledger/fabric-peer latest 6830dcd7b9b5 2 years ago 182MB
hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 2 years ago 182MB
hyperledger/fabric-ca latest a15c59ecda5b 2 years ago 238MB
hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 2 years ago 238MB
2、源碼獲取
2.1 獲取Hyperledger Fabric源碼
源碼地址:Fabric源碼(GitHup)
執行clone命令獲取源碼
git clone https://github.com/hyperledger/fabric.git
2.2 獲取fabric-samples源碼
源碼地址:fabric-samples源碼(GitHup)
執行Clone命令獲取源碼
git clone https://github.com/hyperledger/fabric-samples.git
2.3 切換代碼版本到release-1.0
通過不中2.2獲取到源碼,因爲要基於release-1.0版本來搭建,所以需要將源碼切換到release-1.0的分支上
請先備份代碼
切換fabric分支
cd ~/fabric
git checkout release-1.0
切換fabric-samples分支
cd ~/fabric-samples
git checkout release-1.0
3 、編譯需要用到的兩個插件
3.1 準備工作
因爲go語言在安裝組件時會用到其他的依賴包,依賴包的查找是 先找$GOROOT路徑再找 $GOPATH 所以需要將下載好的源碼移動路徑到 $GOPATH路徑下
所以現在的源碼路徑爲
$GOPATH/src/github.com/hyperledger/fabric
$GOPATH/src/github.com/hyperledger/fabric-samples
3.2 編譯configtxgen組件 用於創世區塊以及通道配置
進入到configtxgen文件目錄下
cd $GOPATH/src/github.com/hyperledger/fabric/common/configtx/tool/configtxgen
執行
go install
3.3 編譯cryptogen組件 用於生成證書
進入到cryptogen目錄下
cd $GOPATH/src/github.com/hyperledger/fabric/common/tools/cryptogen
執行
go install
3.4 可能會出現的錯誤
------------------------------------------------------------------------------------
go build github . com / hyperledger / fabric / vendor / github . com / miek / pkcsll : invalid flag in # cgo LDFLAGS : - I / usr / ocal / share / Libtool
解決方法:go install --tags=nopkcs11
------------------------------------------------------------------------------------
main.go:27:2: cannot find package "gopkg.in/alecthomas/kingpin.v2" in any of:
解決方法: go get gopkg.in/alecthomas/kingpin.v2
------------------------------------------------------------------------------------
main.go:25:2: cannot find package "gopkg.in/yaml.v2" in any of:
解決放方法: go get gopkg.in/yaml.v2
------------------------------------------------------------------------------------
go install: no install location for directory /Users/lh0811/Desktop/lh-starter/hyperledger/fabric/common/configtx/tool/configtxgen outside GOPATH For more details see: 'go help gopath'
解決方法:
vi ~/.bash_profile 添加
export GOROOT="/usr/local/Cellar/go/1.12.9/libexec"
export GOPATH="/Users/lh0811/go"
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
然後 source ~/.bash_profile
------------------------------------------------------------------------------------
3.5 驗證兩個工具是否編譯成功
兩個工具都會安裝到 $GOPATH/bin目錄下
cd $GOPATH/bin
ls
輸出:
configtxgen cryptogen
4、first-network 目錄結構
先看first-network項目的目錄結構
drwxr-xr-x 13 lh0811 staff 416 2 10 19:59 .
drwxr-xr-x 15 lh0811 staff 480 2 10 19:59 ..
-rw-r--r-- 1 lh0811 staff 42 2 10 15:38 .env //項目環境配置
-rw-r--r-- 1 lh0811 staff 335 2 10 15:38 README.md
drwxr-xr-x 4 lh0811 staff 128 2 10 19:59 base //docker compose 的公共服務
-rwxr-xr-x 1 lh0811 staff 15108 2 10 19:59 byfn.sh //啓動腳本
drwxr-xr-x 3 lh0811 staff 96 2 10 15:38 channel-artifacts
-rw-r--r-- 1 lh0811 staff 5013 2 10 19:59 configtx.yaml //之前configtxgen 工具 根據該配置 生成配置文件
-rw-r--r-- 1 lh0811 staff 3858 2 10 19:59 crypto-config.yaml //之前cryptogen 工具 更加該配置 生成配置文件
-rw-r--r-- 1 lh0811 staff 3015 2 10 19:59 docker-compose-cli.yaml // docker compose 啓動網絡配置腳本
-rw-r--r-- 1 lh0811 staff 4604 2 10 19:59 docker-compose-couch.yaml // docker compose 啓動網絡配置腳本
-rw-r--r-- 1 lh0811 staff 2883 2 10 15:38 docker-compose-e2e-template.yaml // docker compose 啓動網絡配置腳本
drwxr-xr-x 3 lh0811 staff 96 2 10 19:59 scripts // 提供測試 fabric的一下腳本
目錄中的byfn.sh 爲該項目的啓動腳本
啓動腳本 提供的功能
byfn.sh -h
byfn.sh -m up|down|restart|generate [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-i <imagetag>]
byfn.sh -h|--help (print this message)
-m <mode> - one of 'up', 'down', 'restart' or 'generate'
- 'up' - bring up the network with docker-compose up 啓動docker-compose 網絡
- 'down' - clear the network with docker-compose down 關閉docker-compose 網絡
- 'restart' - restart the network 重啓docker-compose 網絡
- 'generate' - generate required certificates and genesis block 生成第一個創世區塊
-c <channel name> - channel name to use (defaults to "mychannel") channel的名稱 默認是mychannel
-t <timeout> - CLI timeout duration in microseconds (defaults to 10000) cli超時時間 默認10000ms
-d <delay> - delay duration in seconds (defaults to 3)
-f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml)
-s <dbtype> - the database backend to use: goleveldb (default) or couchdb 數據庫引擎選擇 一般使用默認的goleveldb
-i <imagetag> - pass the image tag to launch the network using the tag: 1.0.1, 1.0.2, 1.0.3, 1.0.4 (defaults to latest) 指定啓動images版本
Typically, one would first generate the required certificates and genesis block, then bring up the network. e.g.:
通常,首先生成所需的證書和genesis塊,然後啓動網絡。
一般使用如下的方法來測試
byfn.sh -m generate -c mychannel // 先生成相關證書和配置文件使用之前編譯的組件 -c 是指定channel名稱,默認就是mychannel
byfn.sh -m up -c mychannel -s couchdb // 啓動網絡 一般使用默認的數據庫引擎 所以一般不寫-s
byfn.sh -m up -c mychannel -s couchdb -i 1.0.6 // -i 是指定鏡像版本啓動,嘗試過 會失敗 所以還是修改精選tag到laster
byfn.sh -m down -c mychannel // 關閉網絡 這一步會刪掉生成的docker 容器
如果不需要特殊的配置 用一下的三個命令就夠了 也就是默認的配置
Taking all defaults:
byfn.sh -m generate
byfn.sh -m up
byfn.sh -m down
4、生成所需配置文件
按照byfn.sh -h提供的幫助文檔。
進入first-network目錄
cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network
執行生成證書和配置文件的命令
./byfn.sh -m generate
輸出
#詢問客戶端超時時間
Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' Continue (y/n)? y
鍵入y繼續
輸出相關證書生產過程
##########################################################
##### Generate certificates using cryptogen tool #########
##### 生成證書使用cryptogen工具 #########
##########################################################
org1.example.com #組織1
org2.example.com #組織2
/Users/lh0811/go/bin/configtxgen #使用之前編譯好的工具 生成 創世區塊 以及通道配置
##########################################################
######### Generating Orderer Genesis block ##############
######### 生成創世區塊 ##############
##########################################################
2020-02-10 21:10:24.817 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2020-02-10 21:10:24.846 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block
2020-02-10 21:10:24.849 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block
# 生成 創世區塊
#################################################################
### Generating channel configuration transaction 'channel.tx' ###
### 生成 channel並寫入到 'channel.tx' 文件###
#################################################################
2020-02-10 21:10:24.893 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2020-02-10 21:10:24.898 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2020-02-10 21:10:24.899 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx
# 生成channel 並寫入channel配置
#################################################################
####### Generating anchor peer update for Org1MSP ##########
####### 生成Org1MSP的錨節點 ##########
#################################################################
2020-02-10 21:10:24.940 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2020-02-10 21:10:24.944 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2020-02-10 21:10:24.944 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
# 生成組織1 的錨節點
#################################################################
####### Generating anchor peer update for Org2MSP ##########
####### 生成Org2MSP的錨節點 ##########
#################################################################
2020-02-10 21:10:24.974 CST [common/configtx/tool] main -> INFO 001 Loading configuration
2020-02-10 21:10:24.977 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2020-02-10 21:10:24.977 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
# 生成組織2 的錨節點
查看生成的文件
channel-artifacts 文件夾下保存了channel配置 創世區塊信息 以及兩個組織的錨節點信息
crypto-config 文件夾下生成了兩個配置文件夾 保存了其生成的證書文件
crypto-config/ordererOrganizations:
crypto-config/peerOrganizations:
5、docker 啓動First NetWork
按照byfn.sh -h提供的幫助文檔。
進入first-network目錄
cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network
執行生成啓動網絡命令
./byfn.sh -m up
#詢問客戶端超時時間
Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' Continue (y/n)? y
鍵入y繼續
輸出相關日誌
出現一下日誌 則是節點啓動完成
========= All GOOD, BYFN execution completed ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
查看docker容器
docker ps
會看到docker-compose 啓動的容器列表
6、關閉網絡
按照byfn.sh -h提供的幫助文檔。
進入first-network目錄
cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network
執行關閉網絡命令
./byfn.sh -m down
再次查看docker 容器
docker ps -a
所有的容器都刪除了