容器中运行Fabric区块链网络

容器中运行Fabric区块链网络

Fabric是区块链项目Hyperleger的主要工程,可以在容器中运行,快速建立实验区块链网络。

1、安装虚拟机

  • 安装ubuntu虚拟客户机,环境为ubuntu16.04.4 LTS。
  • 安装go环境,环境变量配置好后重启 参考:http://www.cnblogs.com/auh2010006/p/6343231.html
  • 更新ubuntu的源,
    • sudo apt update && sudo apt upgrade -y
  • 安装curl
    • sudo apt install curl

2、安装Docker

  • 安装docker curl -sSL https://get.docker.com/ | sh
  • 将用户加入docker组并重启,运行 usermod -aG docker <username>。
  • docker run hello-world,启动hello-world测试docker是否安装成功。
  • 安装docker-compose,要求最好1.8以上,运行 sudo apt install docker-compose
  • 安装nodejs,运行 apt install nodejs
  • 安装npm,运行 apt install npm
  • 为了启动docker后,就可以远程访问,需要修改docker服务配置,文件位于/etc/default/docker(Ubuntu 16.04.04位于/lib/systemd/system/docker.service中):
    • # Use DOCKER_OPTS to modify the daemon startup options.
    • # DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
    • 然后重启docker服务,
      • sudo systemctl daemon-reload
      • sudo systemctl restart docker。

3、安装Fabric

  • 下载fabric的一些工具和脚本并且自动下载镜像和修改tag。
 curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap-1.0.0-rc1.sh | bash这个链接不需要翻墙
  • 配置fabric环境变量并重启。
    • #可以不配置环境变量但是bin目录要放在第15步的第一层文件夹内,因为byfn.sh脚本有设置变量。
  • git clone https://github.com/hyperledger/fabric-samples.git下载一个fabric例子和脚本用于搭建fabric网络
  • 执行fabric中bin文件的get-docker-images.sh获取镜像,会很慢,然后设置别名 命令:docker tag 镜像名:tag 新的镜像名:新的tag即latest
  • 进入byfn.sh脚本目录,执行./byfn.sh -m generate( This first step generates all of the certificates and keys for all our various network entities, the genesis block used to bootstrap the ordering service, and a collection of configuration transactions required to configure a Channel. ),
  • 然后,执行./byfn.sh -m up up命令其实连带执行了generate命令了,会先判断是否已经生成证书,如果没有就会先执行generate
注:./byfn.sh -m down 这个命令是将network摧毁,会删除up过程生成的container、image和证书文件等产生物
  • 网络搭建成功,用docker ps测试是否有容器运行(docker ps -a可以看到不在运行中的容器)

4、运行测试

开始运行测试。

  • 安装部署chaincode:
peer chaincode install -n mycc -v 1.0 -p \
github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 
  • 初始化chaincode:
peer chaincode instantiate -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 -v 1.0 \
-c '{"Args":["init","a", "100", "b","200"]}' \
-P "OR ('Org1MSP.member','Org2MSP.member')"
  • 进入cli容器:
docker exec -it cli bash 
  • 产生一笔交易:
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 <channer_name> \
-n mycc \
-c '{"Args":["invoke","b","a","50"]}' 
  • 查询:
peer chaincode query -C <channer_name> -n mycc -c '{"Args":["query","b"]}' 
  • 打印cli日志:
docker logs -f cli 
  • 打印某个容器的日志:
docker logs dev-peer0.org2.example.com-mycc-1.0

猜你喜欢

转载自my.oschina.net/u/2306127/blog/1647179
今日推荐