Ubuntu 安装 Heperledger Fabric(亲测有效)

引言: 身处天朝,奈何资源都在国外,本文通过不可张扬的手段解决了二进制包无法下载、github克隆巨慢等问题,楼主亲测有效,如按本文搭建报错请在评论区留下脚印,供大家一起讨论学习,谢谢~

相关环境

ubuntu 18.04
Fabric v2.0.0

ubuntu镜像源设置

如您已完成 “镜像源” 等相关基础设置,请点击右侧“目录”>"依赖环境安装"跳转至Fabric环境准备

  • 阿里镜像源设置

    • 打开“软件更新器“:
      Ubuntu 安装 Heperledger Fabric(亲测有效)_第1张图片

    • 点击”停止“取消首次更新:
      Ubuntu 安装 Heperledger Fabric(亲测有效)_第2张图片

    • 点击“设置”打开软件更新器设置界面:
      Ubuntu 安装 Heperledger Fabric(亲测有效)_第3张图片

    • 点击“Ubuntu软件”页签,在“下载至”中选择阿里云镜像:
      Ubuntu 安装 Heperledger Fabric(亲测有效)_第4张图片
      Ubuntu 安装 Heperledger Fabric(亲测有效)_第5张图片

Ubuntu 安装 Heperledger Fabric(亲测有效)_第6张图片

  • 点击“关闭”按钮:

Ubuntu 安装 Heperledger Fabric(亲测有效)_第7张图片

  • 在弹出的界面中,点击“重新载入”按钮,耐心等待更新完毕:
    Ubuntu 安装 Heperledger Fabric(亲测有效)_第8张图片
    Ubuntu 安装 Heperledger Fabric(亲测有效)_第9张图片

  • ubuntu不支持1080分辨率问题解决:

参看: https://www.cnblogs.com/archer-wen/p/10049791.html

依赖环境安装

如您已完成 “go”、“git”、“docker”、“docker-compose” 等相关依赖安装,请点击 右侧“目录”>"Fabric安装"跳转至Fabric安装

选装依赖

  • vim编辑器安装
sudo apt-get install vim -y

必装依赖

  • Git安装
sudo apt-get install git -y
  • Go环境 安装
# 下载安装包
sudo wget https://studygolang.com/dl/golang/go1.15.linux-amd64.tar.gz

# 解压缩
sudo tar -xvzf go1.15.linux-amd64.tar.gz -C /usr/local

# 配置环境变量(注意: 没安装vim请使用其他编辑命令或参看选装依赖中的vim编辑器安装)
sudo vim /etc/profile
# 在文件末尾写入如下信息
export GOPATH=/usr/local/go  
export PATH=$GOPATH/bin:$PATH
# 设置go proxy(防止后续智能合约部署失败)
export GO111MODULE=on
export GOPROXY=https://goproxy.cn

# sudo找不到命令处理方案(借鉴自博客: https://blog.csdn.net/weixin_30347009/article/details/94774508):
	# 1. 编辑文件 /etc/sudoers,将Defaults env_reset ,改为 Defaults !env_reset
	sudo vim /etc/sudoers
	Defaults  !env_reset

	# 2. 编辑文件~/.bashrc
	sudo vim ~/.bashrc
	# 在文件末尾添加新行
	alias sudo='sudo env PATH=$PATH'

# 使环境变量生效(可能会在后续操作中出现问题,此处我选择重启机器)
source /etc/profile
# 如果您配置了~/.bashrc文件且未重启机器,请执行如下命令:
source ~/.bashrc

# 验证配置是否成功
go version
# 输出内容:
    # warning: GOPATH set to GOROOT (/usr/local/go) has no effect
    # go version go1.15 linux/amd64
    
  • Docker安装
# docker 安装
sudo apt-get install docker.io -y

# 验证docker是否安装成功
sudo docker -v
# 安装成功提示: Docker version 19.03.6, build 369ce74a3c

# 设置docker镜像加速器(此处我使用了阿里云的镜像加速器,可参看此博客找到自己的镜像加速器配置信息: https://www.cnblogs.com/LUA123/p/11401962.html):
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://hpy5o1c8.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker


  • Docker-Compose安装
