centos7安装fabric

96
四丢皮得
2018.04.05 21:50 字数 1058

0 权限

登录管理员权限

$ sudo -s

1 基本工具

  • git
$ yum install git
  • curl
$ yum install curl
  • pip
$ yum -y install epel-release
$ yum install python-pip
$ pip install --upgrade pip

2 通用语言

  • Docker
  1. 更新软件包
$ yum update
  1. 卸载旧版本
    如果有的话,卸载旧版本
$ yum remove docker  docker-common docker-selinux docker-engine
  1. 安装需要的软件包
    yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
$ yum install -y yum-utils device-mapper-persistent-data lvm2
  1. 设置yum源
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. 查看所有docker版本
$ yum list docker-ce --showduplicates | sort -r
  1. 安装docker
$ yum install docker-ce

可安装最新版

$ yum install <FQPN>

<FQPN>可指定要安装的版本,例如docker-ce-17.12.0.ce

  1. 验证安装是否成功
$ docker version
[root@hadoop ~]# docker version
Client:
 Version:   18.03.0-ce
 API version:   1.37
 Go version:    go1.9.4
 Git commit:    0520e24
 Built: Wed Mar 21 23:09:15 2018
 OS/Arch:   linux/amd64
 Experimental:  false
 Orchestrator:  swarm

Server:
 Engine:
  Version:  18.03.0-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.4
  Git commit:   0520e24
  Built:    Wed Mar 21 23:13:03 2018
  OS/Arch:  linux/amd64
  Experimental: false

  1. 启动并加入开机启动
$ systemctl start docker
$ systemctl enable docker
  • docker-compose
$ pip install docker-compose
$ docker-compose -version
[root@hadoop ~]# docker-compose -version
docker-compose version 1.20.1, build 5d8c71b
  • go语言
  1. 下载安装包
    可前往https://golang.org/dl/下载最新版安装包
  2. 解压
$ tar -C /usr/local -zxf go1.10.1.linux-amd64.tar.gz
  1. 配置环境变量
$ vim /etc/profile

写入

#go
export PATH=$PATH:/usr/local/go/bin 
export GOROOT=/usr/local/go 
export GOPATH=$HOME/go 
export PATH=$PATH:$HOME/go/bin

然后

$ source /etc/profile
  1. 查看go语言版本
$ go version
[root@hadoop test1]# go version
go version go1.10.1 linux/amd64
  • Node.js
  1. 下载Node.js
    前往Node.js选择对应版本下载
    笔者选用
$ wget https://npm.taobao.org/mirrors/node/v9.9.0/node-v9.9.0.tar.gz
  1. 解压
$ tar -C /usr/local -xzf node-v9.9.0.tar.gz
  1. 安装编译软件
$ yum install gcc gcc-c++
$ gcc -v
[root@hadoop test1]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 

4.8.5版本过低,若已为最新版请跳过至步骤,升级gcc,前往http://ftp.gnu.org/gnu/gcc/下载你想要的版本,笔者选择gcc-7.3.0

$ wget http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz
$ tar -C /usr/local -xzf gcc-7.3.0.tar.gz
$ cd /usr/local/gcc-7.3.0
$ ./contrib/download_prerequisites  
$ mkdir build  
$ cd build  
$ ../configure -enable-checking=release -enable-languages=c,c++ disable-multilib  
$ yum groupinstall "Development Tools" 
$ make           #这个步骤是真的耗时长,建议你去跑几圈锻炼下身体
$ make install 
$ reboot           #一定要记得重启

重启系统

$ gcc -v
[root@hadoop test1]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
[root@hadoop test1]# 

升级成功

  1. 继续安装nodejs
$ sudo -s
$ cd /usr/local/node-v9.9.0/
$ ./configure
$ make              #同样需要等待30分钟左右
$ make install
$ reboot

重启后

$ node -v
[test1@hadoop ~]$ node -v
v9.9.0
[test1@hadoop ~]$ 

安装成功!

  • npm
$ npm install npm -g
$ npm -v
[root@hadoop test1]# npm -v
5.8.0
[root@hadoop test1]# 

