hyperledger fabric 1.0.5 分布式部署 (一)

环境是个人虚拟机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

posted @ 2018-01-26 13:21  chenfool  阅读(8081)  评论(1编辑  收藏  举报