00 导言
由 Linux 基金会发起创建的开源区块链分布式账本 —— Hyperledger Fabric,已经迎来了 1.2.0 版本。该项目着重于性能和可靠性,以及推动区块链和分布式账本技术的跨行业协作。Hyperledger Fabric 可用于全球供应链管理、金融交易、资产账和去中心化的社交网络等场景,但无意以此来构建一种加密货币。
下面我们将带着大家一步一步来搭建自己的HyperLedger Fabric开发环境。老司机已经发车,赶紧跟上。。。
01 操作系统准备
我们选择了ubuntu18.04这个版本。具体怎么安装请参考我们之前的文章 《在VirtualBox里安装用于区块链开发的Ubuntu操作系统》
02 安装curl
后面的步骤我们需要用到curl命令来下载安装脚本和其他文件,所以需要先安装curl,如果已经安装可略过。
第1步: 安装curl,如果已经安装可略过
$ sudo apt install curl
03 安装docker-ce
docker分两个怎么,分为docker-ce(社区版)和docker-ee(企业版)。作为穷人阶级代表的我,只能无奈选择使用社区版。在ubuntu操作系统上安装docker-ce,实际上docker官方提供了三种方式。
1. 通过docker仓库安装
2. 下载.deb包安装
3. 通过安装脚本安装
在这里我们选择第3种方式进行安装。
第1步: 使用curl下载安装脚本
$ curl -fsSL get.docker.com -o get-docker.sh
$ ls get*
get-docker.sh
第2步: 执行脚本进行安装,这一步需要等待一阵时间。
$ sudo sh get-docker.sh
第3步: 设置成非root用户也能执行docker,需要将普通用户加入docker组,我们当前的用户叫blockchain。
$ sudo usermod -aG docker blockchain
第4步: 执行hello-world镜像,验证docker是否安装成功。如果看到 Hello from Docker! This message shows that your installation appears to be working 这句话表明安装成功了。
$ sudo docker run hello-world
...... 一堆提示信息
Hello from Docker!
This message shows that your installation appears to be working correctly.
.....一堆提示信息
04 安装docker-compose
Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器。HyperLedger Fabric需要用到docker-compose,所以我们提前安装。
第1步: 下载某个版本的docker-compose,下载到/usr/local/bin/docker-compose目录。更多版本请访问:https://github.com/docker/compose/releases
$ sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
这里解释一下: uname -s 得到Linux,uname -m 得到x86_64。可在复制到命令行执行查看。
第2步:允许其他用户执行compose相关命令
$ sudo chmod +x /usr/local/bin/docker-compose
第3步:验证安装是否成功
$ docker-compose --version
docker-compose version 1.21.2, build a133471
05 安装go
第1步:下载golang的tar包
$ wget https://storage.googleapis.com/golang/go1.10.1.linux-amd64.tar.gz
由于众所周知的原因,google相应的站点被墙访问不了(兄弟,接受现实吧),我们得想其他办法。推荐从第三方镜像源下载golang的tar包,推荐镜像源 https://studygolang.com/dl,用浏览器打开该网址,下载linux的tar包。
第2步:解压tar包到/usr/local
$ cd 下载
$ sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz
第3步:创建go目录
$ mkdir $HOME/go
第4步:用vi打开~./bashrc,配置环境变量
$ vi ~/.bashrc
增加下面的环境变量,保存退出
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
export GOPATH=$HOME/go
使环境变量立即生效
$ source ~/.bashrc
第5步:检测go是否安装好
$ go version
go version go1.10.3 linux/amd64
06 安装并检测git
$ sudo apt install git
$ git version
06 下载fabric的引导脚本
先创建放置的目录,然后进入该目录,用curl下载脚本
$ cd ~
$ mkdir hyperledger-fabric
$ cd hyperledger-fabric
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-1.2/scripts/bootstrap.sh -o bootstrap.sh
$ ls
bootstrap.sh
07 执行引导脚本
这一步通过运行脚本去安装fabric,并且会下载fabric-sample,而且还会摘取docker镜像。下载过程需要耗费比较长的时间。
$ cd ~/hyperledger-fabric/
sudo bash ./bootstrap.sh 1.2.0 1.2.0 0.4.10
下载完,可以执行docker images查看已经下载的docker相关镜像
$ sudo docker images | grep hyperledger*
hyperledger/fabric-ca 1.2.0 66cc132bd09c 2 weeks ago 252MB
hyperledger/fabric-ca latest 66cc132bd09c 2 weeks ago 252MB
hyperledger/fabric-tools 1.2.0 379602873003 2 weeks ago 1.51GB
hyperledger/fabric-tools latest 379602873003 2 weeks ago 1.51GB
hyperledger/fabric-ccenv 1.2.0 6acf31e2d9a4 2 weeks ago 1.43GB
hyperledger/fabric-ccenv latest 6acf31e2d9a4 2 weeks ago 1.43GB
hyperledger/fabric-orderer 1.2.0 4baf7789a8ec 2 weeks ago 152MB
hyperledger/fabric-orderer latest 4baf7789a8ec 2 weeks ago 152MB
hyperledger/fabric-peer 1.2.0 82c262e65984 2 weeks ago 159MB
hyperledger/fabric-peer latest 82c262e65984 2 weeks ago 159MB
hyperledger/fabric-zookeeper 0.4.10 2b51158f3898 3 weeks ago 1.44GB
hyperledger/fabric-zookeeper latest 2b51158f3898 3 weeks ago 1.44GB
hyperledger/fabric-kafka 0.4.10 936aef6db0e6 3 weeks ago 1.45GB
hyperledger/fabric-kafka latest 936aef6db0e6 3 weeks ago 1.45GB
hyperledger/fabric-couchdb 0.4.10 3092eca241fc 3 weeks ago 1.61GB
hyperledger/fabric-couchdb latest 3092eca241fc 3 weeks ago 1.61GB
hyperledger/fabric-ca x86_64-1.0.4 8e691b3509bf 8 months ago 238MB
hyperledger/fabric-tools x86_64-1.0.4 6051774928a6 8 months ago 1.33GB
hyperledger/fabric-orderer x86_64-1.0.4 b17741e7b036 8 months ago 151MB
hyperledger/fabric-peer x86_64-1.0.4 1ce935adc397 8 months ago 154MB
hyperledger/fabric-javaenv latest a517b70135c7 8 months ago 1.41GB
hyperledger/fabric-javaenv x86_64-1.0.4 a517b70135c7 8 months ago 1.41GB
hyperledger/fabric-ccenv x86_64-1.0.4 856061b1fed7 8 months ago 1.28GB
08 添加环境变量
用vi打开~./bashrc
$ vi ~/.bashrc
增加下面的环境变量,保存退出
export PATH=/home/blockchain/hyperledger-fabric/fabric-samples/bin:$PATH
使环境变量立即生效
$ source ~/.bashrc
检验环境变量是否成功
$ fabric-ca-client version
fabric-ca-client:
Version: 1.2.0
Go version: go1.10
OS/Arch: linux/amd64
09 创建和管理网络
第1步:执行 ./byfn.sh generate 命令生成相应的文件
$ cd ~/hyperledger-fabric/fabric-samples/first-network/
$ ./byfn.sh generate
Generating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] Y
proceeding ...
.......
其中byfn.sh为启动这个网络的启动脚本,启动脚本中除建立一个包含4个节点和1个Order service的网络外,还会启动一个容器用来执行脚本在channel中加入节点,部署和初始化chaincode,以及在部署的chaincode上执行交易。默认channel名称为mychannel,脚本程序会给网络实例生成数字证书和密钥;生成genesis block用来启动ordering service;一些用来配置channel的配置交易
第2步:执行 ./byfn.sh up 启动网络
$ ./byfn.sh up
Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] Y
proceeding ...
LOCAL_VERSION=1.2.0
DOCKER_IMAGE_VERSION=1.2.0
Creating network "net_byfn" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating peer0.org2.example.com ... done
Creating orderer.example.com ... done
Creating peer0.org1.example.com ... done
Creating peer1.org1.example.com ... done
Creating peer1.org2.example.com ... done
Creating cli ... done
____ _____ _ ____ _____
/ ___| |_ _| / \ | _ \ |_ _|
\___ \ | | / _ \ | |_) | | |
___) | | | / ___ \ | _ < | |
|____/ |_| /_/ \_\ |_| \_\ |_|
Build your first network (BYFN) end-to-end test
Channel name : mychannel
Creating channel...
.......
当我们看到START的时候,表示启动成功。
执行结束,显示END
========= All GOOD, BYFN execution completed ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
通过docker ps命令可以查看到节点的启动情况。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c129154a34a dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab "chaincode -peer.add…" 5 minutes ago Up 5 minutes dev-peer1.org2.example.com-mycc-1.0
3746b3295e48 dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9 "chaincode -peer.add…" 6 minutes ago Up 6 minutes dev-peer0.org1.example.com-mycc-1.0
d2cdb7e80c20 dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b "chaincode -peer.add…" 6 minutes ago Up 6 minutes dev-peer0.org2.example.com-mycc-1.0
91034211fb91 hyperledger/fabric-tools:latest "/bin/bash" 7 minutes ago Up 7 minutes cli
b38fa60f4e76 hyperledger/fabric-peer:latest "peer node start" 7 minutes ago Up 7 minutes 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
69fd373480cf hyperledger/fabric-peer:latest "peer node start" 7 minutes ago Up 7 minutes 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com
c5f1469bf120 hyperledger/fabric-orderer:latest "orderer" 7 minutes ago Up 7 minutes 0.0.0.0:7050->7050/tcp orderer.example.com
1faf026ed9c9 hyperledger/fabric-peer:latest "peer node start" 7 minutes ago Up 7 minutes 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com
4e0cb9a0a0aa hyperledger/fabric-peer:latest "peer node start" 7 minutes ago Up 7 minutes 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com
第3步:如果我们要停止网络,可以执行./byfn.sh down 命令来停止
$ ./byfn.sh down
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] Y
proceeding ...
Stopping cli ... done
Stopping peer0.org1.example.com ... done
Stopping peer1.org2.example.com ... done
Stopping orderer.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Removing cli ... done
Removing peer0.org1.example.com ... done
Removing peer1.org2.example.com ... done
Removing orderer.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing network net_byfn
Removing volume net_orderer.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer1.org2.example.com
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
7c129154a34a
3746b3295e48
d2cdb7e80c20
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:af423f72155affe6f12819384004ffdbf3506274458e85ace79b85afa9f2edaf
Deleted: sha256:b3fbcd50f54c60d9454612a3ed2a01b0c4d060b27113fc1a9d1289647d43d0fe
Deleted: sha256:71212a99a5c510e8d4e06d998c9238f8aeab0d39b43585c0f592bcb783212115
Deleted: sha256:6a155ef7756a3361c3c549a4bb9bb8b9138ef53ebfada38d65dc00d874ebc5b6
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:c4c8845cab7101ee6a4456ee544c4a6599c42bbca545b11045d6b47c1223f205
Deleted: sha256:f2f179f4758793728780f909833f710c71f40e77303bd64eb910d572a641c66b
Deleted: sha256:03f9cd5e163dc2fae818d7864f8f068330eb7e0b0709a34d40dfcf0e40bd5d43
Deleted: sha256:8747af4513550fd8f0f6cfd22c61fd531b073a1a733d02ff6e3fb97fb6def027
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:2bffc21709916a8d9bd1c20a65134ac3ca31ef5cc6e1282cea3b6be28d295acb
Deleted: sha256:7cf57b3452456b8da30c08d92d0c054fe32b2c91cd047dc8075706af2c14dc0d
Deleted: sha256:6e5b8c88c9d03c7d776953ee8c1b638d02a60438130c9d174b99455bc43d07b8
Deleted: sha256:b9eee4e0944c3b35fec12aa4bb5624d02825ab12e71d8a6a73e3de9702831382
10 总结
通过官方提供的引导脚本,我们可以快速的搭建好HyperLedger Fabric 1.2.0 环境,前提是我们需要安装好docker-ce和docker-compose。另外官方给的引导脚本路径访问不了,因此我们可以直接去到HyperLedger Fabric的github上找到相应的脚本下载下来,然后再运行。
11 参考资料
1.https://docs.docker.com/install/linux/docker-ce/ubuntu/
2.https://docs.docker.com/compose/install/#install-compose
3.https://blog.csdn.net/qq_30383511/article/details/80354943
4.https://blog.csdn.net/sinat_36742186/article/details/80809954
5.https://www.jianshu.com/p/8beb3a355f99
有疑问加站长微信联系(非本文作者)