3 fabric

  • 下载fabric源码
$ mkdir -p ~/go/src/github.com/hyperledger 
$ cd ~/go/src/github.com/hyperledger 
$ git clone https://github.com/hyperledger/fabric.git

由于Fabric一直在更新,所有我们并不需要最新最新的源码,需要切换到v1.0.0版本的源码即可:

$ cd ~/go/src/github.com/hyperledger/fabric
$ git checkout v1.0.0
  • 下载docker镜像
$ cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
$ source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

这样就可以下载所有需要的Fabric Docker镜像了。由于我们设置了国内的镜像,所以下载应该是比较快的。
下载完毕后,我们运行以下命令检查下载的镜像列表:

[root@hadoop e2e_cli]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
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
  • 启动Fabric网络并完成ChainCode的测试
    我们仍然停留在e2e_cli文件夹,这里提供了启动、关闭Fabric网络的自动化脚本。我们要启动Fabric网络,并自动运行Example02 ChainCode的测试,执行一个命令:
$ ./network_setup.sh up

这个做了以下操作:

7.1编译生成Fabric公私钥、证书的程序,程序在目录:fabric/release/linux-amd64/bin
7.2基于configtx.yaml生成创世区块和通道相关信息,并保存在channel-artifacts文件夹。
7.3基于crypto-config.yaml生成公私钥和证书信息,并保存在crypto-config文件夹中。
7.4基于docker-compose-cli.yaml启动1Orderer+4Peer+1CLI的Fabric容器。
7.5在CLI启动的时候,会运行scripts/script.sh文件,这个脚本文件包含了创建Channel,加入Channel,安装Example02,运行Example02等功能。
最后运行完毕,我们可以看到这样的界面:


===================== All GOOD, End-2-End execution completed ===================== 


 _____   _   _   ____            _____   ____    _____ 
| ____| | \ | | |  _ \          | ____| |___ \  | ____|
|  _|   |  \| | | | | |  _____  |  _|     __) | |  _|  
| |___  | |\  | | |_| | |_____| | |___   / __/  | |___ 
|_____| |_| \_| |____/          |_____| |_____| |_____|

如果您看到这个界面,这说明我们整个Fabric网络已经通了。

  • 测试fabric网络
    我们仍然是以现在安装好的Example02为例,在官方例子中,channel名字是mychannel,链码的名字是mycc。我们首先进入CLI,我们重新打开一个命令行窗口,输入:
$ docker exec -it cli bash

运行以下命令可以查询a账户的余额:

$ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
root@229d9708f2d6:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2018-04-05 13:37:15.815 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-05 13:37:15.815 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-05 13:37:15.815 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-05 13:37:15.815 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-05 13:37:15.816 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A91070A6708031A0C088BCD98D60510...6D7963631A0A0A0571756572790A0161 
2018-04-05 13:37:15.816 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 92EC67AE9246EEBC7ED4A0CD7791A0CBD0DAF527DD551571599719EEF2E5841E 
Query Result: 90
2018-04-05 13:37:15.828 UTC [main] main -> INFO 007 Exiting.....

