Hyperledger: Quickly build a Hyperledger Fabric 1.0 environment (valid for personal testing)

Hyperledger: Quickly build a Hyperledger Fabric 1.0 environment

Let's start our environment construction work:

1. Use VirtualBox and install Ubuntu in it

This step is actually nothing to say, download the latest version of VirtualBox, download Ubuntu Server, I use Ubuntu16.04.2 X64 Server. After installing Ubuntu, you need to ensure that the apt source is domestic, otherwise it will be very slow if it is abroad. The specific method is

sudo  vi /etc/apt/sources.list

Open this apt source list, if you see something like http://us.xxxxxx, it is foreign, if you see something like http://cn.xxxxx , then you don't need to change it. Mine is an American source, so I need to do a batch replacement. In command mode, enter:

:%s/us./cn./g

You can change all the us. to cn.. Then enter: wq to save and exit.

sudo apt-get update

Update the source.

Then install ssh so that you can then connect to Ubuntu remotely using a client like putty or SecureCRT.

sudo apt-get install ssh

2. Go installation

Although Ubuntu's apt-get provides Go installation, the version is relatively old. The best way is to refer to the official website and download the latest version of Go. The specific commands involved include:

wget https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.9.linux-amd64.tar.gz
[Note: Do not use apt to install go, the go version of apt is too low! 

Next edit the environment variables for the current user:

vi ~ / .profile

Add the following:

export PATH=$PATH:/usr/local/go/bin 
export GOROOT=/usr/local/go 
export GOPATH=$HOME/go 
export PATH=$PATH:$HOME/go/bin

Edit After saving and exiting vi, remember to load these environments:

source ~/.profile

We set the go directory GOPATH to the current user's folder, so remember to create the go folder

cd ~
mkdir go

3. Docker installation

We can use the mirror provided by Ali, and the installation is also very convenient. Install Docker with the following command

curl -sSL http: // acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh - if installation fails:

easy way

$ sudo apt-get update
$ sudo apt-get install docker

搞定!
但是这种方法有一个缺点:安装的不一定是最新的docker,安装版本是1.10.2,不过,对于docker使用没任何什么问题,如果要后续升级方便,可以使用下面的方法。


另一种方法

1.切换到root权限或者用sudo

2.升级source列表并保证https和ca证书成功安装

# apt-get update
# apt-get install apt-transport-https ca-certificates

3.增加新的GPG 密钥

# apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

4.新增或编辑source列表里的docker.list文件

# vi /etc/apt/sources.list.d/docker.list  //如果不存在就新增

5.删除已有的entries

6.按照系统版本增加entry(Ubuntu Xenial 16.04 (LTS))

deb https://apt.dockerproject.org/repo ubuntu-xenial main

7.重新执行更新操作,并删除老的repo

# apt-get purge lxc-docker  //没有安装的话,跳过

8.查看是否有正确的可用版本

# apt-cache policy docker-engine

如图:

显示查找的版本

9.从14.04版本以上开始docker推荐安装linux-image-extra

# apt-get install linux-image-extra-$(uname -r)

10.安装docker

# apt-get update
# apt-get install docker-engine
# service docker start
# docker run hello-world

安装完成后需要修改当前用户(我使用的用户叫fabric)权限:

sudo usermod -aG docker fabric

注销并重新登录,然后添加阿里云的Docker Hub镜像:

复制代码
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
复制代码

不同的版本添加方法是不一样的,官方的文档如下:

https://cr.console.aliyun.com/#/accelerator

当然觉得阿里云镜像不好用,喜欢用DaoClound的也可以用DaoClound的镜像。DaoCloud的镜像设置文档为:https://www.daocloud.io/mirror#accelerator-doc

4. Docker-Compose的安装

Docker-compose是支持通过模板脚本批量创建Docker容器的一个组件。在安装Docker-Compose之前,需要安装Python-pip,运行脚本:

sudo apt-get install python-pip

然后是安装docker-compose,我们从官方网站(https://github.com/docker/compose/releases)下载也可以从国内的进行DaoClound下载,为了速度快接下来从DaoClound安装Docker-compose,运行脚本:

curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose 
chmod +x /usr/local/bin/docker-compose

5. Fabric源码下载

我们可以使用Git命令下载源码,首先需要建立对应的目录,然后进入该目录,Git下载源码:
mkdir -p ~/go/src/github.com/hyperledger 
cd ~/go/src/github.com/hyperledger 
git clone https://github.com/hyperledger/fabric.git

由于Fabric一直在更新,所有我们并不需要最新最新的源码,需要切换到v1.0.0版本的源码即可:

cd ~/go/src/github.com/hyperledger/fabric
git checkout v1.0.0

6. Fabric Docker镜像的下载

这个其实很简单,因为我们已经设置了Docker Hub镜像地址,所以下载也会很快。官方文件也提供了批量下载的脚本。我们直接运行:

cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

这样就可以下载所有需要的Fabric Docker镜像了。由于我们设置了国内的镜像,所以下载应该是比较快的。

下载完毕后,我们运行以下命令检查下载的镜像列表:

docker images

得到的结果如下:

image

7.启动Fabric网络并完成ChainCode的测试

我们仍然停留在e2e_cli文件夹,这里提供了启动、关闭Fabric网络的自动化脚本。我们要启动Fabric网络,并自动运行Example02 ChainCode的测试,执行一个命令:

./network_setup.sh up

这个做了以下操作:

7.1编译生成Fabric公私钥、证书的程序,程序在目录:fabric/release/linux-amd64/bin

7.2基于configtx.yaml生成创世区块和通道相关信息,并保存在channel-artifacts文件夹。

7.3基于crypto-config.yaml生成公私钥和证书信息,并保存在crypto-config文件夹中。

7.4基于docker-compose-cli.yaml启动1Orderer+4Peer+1CLI的Fabric容器。

7.5在CLI启动的时候,会运行scripts/script.sh文件,这个脚本文件包含了创建Channel,加入Channel,安装Example02,运行Example02等功能。

最后运行完毕,我们可以看到这样的界面:

image

如果您看到这个界面,这说明我们整个Fabric网络已经通了。

8.手动测试一下Fabric网络

我们仍然是以现在安装好的Example02为例,在官方例子中,channel名字是mychannel,链码的名字是mycc。我们首先进入CLI,我们重新打开一个命令行窗口,输入:

docker exec -it cli bash

运行以下命令可以查询a账户的余额:

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

可以看到余额是90:

image

然后,我们试一试把a账户的余额再转20元给b账户,运行命令:

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 mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}'

运行结果为:

image

现在转账完毕, 我们试一试再查询一下a账户的余额,没问题的话,应该是只剩下70了。我们看看实际情况:

image

果然,一切正常。最后我们要关闭Fabric网络,首先需要运行exit命令退出cli容器。关闭Fabric的命令与启动类似,命令为:

cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli

./network_setup.sh down

现在我们整个Fabric的环境已经测试完毕,恭喜,一切正常,接下来我们就是去做自己的区块链的开发。希望我的文章对大家有所帮助。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392705&siteId=291194637