所有操作在centos7下完成,其他系统(ubuntu/macos..)类似,修改相应指令即可。
1. 安装Golang
在 golang官网 获取最新版本 https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
下载最新版本
1
| wget https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
|
解压
1
| sudo tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz
|
设置环境变量vi ~/.bash_profile
,增加以下内容
1 2 3 4
| export PATH=$PATH:/usr/local/go/bin export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$HOME/go/bin
|
重新载入环境变量
至此 golang 安装成功,输入go version
查看版本
2. 安装Docker
下载并安装,使用阿里的源:
1
| curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
|
替换docker hub源的阿里源,加快镜像下载速度:
1 2 3 4 5
| sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"] } EOF
|
修改完后,重启docker,查看docker版本
1 2 3
| systemctl daemon-reload sudo systemctl restart docker docker version
|
3. 安装docker-compose
1、安装pip
1
| sudo get install python-pip
|
2、 下载docker-compose
1
| curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
|
3、 将docker-compose
移动至/usr/local/bin
文件夹当中,使其作为可执行命令文件
1
| mv ./docker-compose /usr/local/bin/docker-compose
|
4、修改docker-compose
权限
1
| chmod 755 /usr/local/bin/docker-compose
|
5、查看版本
4. 下载Fabric源码
切换至go的GOROOT
路径下,即第一步安装go的时候配置的路径,也可输入go env
查看。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| [root@VM_162_219_centos ~] GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/root/go" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build440227295=/tmp/go-build -gno-record-gcc-switches"
|
切换至/usr/local/go
目录后,进入src
代码目录。如果不存在github.com
文件夹,则新建一个。在github.com
下新建hyperledger
文件夹并进入,此时所在路径为:
1
| /usr/local/go/src/github.com/hyperledger
|
在当前目录下,从github拉取fabric源码
1
| git clone https://github.com/hyperledger/fabric.git
|
本次使用1.0.0
版本(与后面安装的docker fabric镜像版本保持一致!)
1 2
| cd fabric git checkout v1.0.0
|
到此为止,fabric源码下载成功。
5. 下载Docker Fabric镜像
切换到下载的fabric源码目录
1
| cd /usr/local/go/src/github.com/hyperledger/fabric/examples/e2e_cli
|
利用源码中的脚本下载1.0.0
版本的fabric docker镜像
1
| source ./download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
|
下载完后可以执行docker images
查看镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| [root@VM_162_219_centos e2e_cli] REPOSITORY TAG IMAGE ID CREATED SIZE dev-peer1.org2.example.com-mycc-1.0 latest 93a9797573ad 2 hours ago 173 MB dev-peer0.org1.example.com-mycc-1.0 latest bef7e3211370 2 hours ago 173 MB dev-peer0.org2.example.com-mycc-1.0 latest 2884f1a84570 2 hours ago 173 MB docker.io/hello-world latest f2a91732366c 4 months ago 1.85 kB docker.io/hyperledger/fabric-tools x86_64-1.0.0 0403fd1c72c7 8 months ago 1.32 GB hyperledger/fabric-tools latest 0403fd1c72c7 8 months ago 1.32 GB docker.io/hyperledger/fabric-couchdb x86_64-1.0.0 2fbdbf3ab945 8 months ago 1.48 GB hyperledger/fabric-couchdb latest 2fbdbf3ab945 8 months ago 1.48 GB docker.io/hyperledger/fabric-kafka x86_64-1.0.0 dbd3f94de4b5 8 months ago 1.3 GB hyperledger/fabric-kafka latest dbd3f94de4b5 8 months ago 1.3 GB docker.io/hyperledger/fabric-zookeeper x86_64-1.0.0 e545dbf1c6af 8 months ago 1.31 GB hyperledger/fabric-zookeeper latest e545dbf1c6af 8 months ago 1.31 GB docker.io/hyperledger/fabric-orderer x86_64-1.0.0 e317ca5638ba 8 months ago 179 MB hyperledger/fabric-orderer latest e317ca5638ba 8 months ago 179 MB hyperledger/fabric-peer latest 6830dcd7b9b5 8 months ago 182 MB docker.io/hyperledger/fabric-peer x86_64-1.0.0 6830dcd7b9b5 8 months ago 182 MB docker.io/hyperledger/fabric-javaenv x86_64-1.0.0 8948126f0935 8 months ago 1.42 GB hyperledger/fabric-javaenv latest 8948126f0935 8 months ago 1.42 GB docker.io/hyperledger/fabric-ccenv x86_64-1.0.0 7182c260a5ca 8 months ago 1.29 GB hyperledger/fabric-ccenv latest 7182c260a5ca 8 months ago 1.29 GB docker.io/hyperledger/fabric-ca x86_64-1.0.0 a15c59ecda5b 8 months ago 238 MB hyperledger/fabric-ca latest a15c59ecda5b 8 months ago 238 MB docker.io/hyperledger/fabric-baseos x86_64-0.3.1 4b0cab202084 10 months ago 157 MB
|
6. 启动Fabric网络
启动Fabric网络实际上是启动下载的Fabric Docker镜像,并执行一系列命令
1 2 3
| cd /usr/local/go/src/github.com/hyperledger/fabric/examples/e2e_cli
./network_setup.sh up
|
最终出现以下提示时启动成功:
1 2 3 4 5 6 7 8 9 10
| ===================== Query on PEER3 on channel 'mychannel' is successful =====================
===================== All GOOD, End-2-End execution completed =====================
_____ _ _ ____ _____ ____ _____ | ____| | \ | | | _ \ | ____| |___ \ | ____| | _| | \| | | | | | _____ | _| __) | | _| | |___ | |\ | | |_| | |_____| | |___ / __/ | |___ |_____| |_| \_| |____/ |_____| |_____| |_____|
|
启动成功后,可以control+c
退出,fabric并不会中止,而是在后台运行在docker中。
7. 使用合约测试Fabric网络
前面下载的fabric源码中包含了一个合约示例,当启动Fabric网络之后,启动了一个名为mychannel
的channel及一个名为mycc
的链码。
首先连接到Fabric网络所在的docker容器:
1
| docker exec -it cli bash
|
该mycc
的合约定义了两个账户a和b,分别查看他们的账户余额:
1 2 3
| peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
|
Query Result
为账户余额
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer 2018-04-03 12:03:14.367 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-04-03 12:03:14.367 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C0882DB8DD60510...6D7963631A0A0A0571756572790A0161 2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 51A66C3205697E626DDDD25E6995FBCC04BDBD6D602E68959D2995EC13DEC8E1 Query Result: 90 2018-04-03 12:03:14.373 UTC [main] main -> INFO 007 Exiting..... 2018-04-03 12:03:14.373 UTC [main] main -> INFO 007 Exiting..... root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer 2018-04-03 12:08:36.201 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-04-03 12:08:36.201 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A94070A6608031A0B08C4DD8DD60510...6D7963631A0A0A0571756572790A0162 2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 0E7915778933F605D5B7D04D52FFF7608F390E0AF2A7B37C6C3FA6027747DF56 Query Result: 210 2018-04-03 12:08:36.207 UTC [main] main -> INFO 007 Exiting.....
|
使用合约的invoke
方法,从a账户转账20给b账户:
1
| 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和b的余额,此时余额已经发生变化。转账成功。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer 2018-04-03 12:03:14.367 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-04-03 12:03:14.367 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C0882DB8DD60510...6D7963631A0A0A0571756572790A0161 2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 51A66C3205697E626DDDD25E6995FBCC04BDBD6D602E68959D2995EC13DEC8E1 Query Result: 70 2018-04-03 12:03:14.373 UTC [main] main -> INFO 007 Exiting..... root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer 2018-04-03 12:08:36.201 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-04-03 12:08:36.201 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A94070A6608031A0B08C4DD8DD60510...6D7963631A0A0A0571756572790A0162 2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 0E7915778933F605D5B7D04D52FFF7608F390E0AF2A7B37C6C3FA6027747DF56 Query Result: 230 2018-04-03 12:08:36.207 UTC [main] main -> INFO 007 Exiting.....
|
至此fabric环境搭建成功,测试成功。
退出docker容器exit
停止fabric docker进程,中止fabric网络
1 2 3
| cd /usr/local/go/src/github.com/hyperledger/fabric/examples/e2e_cli
./network_setup.sh down
|