看到query result:90
然后,我们试一试把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"]}'
root@229d9708f2d6:/opt/gopath/src/github.com/hyperledger/fabric/peer# 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"]}'
2018-04-05 13:37:41.751 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-05 13:37:41.751 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-05 13:37:41.755 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-05 13:37:41.755 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-05 13:37:41.756 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A91070A6708031A0C08A5CD98D60510...696E766F6B650A01610A01620A023230 
2018-04-05 13:37:41.756 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: DAEF933C0B4BD92307A73AAFBA3DB948F9E81E88DE4EB9C487259714048D8E0E 
2018-04-05 13:37:41.786 UTC [msp/identity] Sign -> DEBU 007 Sign: plaintext: 0A91070A6708031A0C08A5CD98D60510...7E0DF0FFBE4100C6C5B7253BFD8566E2 
2018-04-05 13:37:41.786 UTC [msp/identity] Sign -> DEBU 008 Sign: digest: E8808985B5D74D1018E15F5256CBB0FEBE6845060583E1B08E2E1FD958006ED5 
2018-04-05 13:37:41.791 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> DEBU 009 ESCC invoke result: version:1 response:<status:200 message:"OK" > payload:"\n \214\3535\005\234\277\371\022\322\226\257R\325U\022\360\232\006\024\231\031\231\3022\021\004?\375\243\031\001\302\022Y\nE\022\024\n\004lscc\022\014\n\n\n\004mycc\022\002\010\003\022-\n\004mycc\022%\n\007\n\001a\022\002\010\004\n\007\n\001b\022\002\010\004\032\007\n\001a\032\00270\032\010\n\001b\032\003230\032\003\010\310\001\"\013\022\004mycc\032\0031.0" endorsement:<endorser:"\n\007Org1MSP\022\374\005-----BEGIN -----\nMIICGDCCAb+gAwIBAgIQHNKohFjcEW8G4Ruyy7PLTjAKBggqhkjOPQQDAjBzMQsw\nCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\nYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEcMBoGA1UEAxMTY2Eu\nb3JnMS5leGFtcGxlLmNvbTAeFw0xODA0MDUxMzI4MjRaFw0yODA0MDIxMzI4MjRa\nMFsxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T\nYW4gRnJhbmNpc2NvMR8wHQYDVQQDExZwZWVyMC5vcmcxLmV4YW1wbGUuY29tMFkw\nEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEZ46H4c5saTAs2YTPbv5eh3qYHzCpL/gx\nyATveglttmpJ0TJRSKixAm+fodPz+dDTpIIpQrR6+iReP+NMHHpZJqNNMEswDgYD\nVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwKwYDVR0jBCQwIoAgQN2wPl5OuUUp\nRKHenN1YBn/lr2RBjrbe1YHuUpiYp7owCgYIKoZIzj0EAwIDRwAwRAIgC3WFnoXp\nfnKGrluuVE60sf0XkIe5zkHta0V5Kt4A9F8CIEjxn4UDX9kg7XCTw7C1O0cxXj45\nRCH2/gAaM5KdaJoF\n-----END -----\n" signature:"0D\002 6\376\177\3011\346\361\344\n\224Y5\346\352]\207/|\347\217v\265K0\267Zf\344\324\035\025 \002 *7_I\001\216E\005\013\256\232\037\002\306\256\030~\r\360\377\276A\000\306\305\267%;\375\205f\342" > 
2018-04-05 13:37:41.792 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00a Chaincode invoke successful. result: status:200 
2018-04-05 13:37:41.792 UTC [main] main -> INFO 00b Exiting.....

看到 INFO 00a Chaincode invoke successful. result: status:200
再查询a账户余额:

$ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
root@229d9708f2d6:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2018-04-05 13:37:53.579 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-05 13:37:53.579 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-05 13:37:53.579 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-05 13:37:53.579 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-05 13:37:53.580 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A91070A6708031A0C08B1CD98D60510...6D7963631A0A0A0571756572790A0161 
2018-04-05 13:37:53.580 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 7D79EF8603230FBA1B449A27FD1EF0BE8AE9EE2912078E82D52D6428068EB69D 
Query Result: 70
2018-04-05 13:37:53.590 UTC [main] main -> INFO 007 Exiting.....
root@229d9708f2d6:/opt/gopath/src/github.com/hyperledger/fabric/peer#

变为了70

到此一切正常。

  • 关闭网络
    最后我们要关闭Fabric网络,首先需要运行exit命令退出cli容器。关闭Fabric的命令与启动类似,命令为:
