1.Hyperledger Fabric1.4在Ubuntu18.04中的基础环境搭建及first-network运行

博主参考Fabirc的官方文档和其他博客资料,完成了Fabric1.4版本的环境搭建,在这里记录下来,如有问题,欢迎交流指正。

参考链接:
https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html
https://blog.csdn.net/smallone233/article/details/86569536
https://www.cnblogs.com/llongst/p/9482038.html

部署环境
虚拟机:VMware-workstation-full-15.0.2-10952284.exe
Ubuntu镜像:ubuntu-18.04.2-desktop-amd64.iso
Fabric版本:1.4.0

导航

  • 1 准备工作
    • 1.1 更改系统的软件源
    • 1.2 安装git和curl
  • 2 go的安装和配置
    • 2.1 go安装包下载
    • 2.2 go的安装
    • 2.3 go环境变量配置
  • 3 docker和docker-compose的安装
    • 3.1 docker的安装和配置
    • 3.2 docker-compose的安装
  • 4 Fabric的安装
    • 4.1 创建并进入工作目录
    • 4.2 克隆源码
    • 4.3 下载镜像和fabric-samples
  • 5 运行first-network
    • 5.1 启动网络
    • 5.2 测试网络
    • 5.3 关闭网络

1 准备工作

1.1 更改系统的软件源

系统设置中选择Software & Updates,将系统的软件源改为aliyun。
1.Hyperledger Fabric1.4在Ubuntu18.04中的基础环境搭建及first-network运行_第1张图片

1.2 安装git和curl

在终端中输入:

sudo apt-get update
sudo apt-get install git
sudo apt-get install curl

2 go的安装和配置

直接使用apt安装的go语言版本较低,而fabric1.4要求go版本为1.11.x及以上,推荐手动安装当前最新版本。

2.1 go安装包下载

下载地址:https://studygolang.com/dl
选择Linux版本下载。或使用wget工具下载(go版本为1.12.1):

cd ~
wget https://studygolang.com/dl/golang/go1.12.1.linux-amd64.tar.gz

2.2 go的安装

打开终端,进入go安装包所在目录:

tar -xzf go1.12.1.linux-amd64.tar.gz
sudo mv go /usr/local

2.3 go环境变量配置

使用管理员权限打开/etc/profile文件:

sudo gedit /etc/profile

在文件尾部加入go相关的环境变量:

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

保存退出。
Note:环境变量中的GOPATH可自定义目录位置,上述GOPATH为当前用户home目录下的go文件夹
执行

source /etc/profile

命令或者重启,让当前的go环境变量生效。终端中执行

go version

如出现如下内容,则说明go安装配置成功。

go version go1.12.1 linux/amd64

3 docker和docker-compose的安装

3.1 docker的安装和配置

若已经安装过旧版本的docker,先执行下列命令卸载,没有则可跳过:

sudo apt-get remove docker docker-engine docker.io

准备安装

sudo apt-get update

下载相关工具

sudo apt-get install \
	apt-transport-https \
	ca-certificates \
	software-properties-common

添加docker官方GPG密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

设立仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

安装最新版本docker-ce

sudo apt-get update
sudo apt-get install docker-ce

添加阿里云的Docker Hub镜像

sudo mkdir -p /etc/docker
sudo gedit /etc/docker/daemon.json

将下列内容写入daemon.json文件中,并保存退出

