Hyperledger Fabric的安装和体验

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版本:

 1yum install -y yum-utils \
 2  device-mapper-persistent-data \
 3  lvm2
 4
 5yum-config-manager \
 6    --add-repo \
 7    https://download.docker.com/linux/centos/docker-ce.repo
 8
 9yum install docker-ce
10
11systemctl start docker
12systemctl enable docker

安装docker-compose 1.19.0:

1curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2
3chmod +x /usr/local/bin/docker-compose
4
5docker-compose -v
6docker-compose version 1.19.0, build 9e633ef

2.最小化安装Hyperledger Fabric #

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

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

2.1 pull dokcer镜像 #

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

1cd fabric-1.0.6/scripts

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

1./bootstrap.sh

查看下载fabric二进制文件:

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

查看pull的docker镜像:

 1docker images
 2REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
 3hyperledger/fabric-couchdb   latest              380446aa57b6        2 weeks ago         1.5GB
 4hyperledger/fabric-couchdb   x86_64-1.0.6        380446aa57b6        2 weeks ago         1.5GB
 5hyperledger/fabric-orderer   latest              659d92c1be85        2 weeks ago         151MB
 6hyperledger/fabric-orderer   x86_64-1.0.6        659d92c1be85        2 weeks ago         151MB
 7hyperledger/fabric-peer      latest              28c7c07db540        2 weeks ago         154MB
 8hyperledger/fabric-peer      x86_64-1.0.6        28c7c07db540        2 weeks ago         154MB
 9hyperledger/fabric-ccenv     latest              2e695012bcff        2 weeks ago         1.28GB
10hyperledger/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:

1wget https://github.com/hyperledger/fabric-samples/archive/v1.0.6.tar.gz -O fabric-samples-v1.0.6.tar.gz
2tar -zxvf fabric-samples-v1.0.6.tar.gz
3
4ls fabric-samples-1.0.6/
5balance-transfer  chaincode                 fabcar         high-throughput  MAINTAINERS.md  scripts
6basic-network     chaincode-docker-devmode  first-network  LICENSE          README.md

接下来体验basic-network的sample:

1cd fabric-samples-1.0.6/basic-network/
2
3docker-compose up -d
4Creating network "net_basic" with the default driver
5Pulling ca.example.com (hyperledger/fabric-ca:latest)...
6ERROR: 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:

1docker-compose up -d

共启动如下5个容器:

1docker ps
2CONTAINER ID        IMAGE                                     COMMAND                  CREATED             STATUS              PORTS                                            NAMES
3afe407ae5e26        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
433639c048aa7        hyperledger/fabric-tools:x86_64-1.0.6     "/bin/bash"              48 seconds ago      Up 46 seconds                                                        cli
56f1b776e18a4        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
6ead19674da34        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
79117f3913913        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:

1docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/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加入到通道中:

1docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b mychannel.block

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

1docker exec -it cli sh
2
3peer chaincode install -n mychaincode -v v0 -p github.com/chaincode_example02

实例化chaincode:

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

查询foo和bar的值:

1peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "foo"]}'
2Query Result: 100
3
4peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "bar"]}'
5Query Result: 50

调用chaincode从foo转移10到bar:

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

重新查询foo和bar的值:

1peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "foo"]}'
2Query Result: 90
3
4peer chaincode query -C mychannel -n mychaincode -v v0 -c '{"Args": ["query", "bar"]}'
5Query Result: 60

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

参考 #

© 2024 青蛙小白 | 总访问量 | 总访客数