$ exit
$ cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli
$ ./network_setup.sh down
[root@hadoop e2e_cli]# ./network_setup.sh down
setting to default channel 'mychannel'
WARNING: The CHANNEL_NAME variable is not set. Defaulting to a blank string.
WARNING: The TIMEOUT variable is not set. Defaulting to a blank string.
Stopping cli                    ... done
Stopping peer0.org1.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping orderer.example.com    ... done
Stopping peer1.org2.example.com ... done
Stopping peer0.org2.example.com ... done
Removing cli                    ... done
Removing peer0.org1.example.com ... done
Removing peer1.org1.example.com ... done
Removing orderer.example.com    ... done
Removing peer1.org2.example.com ... done
Removing peer0.org2.example.com ... done
Removing network e2ecli_default
d96900372d3c
02cf5b58c8d1
081f2ff90a3e
Untagged: dev-peer1.org2.example.com-mycc-1.0:latest
Deleted: sha256:d79810766ed3b93f0b4f8347d8ba07a6b049ee02d61083e33747ebbff5e3a2cb
Deleted: sha256:3256f508809df7e770b4046506be5ea0dd4f7091b81cefaa08082d860763b74b
Deleted: sha256:d9a333101acbf87bef80e9ae845e5ad07e4c384ab246cbeee85bc4866c5ec0fc
Deleted: sha256:45164aa46fd054c276db82bf65a414da1fdf4383118414e6e0828c47208ce329
Deleted: sha256:057af6ec45262408b3f4b7ae897ff839433e17219d5978406c777fce3693996e
Deleted: sha256:2140d0e5697c2f4efd1a1be9ecd3b0c0e140e0ac096f378ce333486b328d549f
Deleted: sha256:1589200c0494571bc49a3efe67750d919ea619f5fa79166f8c9f537df6d1f265
Untagged: dev-peer0.org1.example.com-mycc-1.0:latest
Deleted: sha256:67155cb00497bd398323db0730c1130af72e77f601c93c8e8efb2f4f68480ed0
Deleted: sha256:8aa713e11e4474fcc6029cb010058206d5bf81021d0f5ecec30aa022105ae339
Deleted: sha256:955298af8156d591ff11d9060f979fc7f1a6505253cb7ed88d4c1677fab1f2a2
Deleted: sha256:3fde7086356e72f8dda3d88bdb08ff51854d7c5565918d67c3cb6bf9ec98cd03
Deleted: sha256:db32c7e813ba3ec0305b33679a97946fb685b74c33d8a7aa08bf41add6e14bfd
Deleted: sha256:680a7996e8d994d4b6597f0a01e74980ded89ce56791d80fd83e0ea5973c3611
Deleted: sha256:5bc478d7aff121341751a5eb61c98d282d055d6db0475d5d7579251b087515d0
Untagged: dev-peer0.org2.example.com-mycc-1.0:latest
Deleted: sha256:4fb0e99c0f1d2aa1bc8bee3dfa1b4ccb69a20a81a4c76eeba06bd7f785a4d271
Deleted: sha256:5788b7f5ffceccf08f67c83d6c00ae73f782a320d6d731674e87c1356e50c2ae
Deleted: sha256:945c3684ff06a1a1bc96c2999a443e92110f9e6c9e0bc02dbe1a8bea3673e55c
Deleted: sha256:c432a68d6c3c671527796e402d6ec12011f1e0effe3feba048131874d0eff46c
Deleted: sha256:1ed302def51d694a877a1b28630bd4b94a9a53d3a163ec349aedc5bc7a1a2d31
Deleted: sha256:78e57818eec78e8f244cde5a757885e202c44aa38389ef3da91f83d337206b0f
Deleted: sha256:1fd56da419d7fd9fa0f0e8b7274efb5d7b9082ef6881912732f0ac1502f46c5c
[root@hadoop e2e_cli]# 

结束语

到此整个安装测试结束,此篇仅为测试fabric框架在centos 7 中的安装测试。中间坑无数,如:

  • go语言需要设置GOPATH、GOROOT环境变量,否则运行./network_setup.sh up命令会出现找不到mycc链。
  • node.js需要gcc编译支持,而yum install安装的gcc版本过低,不能顺利安装,因此需要对gcc版本升级,而升级的时候make实在实在实在太慢了!!!
  • docker镜像安装,最好在后面写入版本信息,否则下载的可能与git checkout所对应版本不一致,./network_setup.sh up命令时会出现各类奇葩问题。
教程简述
Web note ad 1