# docker-compose下载
sudo wget -O /usr/local/bin/docker-compose https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m`

# 修改文件权限
sudo chmod +x /usr/local/bin/docker-compose

# 验证docker-compose是否安装成功
docker-compose -v
# 安装成功提示: docker-compose version 1.25.5, build 8a1c60f6

Fabric安装

  • 下载fabric源码
# 建立目录
sudo mkdir -p /opt/gopath/src/github.com/hyperledger
cd /opt/gopath/src/github.com/hyperledger

# 下载fabric源码(由于github下载巨慢,此处我通过gitee导入了github上的fabric库)
sudo git clone https://gitee.com/yixiu00173/fabric.git

# 切换fabric至 release 2.0分支
cd fabric
sudo git checkout release-2.0
  • 修改bootstrap.sh
# 修改./scripts/bootstrap.sh
sudo vim ./scripts/bootstrap.sh
# 找到65行如下代码(此处修改了拉取fabric-samples的仓库地址):
git clone -b master https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
# 注释该行代码并在其下方写入:
git clone -b master https://gitee.com/yixiu00173/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
# 找到197行如下代码并注释(这里下载二进制文件,后面我们再手动下载二进制包):
pullBinaries
  • 下载fabric-samples 及 docker镜像(耐心等待下载完成~,当前所在目录: )
cd scripts
sudo ./bootstrap.sh

# 执行完成后,如正确无误,则执行如下命令可看到相应的输出内容:
ls -l fabric-samples/
# 总用量 76
# drwxr-xr-x 8 root root  4096 8月  19 17:20 chaincode
# drwxr-xr-x 3 root root  4096 8月  19 17:20 chaincode-docker-devmode
# drwxr-xr-x 4 root root  4096 8月  19 17:20 ci
# -rw-r--r-- 1 root root   597 8月  19 17:20 CODE_OF_CONDUCT.md
# -rw-r--r-- 1 root root   109 8月  19 17:20 CODEOWNERS
# drwxr-xr-x 3 root root  4096 8月  19 17:20 commercial-paper
# -rw-r--r-- 1 root root   935 8月  19 17:20 CONTRIBUTING.md
# drwxr-xr-x 5 root root  4096 8月  19 17:20 fabcar
# drwxr-xr-x 6 root root  4096 8月  19 17:20 first-network
# drwxr-xr-x 4 root root  4096 8月  19 17:20 high-throughput
# drwxr-xr-x 4 root root  4096 8月  19 17:20 interest_rate_swaps
# -rw-r--r-- 1 root root 11358 8月  19 17:20 LICENSE
# -rw-r--r-- 1 root root  1644 8月  19 17:20 MAINTAINERS.md
# drwxr-xr-x 2 root root  4096 8月  19 17:20 off_chain_data
# -rw-r--r-- 1 root root  1694 8月  19 17:20 README.md
# -rw-r--r-- 1 root root  1035 8月  19 17:20 SECURITY.md
# drwxr-xr-x 8 root root  4096 8月  19 17:20 test-network

sudo docker images
# REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
# hyperledger/fabric-javaenv     2.0                 bb3837bf990f        5 months ago        505MB
# hyperledger/fabric-javaenv     2.0.1               bb3837bf990f        5 months ago        505MB
# hyperledger/fabric-javaenv     latest              bb3837bf990f        5 months ago        505MB
# hyperledger/fabric-tools       2.0                 5c9a03790913        5 months ago        512MB
# hyperledger/fabric-tools       2.0.1               5c9a03790913        5 months ago        512MB
# hyperledger/fabric-tools       latest              5c9a03790913        5 months ago        512MB
# hyperledger/fabric-peer        2.0                 5c7e5946f3dc        5 months ago        57.2MB
# hyperledger/fabric-peer        2.0.1               5c7e5946f3dc        5 months ago        57.2MB
# hyperledger/fabric-peer        latest              5c7e5946f3dc        5 months ago        57.2MB
# hyperledger/fabric-orderer     2.0                 92bd220edcdd        5 months ago        39.7MB
# hyperledger/fabric-orderer     2.0.1               92bd220edcdd        5 months ago        39.7MB
# hyperledger/fabric-orderer     latest              92bd220edcdd        5 months ago        39.7MB
# hyperledger/fabric-ccenv       2.0                 800087268d9b        5 months ago        529MB
# hyperledger/fabric-ccenv       2.0.1               800087268d9b        5 months ago        529MB
# hyperledger/fabric-ccenv       latest              800087268d9b        5 months ago        529MB
# hyperledger/fabric-baseos      2.0                 74ff718f6f67        5 months ago        6.9MB
# hyperledger/fabric-baseos      2.0.1               74ff718f6f67        5 months ago        6.9MB
# hyperledger/fabric-baseos      latest              74ff718f6f67        5 months ago        6.9MB
# hyperledger/fabric-ca          1.4                 3b96a893c1e4        5 months ago        150MB
# hyperledger/fabric-ca          1.4.6               3b96a893c1e4        5 months ago        150MB
# hyperledger/fabric-ca          latest              3b96a893c1e4        5 months ago        150MB
# hyperledger/fabric-zookeeper   0.4                 ede9389347db        9 months ago        276MB
# hyperledger/fabric-zookeeper   0.4.18              ede9389347db        9 months ago        276MB
# hyperledger/fabric-zookeeper   latest              ede9389347db        9 months ago        276MB
# hyperledger/fabric-kafka       0.4                 caaae0474ef2        9 months ago        270MB
# hyperledger/fabric-kafka       0.4.18              caaae0474ef2        9 months ago        270MB
# hyperledger/fabric-kafka       latest              caaae0474ef2        9 months ago        270MB
# hyperledger/fabric-couchdb     0.4                 d369d4eaa0fd        9 months ago        261MB
# hyperledger/fabric-couchdb     0.4.18              d369d4eaa0fd        9 months ago        261MB
# hyperledger/fabric-couchdb     latest              d369d4eaa0fd        9 months ago        261MB
  • 手动下载二进制包(当前所在目录:/opt/gopath/src/github.com/hyperledger )
# 二进制包下载地址的相关信息可以从./scripts/bootstrap.sh中找到
	# ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
    # VERSION=2.0.1
	# CA_VERSION=1.4.6
	# BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
	# CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz
	# "https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
	# "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
# 根据如上信息推断出下载地址为:
	# https://github.com/hyperledger/fabric/releases/download/v2.0.1/hyperledger-fabric-linux-amd64-2.0.1.tar.gz
	# https://github.com/hyperledger/fabric-ca/releases/download/v1.4.6/hyperledger-fabric-ca-linux-amd64-1.4.6.tar.gz
# 从如下地址下载二进制文件压缩包:
https://download.csdn.net/download/weixin_40119256/12728794
# 将压缩包解压后得到hyperledger-fabric-ca-linux-amd64-1.4.6.tar.gz 和 hyperledger-fabric-linux-amd64-2.0.1.tar.gz
# 将如上两个压缩包存放到 /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples 目录下
# 解压如上两个压缩包
sudo tar -xvzf hyperledger-fabric-linux-amd64-2.0.1.tar.gz
sudo tar -xvzf hyperledger-fabric-ca-linux-amd64-1.4.6.tar.gz
# 验证文件是否有缺失:
ls -l ./bin
# 输出如下内容表示正常:
# 总用量 214332
# -rwxr-xr-x 1  1001  1001 20999000 2月  27 06:05 configtxgen
# -rwxr-xr-x 1  1001  1001 17448272 2月  27 06:05 configtxlator
# -rwxr-xr-x 1  1001  1001 13344644 2月  27 06:05 cryptogen
# -rwxr-xr-x 1  1001  1001 19116716 2月  27 06:05 discover
# -rwxrwxr-x 1 yixiu yixiu 22964320 3月  29 10:12 fabric-ca-client
# -rwxrwxr-x 1 yixiu yixiu 29967984 3月  29 10:13 fabric-ca-server
# -rwxr-xr-x 1  1001  1001 12352844 2月  27 06:05 idemixgen
# -rwxr-xr-x 1  1001  1001 32764776 2月  27 06:05 orderer
# -rwxr-xr-x 1  1001  1001 50501032 2月  27 06:05 peer

Fabric测试网络启动&交互

搬运自: https://hyperledger-fabric.readthedocs.io/en/release-2.0/test_network.html

  • 当前所在目录

/opt/gopath/src/github.com/hyperledger/fabric/scripts

  • 进入test-network目录
cd fabric-samples/test-network/
  • 启动测试网络
sudo ./network.sh up
# 启动成功后会产生如下输出信息: 
CONTAINER ID        IMAGE                               COMMAND             CREATED             STATUS                  PORTS                              NAMES
7b2f0b55f271        hyperledger/fabric-peer:latest      "peer node start"   3 seconds ago       Up 1 second             0.0.0.0:7051->7051/tcp             peer0.org1.example.com
ad2015f9a114        hyperledger/fabric-peer:latest      "peer node start"   3 seconds ago       Up 1 second             7051/tcp, 0.0.0.0:9051->9051/tcp   peer0.org2.example.com
bc26d10d2f57        hyperledger/fabric-orderer:latest   "orderer"           3 seconds ago       Up Less than a second   0.0.0.0:7050->7050/tcp             orderer.example.com
  • 创建通道
sudo ./network.sh createChannel
# 创建成功将会产生如下输出信息:
========= Channel successfully joined =========== 
  • 启动智能合约
sudo ./network.sh deployCC
# 启动成功将会产生如下输出信息:
Querying chaincode on peer0.org1...
Using organization 1
===================== Querying on peer0.org1 on channel 'mychannel'... ===================== 
Attempting to Query peer0.org1 ...1597830487 secs
++ peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
++ res=0
++ set +x

[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
===================== Query successful on peer0.org1 on channel 'mychannel' ===================== 
  • 与测试网络交互
# 配置二进制环境变量
export PATH=${PWD}/../bin:${PWD}:$PATH
# 配置fabric 相关配置文件
export FABRIC_CFG_PATH=$PWD/../config/

# 配置环境变量,以peer Org1进行相关操作
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

# 查询通道分类账中的汽车列表
sudo peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
# 执行成功将产生如下输出信息:
[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]

# 更改汽车所有者
sudo peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"changeCarOwner","Args":["CAR9","Dave"]}'
# 执行成功将产生如下输出:
2020-08-19 17:54:15.338 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 

# 配置环境变量,以peer Org2进行相关操作
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

# 调用合约查看CAR9的相关信息
sudo peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryCar","CAR9"]}'
# 执行成功将产生如下输出:
{"make":"Holden","model":"Barina","colour":"brown","owner":"Dave"}

# 停止测试网络
sudo ./network.sh down

你可能感兴趣的:(区块链)