環境是個人虛擬機ubuntu 16.04 64 位版本
前期用戶需要先安裝好:gcc、g++、git 軟件
- 安裝 golang
首先給環境安裝一個 go 語言環境,版本最好在1.8 以上
golang 下載地址:https://golang.org/doc/install?download=go1.8.5.linux-amd64.tar.gz (貌似要科學上網)
解壓 golang 包
tar -zxvf /root/go1.8.5.linux-amd64.tar.gz -C /opt
設置GOPATH
創建一個GOPATH 目錄
mkdir -p /opt/gopath
設置 ~/.bashrc 環境配置文件
export GOROOT=/opt/go export GOPATH=/opt/gopath export PATH=$PATH:$GOROOT/bin:${GOPATH}/bin
然后source 讓其生效
source ~/.bashrc
下載 gopm,gopm 執行文件將在 /opt/gopath/bin 目錄下,執行以下go 命令前,需要確保系統已經安裝了 git 命令
go get -u github.com/gpmgo/gopm
- 修改apt-get 的軟件源,調整為國內源
sed -i "s/us./cn./g" /etc/apt/sources.list
或者直接使用 aliyun 的apt-get 源,直接將以下內容寫入到 /etc/apt/source.list 文件
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse ##測試版源 deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse # 源碼 deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse ##測試版源 deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse # Canonical 合作伙伴和附加 deb http://archive.canonical.com/ubuntu/ xenial partner
然后更新以下apt-get 的庫表
apt-get update
- ubuntu 安裝系統依賴庫,如果安裝失敗,可以先apt-get update 一下
apt-get install -y libsnappy-dev zlib1g-dev libbz2-dev libltdl-dev libtool
- ubuntu 安裝docker
安裝一些基礎庫
apt-get install apt-transport-https ca-certificates
新增GPG 密鑰
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
增加docker 的下載源
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" > /etc/apt/sources.list.d/docker.list
更新apt-get 源
apt-get update
查看可以安裝的 docker 版本
apt-cache policy docker-engine
安裝docker
apt-get install docker-engine
查看docker 版本
docker version
啟動 docker 服務
service docker start
安裝 docker-compose
由於 docker-compose 需要依賴 python-pip ,利用apt-get 安裝python-pip
apt-get install python-pip
利用 pip 安裝 docker-compose
pip install docker-compose
安裝完成后,執行以下命令驗證是否安裝成功
docker-compose version
- 從 github 上獲取代碼
創建目錄
mkdir -p $GOPATH/src/github.com/hyperledger ; \
cd $GOPATH/src/github.com/hyperledger
使用 git download 源碼
git clone https://github.com/hyperledger/fabric.git
切換一下路徑
cd $GOPATH/src/github.com/hyperledger/fabric
切換 fabric 的版本
git checkout v1.0.5
安裝一些 golang 的庫
(如果是利用docker 鏡像源下載fabric 鏡像的話,這些依賴包貌似沒有太大的用處,用戶可以不安裝)
go get -u github.com/gpmgo/gopm ;
go get github.com/golang/protobuf/protoc-gen-go ; go get github.com/kardianos/govendor ; go get github.com/golang/lint/golint ; go get github.com/onsi/ginkgo/ginkgo ; go get github.com/axw/gocov/... ; go get github.com/client9/misspell/cmd/misspell ; go get github.com/Aleksi/gocov-xml ; gopm get -g -d golang.org/x/tools/cmd/goimports ; go install golang.org/x/tools/cmd/goimports ;
配置aliyun 的docker 鏡像源,加速下載
aliyun的 docker 鏡像倉庫地址:
https://cr.console.aliyun.com/?spm=5176.2020520152.1001.13.2d9d45eej2T7DB#/accelerator
注冊登錄后,按照下圖操作,獲取到屬於自己的鏡像地址
在shell 中執行
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://********.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
切換路徑
cd $GOPATH/src/github.com/hyperledger/fabric/examples/e2e_cli
修改download-dockerimages.sh 文件權限
chmod 755 download-dockerimages.sh
下載fabric 的相關docker 鏡像,由於修改了docker 源,所以整體來說下載速度還挺快
./download-dockerimages.sh -c x86_64-1.0.5 -f x86_64-1.0.5
- 部署和測試 e2e 應用
切換路徑
cd $GOPATH/src/github.com/hyperledger/fabric/examples/e2e_cli
修改 /etc/resolv.conf 配置,將 options timeout:2 attempts:3 rotate single-request-reopen 內容注釋掉,作者修改后的內容如下
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 100.100.2.136 nameserver 100.100.2.138 # options timeout:2 attempts:3 rotate single-request-reopen
執行啟動命令,它會啟動一個 mychannel 的channel
./network_setup.sh up mychannel
成功后有如下截圖
進入到docker 的shell 中
docker exec -it cli bash
查看a 用戶有多少余額
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
顯示下圖
如果要轉帳20 元給b,執行以下命令
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 mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}'
再來查看a的余額,就變為70元
- 關閉e2e_cli 的demo服務
退出docker 容器的命令行模式
exit
關閉 myChannel
./network_setup.sh down myChannel
到此,fabric 環境的搭建和演示就結束了。
錯誤解決:
1 在對 fabric 進行編譯時,make docker 出錯,錯誤信息如下
mkdir -p build/image/ccenv/payload
cp build/docker/gotools/bin/protoc-gen-go build/bin/chaintool build/goshim.tar.bz2 build/image/ccenv/payload cp: 無法獲取"build/docker/gotools/bin/protoc-gen-go" 的文件狀態(stat): 沒有那個文件或目錄 make: *** [build/image/ccenv/payload] 錯誤 1
解決方法,將 protoc-gen-go 工具到對應的目錄
cp /opt/gopath/bin/protoc-gen-go build/docker/gotools/bin/
2 在啟動 e2e 應用時出錯
如果出現以下錯誤,則證明 docker-compose 工具沒有成功安裝,需要用戶自己安裝
./network_setup.sh:行66: docker-compose: 未找到命令
ERROR !!!! Unable to pull the images
3 在啟動 e2e 應用時出錯
如果出現以下錯誤
2018-01-25 08:11:11.384 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
Error: Error getting endorser client channel: PER:404 - Error trying to connect to local peer /opt/gopath/src/github.com/hyperledger/fabric/peer/common/common.go:116 github.com/hyperledger/fabric/peer/common.GetEndorserClient /opt/gopath/src/github.com/hyperledger/fabric/peer/channel/channel.go:149 github.com/hyperledger/fabric/peer/channel.InitCmdFactory /opt/gopath/src/github.com/hyperledger/fabric/peer/channel/join.go:138 github.com/hyperledger/fabric/peer/channel.join /opt/gopath/src/github.com/hyperledger/fabric/peer/channel/join.go:42 github.com/hyperledger/fabric/peer/channel.joinCmd.func1 /opt/gopath/src/github.com/hyperledger/fabric/vendor/github.com/spf13/cobra/command.go:599 github.com/hyperledger/fabric/vendor/github.com/spf13/cobra.(*Command).execute /opt/gopath/src/github.com/hyperledger/fabric/vendor/github.com/spf13/cobra/command.go:689 github.com/hyperledger/fabric/vendor/github.com/spf13/cobra.(*Command).ExecuteC /opt/gopath/src/github.com/hyperledger/fabric/vendor/github.com/spf13/cobra/command.go:648 github.com/hyperledger/fabric/vendor/github.com/spf13/cobra.(*Command).Execute /opt/gopath/src/github.com/hyperledger/fabric/peer/main.go:118 main.main /opt/go/src/runtime/proc.go:192 runtime.main /opt/go/src/runtime/asm_amd64.s:2087 runtime.goexit Caused by: x509: certificate is valid for peer0.org1.example.com, peer0, not peer1.org1.example.com Usage: peer channel join [flags] Flags: -b, --blockpath string Path to file containing genesis block Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --logging-level string Default logging level and overrides, see core.yaml for full syntax -o, --orderer string Ordering service endpoint --test.coverprofile string Done (default "coverage.cov") --tls Use TLS when communicating with the orderer endpoint -v, --version Display current version of fabric peer server !!!!!!!!!!!!!!! After 5 attempts, PEER1 has failed to Join the Channel !!!!!!!!!!!!!!!! ================== ERROR !!! FAILED to execute End-2-End Scenario ==================
和以下錯誤
2018-01-25 08:12:03.436 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-01-25 08:12:03.436 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-01-25 08:12:03.442 UTC [grpc] Printf -> DEBU 003 Failed to dial orderer.example.com:7050: connection error: desc = "transport: authentication handshake failed: x509: certificate signed by unknown authority (possibly because of \"x509: ECDSA verification failure\" while trying to verify candidate authority certificate \"tlsca.example.com\")"; please retry. Error: Error connecting due to rpc error: code = Internal desc = connection error: desc = "transport: authentication handshake failed: x509: certificate signed by unknown authority (possibly because of \"x509: ECDSA verification failure\" while trying to verify candidate authority certificate \"tlsca.example.com\")" Usage: peer channel create [flags] Flags: -c, --channelID string In case of a newChain command, the channel ID to create. -f, --file string Configuration transaction file generated by a tool such as configtxgen for submitting to orderer -t, --timeout int Channel creation timeout (default 5) Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --logging-level string Default logging level and overrides, see core.yaml for full syntax -o, --orderer string Ordering service endpoint --test.coverprofile string Done (default "coverage.cov") --tls Use TLS when communicating with the orderer endpoint -v, --version Display current version of fabric peer server !!!!!!!!!!!!!!! Channel creation failed !!!!!!!!!!!!!!!! ================== ERROR !!! FAILED to execute End-2-End Scenario ==================
都是因為環境遺留的問題
解決方式,重新關閉網絡
./network_setup.sh down mychannel
如果是在阿里雲機器上部署fabric ,在e2e_cli 啟動網絡時,遇到以下錯誤
2018-01-26 05:03:26.153 UTC [msp] GetDefaultSigningIdentity -> DEBU 00d Obtaining default signing identity 2018-01-26 05:03:26.153 UTC [msp/identity] Sign -> DEBU 00e Sign: plaintext: 0AC3060A1508021A06089EEDAAD30522...4570C57BBD33F75CA1D12B806981FAF1 2018-01-26 05:03:26.153 UTC [msp/identity] Sign -> DEBU 00f Sign: digest: A5892BF4C08D07882B34D959932CFA784DE00E1B29D40411B1CF8B59C16DF557 fatal error: unexpected signal during runtime execution [signal SIGSEGV: segmentation violation code=0x1 addr=0x63 pc=0x7ff4023cb259] runtime stack: runtime.throw(0xddc771, 0x2a) /opt/go/src/runtime/panic.go:566 +0x95 runtime.sigpanic() /opt/go/src/runtime/sigpanic_unix.go:12 +0x2cc .... .... .... goroutine 34 [select]: net.cgoLookupIP(0x141ef60, 0xc4203bea20, 0x7ffef3e2fa83, 0x13, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) /opt/go/src/net/cgo_unix.go:209 +0x2f5 net.lookupIP(0x141ef60, 0xc4203bea20, 0x7ffef3e2fa83, 0x13, 0x0, 0x0, 0x0, 0x0, 0x0) /opt/go/src/net/lookup_unix.go:70 +0xf9 net.glob..func11(0x141ef60, 0xc4203bea20, 0xe45178, 0x7ffef3e2fa83, 0x13, 0xc4203d46c8, 0x720699, 0xc4203d4708, 0xc4203d4718, 0x455cc0) /opt/go/src/net/hook.go:19 +0x52 net.lookupIPContext.func1(0xc42014eea0, 0x7ffef3e2fa83, 0x18, 0x0) /opt/go/src/net/lookup.go:119 +0x5c internal/singleflight.(*Group).doCall(0x143be60, 0xc4203bc870, 0x7ffef3e2fa83, 0x13, 0xc420177c20) /opt/go/src/internal/singleflight/singleflight.go:93 +0x3c created by internal/singleflight.(*Group).DoChan /opt/go/src/internal/singleflight/singleflight.go:86 +0x339 !!!!!!!!!!!!!!! Channel creation failed !!!!!!!!!!!!!!!! ================== ERROR !!! FAILED to execute End-2-End Scenario ==================
用戶則需要修改 /etc/resolv.conf 配置,將 options timeout:2 attempts:3 rotate single-request-reopen 這一行內容注釋掉
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 100.100.2.136 nameserver 100.100.2.138 # options timeout:2 attempts:3 rotate single-request-reopen
這個解決方法是從 https://yq.aliyun.com/articles/238940 看到的,作者試過,給增加 GODEBUG=netdns=go 環境變量也不好使,只會又出現以下問題
2018-01-26 05:09:10.812 UTC [msp/identity] Sign -> DEBU 00e Sign: plaintext: 0AC3060A1508021A0608F6EFAAD30522...18EB3B68613F5C67FA2AB9ACD1F9C144 2018-01-26 05:09:10.812 UTC [msp/identity] Sign -> DEBU 00f Sign: digest: E5BC4EB4ED536D53AC743587E0AD9FD2BAB2EFC9C6A1775D5BA8ADFC9252566C Error: Got unexpected status: BAD_REQUEST Usage: peer channel create [flags] Flags: -c, --channelID string In case of a newChain command, the channel ID to create. -f, --file string Configuration transaction file generated by a tool such as configtxgen for submitting to orderer -t, --timeout int Channel creation timeout (default 5) Global Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --logging-level string Default logging level and overrides, see core.yaml for full syntax -o, --orderer string Ordering service endpoint --test.coverprofile string Done (default "coverage.cov") --tls Use TLS when communicating with the orderer endpoint -v, --version Display current version of fabric peer server !!!!!!!!!!!!!!! Channel creation failed !!!!!!!!!!!!!!!! ================== ERROR !!! FAILED to execute End-2-End Scenario ==================
而真正能夠徹底解決的方法,就是將 /etc/resolv.conf 注釋掉 options timeout:2 attempts:3 rotate single-request-reopen ,就能夠正常啟動了
參考博客:
源碼編譯fabric hyperledger 1.0:
http://www.tk4479.net/oliverlyn/article/details/78443686
http://www.tk4479.net/remote_roamer/article/details/73733426
http://www.tk4479.net/so5418418/article/details/78355868
一個系列文檔,值得學習:
http://www.bijishequ.com/authorarticle.html?author=Aberic
ubuntu 安裝 fabric 教程:
http://www.cnblogs.com/studyzy/p/7437157.html
離線安裝 docker :
http://www.yunweipai.com/archives/20324.html
安裝docker-compose:
http://blog.csdn.net/womenrendeme/article/details/76904553
錯誤文件解答:
http://blog.csdn.net/iflow/article/details/77951610
啟動 docker 的7050 和 7051 等端口的方式:
http://shouce.jb51.net/blockchain_guide/fabric/v0.6/install.html
解決 阿里雲上 啟動 e2e_cli demo 失敗的問題 【重要】
https://yq.aliyun.com/articles/238940