{
 "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}

接下来执行:

sudo systemctl daemon-reload
sudo systemctl restart docker

添加当前用户权限

sudo usermod -aG docker XXX	(XXX为当前用户名)
sudo chmod -R 777 /var/run/docker.sock

测试
输入:

docker version

当出现下列内容时表明docker安装配置完毕

Client:
 Version:           18.09.4
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        d14af54266
 Built:             Wed Mar 27 18:35:44 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.4
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       d14af54
  Built:            Wed Mar 27 18:01:48 2019
  OS/Arch:          linux/amd64
  Experimental:     false

3.2 docker-compose的安装

安装

sudo apt-get install python-pip
sudo pip install docker-compose

测试
输入:

docker-compose version

当出现下列内容时表明docker-compose安装完毕

docker-compose version 1.24.0, build 0aa5906
docker-py version: 3.7.2
CPython version: 2.7.15rc1
OpenSSL version: OpenSSL 1.1.0g  2 Nov 2017

4 Fabric的安装

4.1 创建并进入工作目录

mkdir -p $GOPATH/src/github.com/hyperledger/
cd $GOPATH/src/github.com/hyperledger/

4.2 克隆源码

git clone https://github.com/hyperledger/fabric.git
cd fabric
git checkout v1.4.0

4.3 下载镜像和fabric-samples

cd scripts/

执行scripts目录中的bootstrap.sh脚本会自动下载fabric-samples和fabric镜像

./bootstrap.sh

由于镜像较大,此过程需要较长时间完成。脚本完成后会发现当前目录有了fabric-samples目录,之后执行:

docker images

当出现下列内容时说明镜像部署成功:

REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
hyperledger/fabric-javaenv     1.4.0               3d91b3bf7118        2 months ago        1.75GB
hyperledger/fabric-javaenv     latest              3d91b3bf7118        2 months ago        1.75GB
hyperledger/fabric-tools       1.4.0               0a44f4261a55        3 months ago        1.56GB
hyperledger/fabric-tools       latest              0a44f4261a55        3 months ago        1.56GB
hyperledger/fabric-ccenv       1.4.0               5b31d55f5f3a        3 months ago        1.43GB
hyperledger/fabric-ccenv       latest              5b31d55f5f3a        3 months ago        1.43GB
hyperledger/fabric-orderer     1.4.0               54f372205580        3 months ago        150MB
hyperledger/fabric-orderer     latest              54f372205580        3 months ago        150MB
hyperledger/fabric-peer        1.4.0               304fac59b501        3 months ago        157MB
hyperledger/fabric-peer        latest              304fac59b501        3 months ago        157MB
hyperledger/fabric-ca          1.4.0               1a804ab74f58        3 months ago        244MB
hyperledger/fabric-ca          latest              1a804ab74f58        3 months ago        244MB
hyperledger/fabric-zookeeper   0.4.14              d36da0db87a4        5 months ago        1.43GB
hyperledger/fabric-zookeeper   latest              d36da0db87a4        5 months ago        1.43GB
hyperledger/fabric-kafka       0.4.14              a3b095201c66        5 months ago        1.44GB
hyperledger/fabric-kafka       latest              a3b095201c66        5 months ago        1.44GB
hyperledger/fabric-couchdb     0.4.14              f14f97292b4c        5 months ago        1.5GB
hyperledger/fabric-couchdb     latest              f14f97292b4c        5 months ago        1.5GB

此时Fabric1.4的基础环境已搭建完成。

5 运行first-network

5.1 启动网络

cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
./byfn.sh up

当出现下列内容时(all good)说明网络启动成功:

...
...
===================== Chaincode is installed on peer1.org2 ===================== 

Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mychannel'... ===================== 
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Attempting to Query peer1.org2 ...3 secs
+ res=0
+ set +x

90
===================== Query successful on peer1.org2 on channel 'mychannel' ===================== 

========= All GOOD, BYFN execution completed =========== 


 _____   _   _   ____   
| ____| | \ | | |  _ \  
|  _|   |  \| | | | | | 
| |___  | |\  | | |_| | 
|_____| |_| \_| |____/  

5.2 测试网络

进入容器

docker exec -it cli bash

此时初始状态为a:90,b:210
查询“a”

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

显示

90

交易 a->b 10

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 --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt\
  --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt\
  -c '{"Args":["invoke","a","b","10"]}'

出现

2019-04-10 12:09:34.421 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

则交易成功,此时对“b”进行查询:

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

显示为

220

说明交易成功。

5.3 关闭网络

执行

exit

退出容器。之后执行

./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 peer1.org2.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping orderer.example.com    ... done
Removing cli                    ... done
Removing peer1.org2.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing orderer.example.com    ... done
Removing network net_byfn
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.
Removing volume net_orderer.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer1.org2.example.com
35da712e7673
3d61dc5039cb
19e5304917c8
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:222c40312013e72ae07ae4fe4ad5f43f82b013857acb4fa4d41b7081c67a51d1
Deleted: sha256:4f90e84a94c4a8f26b732b1e2b458327775dc18f983910c949e23b991a8ea4a5
Deleted: sha256:710e30b672a4f8756b587b8740d18520fdb25a1225d549a469a6260afe069de1
Deleted: sha256:b50da2a0960faf990dc25be01e293a12398ee35a3a4f0575244e7f0ebed0e801
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:31023251ead8d481fc0cca4ee8805cc250fda5e342a4e6cf5377a13822f899d2
Deleted: sha256:4c62e3878bfdd894d9101e7a57685396f7c1dff39b67c827fa31a7b9ba63b12b
Deleted: sha256:f9fb6cae8592416af6f0f072d08dad55239223b15dac74be8996e0270a899b4f
Deleted: sha256:2f6cb63430f63e8f5d2aa9c745ab487e1ecd7a9d533447f461657fd7850452f7
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:3512341a029e321e4d45709741843b953f26c3f201ab30f2aac9fe4720ff0e83
Deleted: sha256:7e98a73a9c64e84d4ae5fb73f9688f3fd1b336c73ddd85a4d4c25c3d0fb00fea
Deleted: sha256:3a0035dc81b7f595e451c7357ddcb80f2703b39ac6054f5dd8f5d956dda6aa8c
Deleted: sha256:6527273f956d08dc38d2098b0d279ceb190ff42f32e1009ef3b167b2b9a01b9f

至此,Fabric1.4中的first-network例子运行测试完毕。

你可能感兴趣的:(区块链,fabric,区块链,Hyperledger,Fabric,Ubuntu)