今天尝试了Mac系统下跑Hyperledger Fabric,过程中踩了一些坑,特地整理出步骤方便大家学习,下面就开始吧。
1. 前提条件
1.1 安装Go并配置环境变量
Mac可以直接通过Homebrew安装go环境:brew install go
或者前往官网下载安装包
安装完毕后在命令行输入go version
检查是否安装成功:
➜ go version
go version go1.10.1 darwin/amd64
然后开始配置环境变量:
-
先新建一个Go的工作空间文件夹,文件夹路径建议放在$HOME下:
➜ cd $HOME ➜ mkdir go
-
进入
$HOME
目录下:➜ cd $HOME
编辑
.bash_profile
文件,如果没有请新建。在文件末尾加上:#GOPATH export GOPATH=$HOME/go #GOBIN export GOBIN=$GOPATH/bin export PATH=$PATH:$GOBIN
编辑之后保存,然后执行
source
命令,使之生效:source .bash_profile
-
通过
go env
检查:➜ go env OARCH="amd64" GOBIN="/Users/jianjiangwang/go/bin" GOCACHE="/Users/jianjiangwang/Library/Caches/go-build" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/jianjiangwang/go" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
1.2 安装Docker并更换镜像源
在官网下载Docker CE for Mac完成后安装。
安装后可以在命令行中检查docker
和docker-compose
的版本:
➜ docker --version
Docker version 17.12.0-ce, build c97c6d6
➜ docker-compose --version
docker-compose version 1.18.0, build 8dd22a9
开启Docker CE for Mac, 在运行Hyperledger Fabric时,请确保Docker处于开启状态:
Docker拉镜像时会从海外的节点拉取,因为墙的原因,从海外节点拉取简直就是龟速,所以建议将Docker的镜像源更换至aliyun提供的节点
更换镜像源步骤:
可以直接使用我的专属加速器
https://aic2v8yz.mirror.aliyuncs.com
,或者去阿里云镜像服务自己申请专属加速器点击Docker客户端图标 >> Preference >> Daemon , 然后在
Registry mirrors
中加入加速器链接https://aic2v8yz.mirror.aliyuncs.com
:
2. 下载Hyperledger Fabric项目源代码
进入Go的工作空间, 为fabric创建项目路径:
➜ cd $GOPATH
➜ mkdir -p src/github.com/hyperledger
➜ cd src/github.com/hyperledger
➜ pwd
/Users/jianjiangwang/go/src/github.com/hyperledger
从git上拉取Hyperledger Fabric:
➜ git clone git@github.com:hyperledger/fabric.git
➜ ls
fabric
进入项目文件夹,查看tag:
➜ cd fabric
➜ git tag
baseimage-v0.0.11
v0.6.0-preview
v0.6.1-preview
v1.0.0
v1.0.0-alpha
v1.0.0-alpha2
v1.0.0-beta
v1.0.0-rc1
v1.0.1
v1.0.2
v1.0.3
v1.0.4
v1.0.5
v1.0.6
v1.1.0
v1.1.0-alpha
v1.1.0-preview
v1.1.0-rc1
上面的tag表示相应的fabric项目的版本, fabric项目现在还处于早期发展阶段, 修改频繁且不一定能向下兼容, 所以在继续之前请先确定一个版本,避免后面踩坑。 笔者在这里使用v1.0.0
➜ git checkout v1.0.0
➜ git branch
* (HEAD detached at v1.0.0)
release-1.1
3. 启动项目
进入fabric文件夹下的examples/e2e_cli
, 下面我们要测试e2e_cli
这个demo:
➜ cd examples/e2e_cli
➜ pwd
/Users/jianjiangwang/go/src/github.com/hyperledger/fabric/examples/e2e_cli
执行download-dockerimage.sh,程序将会通过docker拉取项目所需镜像, 为了统一版本,请指定拉取镜像的版本号:
➜ chmod +x download-dockerimages.sh
➜ ./download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
等待镜像拉取完成:
===> List out hyperledger docker images
hyperledger/fabric-tools latest 0403fd1c72c7 8 months ago 1.32GB
hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 8 months ago 1.32GB
hyperledger/fabric-couchdb latest 2fbdbf3ab945 8 months ago 1.48GB
hyperledger/fabric-couchdb x86_64-1.0.0 2fbdbf3ab945 8 months ago 1.48GB
hyperledger/fabric-kafka latest dbd3f94de4b5 8 months ago 1.3GB
hyperledger/fabric-kafka x86_64-1.0.0 dbd3f94de4b5 8 months ago 1.3GB
hyperledger/fabric-zookeeper latest e545dbf1c6af 8 months ago 1.31GB
hyperledger/fabric-zookeeper x86_64-1.0.0 e545dbf1c6af 8 months ago 1.31GB
hyperledger/fabric-orderer latest e317ca5638ba 8 months ago 179MB
hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 8 months ago 179MB
hyperledger/fabric-peer latest 6830dcd7b9b5 8 months ago 182MB
hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 8 months ago 182MB
hyperledger/fabric-javaenv latest 8948126f0935 8 months ago 1.42GB
hyperledger/fabric-javaenv x86_64-1.0.0 8948126f0935 8 months ago 1.42GB
hyperledger/fabric-ccenv latest 7182c260a5ca 8 months ago 1.29GB
hyperledger/fabric-ccenv x86_64-1.0.0 7182c260a5ca 8 months ago 1.29GB
hyperledger/fabric-ca latest a15c59ecda5b 8 months ago 238MB
hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 8 months ago 238MB
hyperledger/fabric-baseos x86_64-0.3.1 4b0cab202084 10 months ago 157MB
现在执行完整脚本:
➜ ./network_setup.sh up <channel-ID>
如果没有设置channel-ID
参数,channel名默认是mychannel
。脚本执行成功后输出:
===================== Query on PEER3 on channel 'mychannel' is successful =====================
===================== All GOOD, End-2-End execution completed =====================
_____ _ _ ____ _____ ____ _____
| ____| | \ | | | _ \ | ____| |___ \ | ____|
| _| | \| | | | | | _____ | _| __) | | _|
| |___ | |\ | | |_| | |_____| | |___ / __/ | |___
|_____| |_| \_| |____/ |_____| |_____| |_____|
此时,网络启动运行并测试成功。
如果启动失败请查看错误提示信息。很多情况下都是因为项目的git tag版本号与拉取的docker镜像版本不同导致的
停止网络:
# 在e2e_cli目录下
➜ ./network_setup.sh down
➜ docker rm -f $(docker ps -aq)
删除镜像:
执行docker images
命令查看Chaincode镜像,类似输出如下:
REPOSITORY TAG IMAGE ID CREATED SIZE
dev-peer1.org2.example.com-mycc-1.0 latest 3d58e4dac733 4 hours ago 173MB
dev-peer0.org1.example.com-mycc-1.0 latest 91bd46f62e1e 4 hours ago 173MB
dev-peer0.org2.example.com-mycc-1.0 latest 56d327659590 4 hours ago 173MB
hyperledger/fabric-tools latest 0403fd1c72c7 8 months ago 1.32GB
hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 8 months ago 1.32GB
hyperledger/fabric-couchdb latest 2fbdbf3ab945 8 months ago 1.48GB
hyperledger/fabric-couchdb x86_64-1.0.0 2fbdbf3ab945 8 months ago 1.48GB
hyperledger/fabric-kafka latest dbd3f94de4b5 8 months ago 1.3GB
hyperledger/fabric-kafka x86_64-1.0.0 dbd3f94de4b5 8 months ago 1.3GB
hyperledger/fabric-zookeeper latest e545dbf1c6af 8 months ago 1.31GB
hyperledger/fabric-zookeeper x86_64-1.0.0 e545dbf1c6af 8 months ago 1.31GB
hyperledger/fabric-orderer latest e317ca5638ba 8 months ago 179MB
hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 8 months ago 179MB
hyperledger/fabric-peer latest 6830dcd7b9b5 8 months ago 182MB
hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 8 months ago 182MB
hyperledger/fabric-javaenv latest 8948126f0935 8 months ago 1.42GB
hyperledger/fabric-javaenv x86_64-1.0.0 8948126f0935 8 months ago 1.42GB
hyperledger/fabric-ccenv latest 7182c260a5ca 8 months ago 1.29GB
hyperledger/fabric-ccenv x86_64-1.0.0 7182c260a5ca 8 months ago 1.29GB
hyperledger/fabric-ca latest a15c59ecda5b 8 months ago 238MB
hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 8 months ago 238MB
hyperledger/fabric-baseos x86_64-0.3.1 4b0cab202084 10 months ago 157MB
然后删除上面三个e2e-cli
创建的镜像:
docker rmi -f 3d58e4dac733 91bd46f62e1e 56d327659590