Hyperledger Fabric的安装和体验

2018-03-07 阅读: Hyperledger Fabric

0.Hyperledger Fabric学习笔记目录

  1. Hyperledger Fabric的安装和体验
  2. 初识Hyperledger Fabric网络节点和交易流程
  3. Hyperledger Fabric 1.0的架构
  4. 构建第一个Hyperledger Fabric网络

Hyperledger(超级账本)是Linux基金会启动的区块链项目,Hyperledger Fabric是Hyperledger中的一个区块链项目。本篇作为个人学习Hyperledger Fabric的开始,将简单安装和体验一下。

1.安装Docker和docker-compose

这里安装最新的Docker CE 1712版本:

yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

yum install docker-ce

systemctl start docker
systemctl enable docker

安装docker-compose 1.19.0:

curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose -v
docker-compose version 1.19.0, build 9e633ef

2.最小化安装Hyperledger Fabric

接下来在本地最小化安装一下Hyperledger Fabric 1.0.6。 首先下载源码并解压缩:

wget https://github.com/hyperledger/fabric/archive/v1.0.6.tar.gz
tar -zxvf v1.0.6.tar.gz

2.1 pull dokcer镜像

解压缩的scripts目录中有bootstrap脚本:

cd fabric-1.0.6/scripts

执行bootstrap.sh脚本可下载对应平台的fabric二进制版本和pull相关的docker镜像。

./bootstrap.sh

查看下载fabric二进制文件:

bin/
configtxgen  configtxlator  cryptogen  get-byfn.sh  get-docker-images.sh  orderer  peer

查看pull的docker镜像:

docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
hyperledger/fabric-couchdb   latest              380446aa57b6        2 weeks ago         1.5GB
hyperledger/fabric-couchdb   x86_64-1.0.6        380446aa57b6        2 weeks ago         1.5GB
hyperledger/fabric-orderer   latest              659d92c1be85        2 weeks ago         151MB
hyperledger/fabric-orderer   x86_64-1.0.6        659d92c1be85        2 weeks ago         151MB
hyperledger/fabric-peer      latest              28c7c07db540        2 weeks ago         154MB
hyperledger/fabric-peer      x86_64-1.0.6        28c7c07db540        2 weeks ago         154MB
hyperledger/fabric-ccenv     latest              2e695012bcff        2 weeks ago         1.28GB
hyperledger/fabric-ccenv     x86_64-1.0.6        2e695012bcff        2 weeks ago         1.28GB

因为这里我们只是基于docker-compose做最小安装和体检,所以在运行bootstrap脚本时可以跳过下载二进制文件的步骤,跳过的方式注释掉bootstrap.sh脚本中curl进行二进制下载的命令

2.2 部署Hyperledger Fabric的网络

https://github.com/hyperledger/fabric-samples已经提供了很多配置好的docker-compose.yml,可以直接体验使用。

下载fabric-samples:

wget https://github.com/hyperledger/fabric-samples/archive/v1.0.6.tar.gz -O fabric-samples-v1.0.6.tar.gz
tar -zxvf fabric-samples-v1.0.6.tar.gz

ls fabric-samples-1.0.6/
balance-transfer  chaincode                 fabcar         high-throughput  MAINTAINERS.md  scripts
basic-network     chaincode-docker-devmode  first-network  LICENSE          README.md

接下来体验basic-network的sample:

cd fabric-samples-1.0.6/basic-network/

docker-compose up -d
Creating network "net_basic" with the default driver
Pulling ca.example.com (hyperledger/fabric-ca:latest)...
ERROR: manifest for hyperledger/fabric-ca:latest not found

上面执行docker-compose时报了找不到hyperledger/fabric-ca:latest镜像的错误,docker-hub中确实没有lastest的镜像,因此这里修改docker-compose.yml文件,将其中各个容器使用的镜像都指定具体的tag号x86_64-1.0.6

重新运行docker-compose:

docker-compose up -d

共启动如下5个容器:

docker ps
CONTAINER ID        IMAGE                                     COMMAND                  CREATED             STATUS              PORTS                                            NAMES
afe407ae5e26        hyperledger/fabric-peer:x86_64-1.0.6      "peer node start"        46 seconds ago      Up 45 seconds       0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
33639c048aa7        hyperledger/fabric-tools:x86_64-1.0.6     "/bin/bash"              48 seconds ago      Up 46 seconds                                                        cli
6f1b776e18a4        hyperledger/fabric-orderer:x86_64-1.0.6   "orderer"                48 seconds ago      Up 46 seconds       0.0.0.0:7050->7050/tcp                           orderer.example.com
ead19674da34        hyperledger/fabric-couchdb:x86_64-1.0.6   "tini -- /docker-ent…"   48 seconds ago      Up 46 seconds       4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp       couchdb
9117f3913913        hyperledger/fabric-ca:x86_64-1.0.6        "sh -c 'fabric-ca-se…"   48 seconds ago      Up 46 seconds       0.0.0.0:7054->7054/tcp                           ca.example.com

3.体验fabric

执行下面的命令创建channel:

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx

执行下面的命令将peer0.org1.example.com加入到通道中:

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel join -b mychannel.block

为peer节点peer0.org1.example.com安装chaincode:

docker exec -it cli sh

peer chaincode install -n mychaincode -v v0 -p github.com/chaincode_example02

实例化chaincode:

peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mychaincode -v v0 -c '{"Args": ["init", "foo", "100", "bar", "50"]}'

查询foo和bar的值:

peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "foo"]}'
Query Result: 100

peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "bar"]}'
Query Result: 50

调用chaincode从foo转移10到bar:

peer chaincode invoke -C mychannel -n mychaincode -v v0 -c '{"Args": ["invoke", "foo", "bar", "10"]}'

重新查询foo和bar的值:

peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "foo"]}'
Query Result: 90

peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "bar"]}'
Query Result: 60

通过docker ps查看多了一个dev-peer0.org1.example.com-mychaincode-v0容器,可以看出Hyperledger Fabric 1.0中的chaincode也是运行在容器中的。

参考

标题:Hyperledger Fabric的安装和体验
本文链接:https://blog.frognew.com/2018/03/hyperledger-fabric.html
转载请注明出处。

目录