centos7 上部署和使用 fabric1.1.0

本文记录如何在 centos7 虚拟机上部署并试用fabric1.1.0网络。

由于是 Centos7,官网提到的curl和python版本都是正确的,不用折腾。另外可执行文件社区都已经预编译好了,如果只是试用可以暂时不安装go,直接开始部署:

1. 安装docker

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager  --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install -y docker-ce-18.03.0.ce

为了加速docker拉取镜像,配置国内镜像

$ mkdir -p /etc/docker/
$ cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
EOF

$ systemctl enable docker
$ systemctl restart docker
$ docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.03.0-ce
....
Registry Mirrors:
 https://docker.mirrors.ustc.edu.cn  # ------- 确认这里生效
Live Restore Enabled: false

2. 安装docker-compose

$ sudo curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose version
docker-compose version
docker-compose version 1.20.1, build 5d8c71b
docker-py version: 3.1.4
CPython version: 3.6.4
OpenSSL version: OpenSSL 1.0.1t  3 May 2016

3. 下载二进制文件和docker镜像

准备工作目录为 fabric

$ cd ~
$ mkdir fabric
$ cd fabric
# 下载1.1 版本的bootstrap.sh 脚本
$ wget https://raw.githubusercontent.com/hyperledger/fabric/release-1.1/scripts/bootstrap.sh
$ bash bootstrap.sh

bootstrap 脚本会执行如下动作:
1. 下载预编译好的fabric和fabric-ca
2. 拉取fabric 1.1.0 的docker镜像
3. 拉取fabric 依赖的第三方软件镜像,如couchdb,kafka,zookeeper,版本号都是0.4.6

执行完成后查看当前目录的bin,应该会有如下文件

ls bin/ -lh
总用量 146M
-rwxrwxr-x 1 1001 1001 23M 3月  16 06:13 configtxgen
-rwxrwxr-x 1 1001 1001 24M 3月  16 06:13 configtxlator
-rwxrwxr-x 1 1001 1001 12M 3月  16 06:13 cryptogen
-rwxrwxr-x 1 1001 1001 20M 3月  16 09:33 fabric-ca-client
-rwxrwxr-x 1 1001 1001 757 4月  11 16:00 get-docker-images.sh
-rwxrwxr-x 1 1001 1001 31M 3月  16 06:14 orderer
-rwxrwxr-x 1 1001 1001 38M 3月  16 06:14 peer

4. 运行 fabric-sample

$ yum install -y git
$ git clone https://github.com/hyperledger/fabric-samples.git
$ mv bin fabric-samples/
$ cd fabric-samples
$ git checkout v1.1.0
$ cd first-network/

接下来的内容和官方教程就很像了:

./byfn.sh -m generate
Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n]
proceeding ...
/root/fabric/fabric-samples/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

/root/fabric/fabric-samples/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2018-04-11 16:21:21.523 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-04-11 16:21:21.531 CST [msp] getMspConfig -> INFO 002 Loading NodeOUs
2018-04-11 16:21:21.531 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2018-04-11 16:21:21.531 CST [common/tools/configtxgen] doOutputBlock -> INFO 004 Generating genesis block
2018-04-11 16:21:21.532 CST [common/tools/configtxgen] doOutputBlock -> INFO 005 Writing genesis block
+ res=0
+ set +x
....
$ ./byfn.sh -m up -i 1.1.0
Starting with channel 'mychannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n]
proceeding ...
2018-04-11 09:03:16.069 UTC [main] main -> INFO 001 Exiting.....
LOCAL_VERSION=1.1.0
DOCKER_IMAGE_VERSION=1.1.0
/root/fabric/fabric-samples/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
...
...
===================== Query on peer1.org2 on channel 'mychannel' is successful =====================

========= All GOOD, BYFN execution completed ===========


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

查看运行的docker容器

docker ps
CONTAINER ID        IMAGE                                                                                                  COMMAND                  CREATED              STATUS              PORTS                                              NAMES
2883f3826ab6        dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"   About a minute ago   Up About a minute                                                      dev-peer1.org2.example.com-mycc-1.0
83fb2369b907        dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"   2 minutes ago        Up 2 minutes                                                           dev-peer0.org1.example.com-mycc-1.0
ee7b1e26f81f        dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b   "chaincode -peer.add…"   2 minutes ago        Up 2 minutes                                                           dev-peer0.org2.example.com-mycc-1.0
5aeb6c7202f6        hyperledger/fabric-tools:x86_64-1.1.0                                                                  "/bin/bash"              2 minutes ago        Up 2 minutes                                                           cli
70c195a04681        hyperledger/fabric-peer:x86_64-1.1.0                                                                   "peer node start"        3 minutes ago        Up 2 minutes        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
d8035035693b        hyperledger/fabric-peer:x86_64-1.1.0                                                                   "peer node start"        3 minutes ago        Up 2 minutes        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
db9f922e099a        hyperledger/fabric-peer:x86_64-1.1.0                                                                   "peer node start"        3 minutes ago        Up 2 minutes        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
9008ca1ece9d        hyperledger/fabric-peer:x86_64-1.1.0                                                                   "peer node start"        3 minutes ago        Up 2 minutes        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
9b6a8c6346b8        hyperledger/fabric-orderer:x86_64-1.1.0                                                                "orderer"                3 minutes ago        Up 2 minutes        0.0.0.0:7050->7050/tcp                             orderer.example.com

5. 使用 cli

docker exec -it cli /bin/bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash"

这么复杂的方法是要绕开 docker的bug:会导致attach进去之后终端特别诡异。

查看已经安装的chaincode:

# peer chaincode list --installed
Get installed chaincodes on peer:
Name: mycc, Version: 1.0, Path: github.com/chaincode/chaincode_example02/go/, Id: 476fca1a949274001971f1ec2836cb09321f0b71268b3762d68931c93f218134
2018-04-11 09:14:26.305 UTC [main] main -> INFO 001 Exiting.....

查询账户余额

# peer chaincode query -C mychannel -n mycc -c '{"Args": ["query", "a"]}'
2018-04-11 09:15:07.853 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-04-11 09:15:07.853 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 90
2018-04-11 09:15:07.857 UTC [main] main -> INFO 003 Exiting.....

# peer chaincode query -C mychannel -n mycc -c '{"Args": ["query", "b"]}'
2018-04-11 09:15:39.147 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-04-11 09:15:39.147 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 210
2018-04-11 09:15:39.152 UTC [main] main -> INFO 003 Exiting.....

转账

# peer chaincode invoke   --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","90"]}'
2018-04-11 09:16:44.028 UTC [chaincodeCmd] InitCmdFactory -> INFO 001 Get chain(mychannel) orderer endpoint: orderer.example.com:7050
2018-04-11 09:16:44.032 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default escc
2018-04-11 09:16:44.032 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default vscc
2018-04-11 09:16:44.038 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 004 Chaincode invoke successful. result: status:200
2018-04-11 09:16:44.039 UTC [main] main -> INFO 005 Exiting.....

查询a账户余额

# peer chaincode query -C mychannel -n mycc -c '{"Args": ["query", "a"]}'
2018-04-11 09:17:23.511 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-04-11 09:17:23.511 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 0
2018-04-11 09:17:23.516 UTC [main] main -> INFO 003 Exiting.....

关闭走人

./byfn.sh -m down

玩得快乐!

参考文档:

发表评论

电子邮件地址不会被公开。 必填项已用*标注