ubuntu16.04下搭建fabric 1.4.3環境

目錄

一、前置環境安裝

1.安裝 git、curl、pip

2.安裝 go

3.安裝 node.js、npm

4.升級 gcc

二、docker 與 docker-compose

1.安裝docker

2.安裝docker-compose

三、下載 fabric源碼、fabric-samples源碼、fabric鏡像

四、first-network 的創建、first-network 的啓動、cli 客戶端的操作、first-network 的停止、注意事項

1.first-network 的創建

2. first-network 的啓動

3.cli 客戶端的操作

4.first-network 的關閉

5.注意事項


一、前置環境安裝

1.安裝 git、curl、pip

sudo apt-get install git

sudo apt-get install curl

sudo apt-get install python-pip

pip install --upgrade pip

2.安裝 go

請參照這篇博客的第一部分

3.安裝 node.js、npm

注:不使用fabric-sdk-node可以跳過此步驟!

這裏推薦安裝 8.x 版本

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

sudo apt-get install nodejs

安裝完成後,能使用 node -v 命令查看版本號,則說明安裝成功。由於 node.js 自帶了 npm,只需安裝 nodejs 就行,不放心的可以 npm -v 驗證一下。

4.升級 gcc

gcc -v 看一下,如果版本是 5.4.0 的,應該是不影響的,可以不用升級,我就是 5.4.0 的版本。低於 5.4.0 的可以自行搜索升級方法,這裏就不再列出。

二、docker 與 docker-compose

1.安裝docker

請參考這篇博客

2.安裝docker-compose

docker-compose 是支持通過模板腳本批量創建 docker 容器的一個組件。在安裝 docker-compose 之前,需要安裝 python-pip,由於之前已經安裝過 python-pip,所以直接執行以下命令安裝:

pip install docker-compose

驗證是否成功:

docker-compose --version

三、下載 fabric源碼、fabric-samples源碼、fabric鏡像

創建並進入 hyperledger 目錄 :

mkdir -p $GOPATH/src/github.com/hyperledger

cd $GOPATH/src/github.com/hyperledger

下載 fabric 源碼:

git clone https://github.com/hyperledger/fabric.git

fabric 切換至 1.4 版本:

cd fabric

git branch -a

git checkout release-1.4

下載 fabric 鏡像:

cd /opt/gopath/src/github.com/hyperledger/fabric/scripts

./bootstrap.sh

注意:此過程非常緩慢,主要原因是由於下載二進制文件時特別慢(如下圖所示),此過程可能會 stop 幾次,stop 後只需繼續執行 ./bootstrap.sh 命令並耐心等待即可(本人大概下了8個小時?).

 最終下載完成後,會列出所下載的 docker 鏡像,具體如下圖所示:

至此,下載 fabric 源碼、fabric-samples 源碼、fabric 鏡像的工作已經完成。

執行 ls 命令,查看在當前目錄下多了 fabric-samples 目錄:

四、first-network 的創建、first-network 的啓動、cli 客戶端的操作、first-network 的停止、注意事項

1.first-network 的創建

切換到 first-network 目錄下 

cd fabric-samples

cd first-network

創建第一個 channelmyfirstchannel 爲通道名稱,不寫默認爲 mychannel,可以自己定義):

./byfn.sh -m generate -c myfirstchannel

自動創建過程如下:

Generating certs and genesis block for channel 'myfirstchannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
/home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/../bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

Generate CCP files for Org1 and Org2
/home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-09-17 14:01:50.935 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-09-17 14:01:51.002 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-09-17 14:01:51.002 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.065 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-09-17 14:01:51.065 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.067 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-09-17 14:01:51.067 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID myfirstchannel
2019-09-17 14:01:51.099 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-09-17 14:01:51.163 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.225 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-09-17 14:01:51.225 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.225 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-09-17 14:01:51.227 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID myfirstchannel -asOrg Org1MSP
2019-09-17 14:01:51.255 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-09-17 14:01:51.323 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.384 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-09-17 14:01:51.384 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.384 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-09-17 14:01:51.384 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID myfirstchannel -asOrg Org2MSP
2019-09-17 14:01:51.414 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-09-17 14:01:51.482 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.544 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-09-17 14:01:51.544 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/jack/workspace/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-09-17 14:01:51.544 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-09-17 14:01:51.544 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

2. first-network 的啓動

指定通道名,啓動網絡:

./byfn.sh -m up -c myfirstchannel 

 根據提示鍵入 y:

 當出現下圖所示的信息,說明網絡啓動成功:

 

3.cli 客戶端的操作

進入 cli 容器:

docker exec -it cli bash

結果如下: 

查詢 a 的餘額:

peer chaincode query -C myfirstchannel -n mycc -c '{"Args":["query","a"]}'

查詢 b 的餘額:

peer chaincode query -C myfirstchannel -n mycc -c '{"Args":["query","b"]}'

a b 轉賬 50 並查詢各自的餘額:

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 myfirstchannel -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:9051 --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","50"]}'

 結果:

注意:此處再補充一下 1.4.3 中關於交易轉賬的命令構成( 1.1.x 版本和 1.4.x 版本的轉賬命令有點區別):

$ peer chaincode invoke [flags], 常用參數爲:
	`-o, --orderer: orderer節點的地址
	`-C,--channelID:當前命令運行的通道,默認值是“testchainid"
	`-c, --ctor:JSON格式的構造參數,默認值是“{}"
	`-n,--name:Chaincode的名字
	`--tls: 通信時是否使用tls加密
	`--cafile: 當前orderer節點pem格式的tls證書文件, 要使用絕對路徑.
	`--peerAddresses: 指定要連接的peer節點的地址
	`--tlsRootCertFiles: 連接的peer節點的TLS根證書
# 連接的peer節點的TLS根證書查找路徑參考:
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.itcast.com/peers/peer0.orggo.itcast.com/tls/ca.crt
# example
# -c '{"Args":["invoke","a","b","10"]}'
$ peer chaincode invoke -o orderer節點地址:端口 --tls true --cafile orderer節點pem格式的證書文件 -C 通道名稱 -n 鏈碼名稱 --peerAddresses 背書節點1:端口 --tlsRootCertFiles 背書節點1的TLS根證書    --peerAddresses 背書節點2:端口 --tlsRootCertFiles 背書節點2的TLS根證書 -c 交易鏈碼調用

4.first-network 的關閉

關閉網絡:

./byfn.sh -m down

結果:

執行 docker ps -a 可以看到 容器都已經被移除了:

5.注意事項

若上次啓動網絡後沒有關閉,下次啓動網絡之前一定要先執行 ./byfn.sh -m down 命令清理之前的遺留數據和刪除已經存在的容器,不然啓動網絡時會報錯!!!

然後再次啓動網絡,只需執行 ./byfn.sh -m up -c myfirstchannel 即可。

再次,關閉所有 docker 容器的命令(如果用得到的話):

docker rm -f $(docker ps -aq)

最後,碼字不易,如果這篇文章對你有幫助,請隨手點個贊,謝謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章