环境准备
修改yum源
备份repo源
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.back
下载aliyun的repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
清理缓存
yum clean all
重新生成缓存
yum makecache
更新
yum -y update
安装docker
安装需要的软件包,yum-utils提供yum-config-manager功能
yum install -y yum-utils device-mapper-persistent-data lvm2
设置 yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
选择适合的docker版本号 安装docker
yum install docker-ce-18.03.1.ce
启动docker服务
systemctl start docker
开机自启动
systemctl enable docker
配置加速镜像
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://qrclyekc.mirror.aliyuncs.com"]
}
EOF
#重新载入daemon
sudo systemctl daemon-reload
#重启docker
sudo systemctl restart docker
验证docker安装
docker version
安装docker-compose
下载docker-compose ,根据自己需求选择版本号
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
更改二进制文件的权限
chmod +x /usr/local/bin/docker-compose
验证安装状态
docker-compose version
安装go
yum install golang
配置环境变量
vi /etc/profile
export GO111MODULE=on
export GOROOT=/usr/local/go
export GOPATH=/root/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
切换到/root目录,并创建gopath文件夹,后面开发和编译过程中用go get安装第三方包会用到
cd /root
mkdir gopath
使换将变量配置生效,并查看golang的版本
source /etc/profile
go version
前置版本
[root@i-3ctrn15d scripts]# docker -v
Docker version 18.03.1-ce, build 9ee9f40
-----------------------------------------
[root@i-3ctrn15d scripts]# docker-compose -v
docker-compose version 1.25.0, build 0a186604
---------------------------------------------
[root@i-3ctrn15d scripts]# go version
go version go1.15.13 linux/amd64
Fabric环境搭建
首先创建文件夹,需要在gopath底下进行创建
mkdir /root/gopath/src/github.com/hyperledger
# 进入创建的文件夹
cd /root/gopath/src/github.com/hyperledger
从github上拉取源码
git clone "https://github.com/hyperledger/fabric.git"
cd fabric/
#本文使用的是1.4版本的Fabric,需要以下命令检出fabric版本为1.4的分支
git checkout release-1.4
#下载必备的文件,包括二进制文件,fabric-samples及必备的docker镜像
cd scripts/
#下载是比较慢的,如果出现错误或者长时间没有速度只需要重新运行就可以了
sudo ./bootstrap.sh
由于可能因为被墙的关系无法下载,则进行手动下载
首先对bootstrap.sh进行修改
vi bootstrap.sh
注释掉binariesInstall,不进行二进制文件的下载
if [ "$BINARIES" == "true" ]; then
echo
echo "Installing Hyperledger Fabric binaries"
echo
##这里为注释的地方
#binariesInstall
fi
if [ "$DOCKER" == "true" ]; then
echo
echo "Installing Hyperledger Fabric docker images"
echo
dockerInstall
fi
由于1.4.12版本太新,fabric镜像中的javaenv的会显示
Error response from daemon: No such image: hyperledger/fabric-javaenv:1.4.12
经过尝试,1.4.7的版本可以拉取镜像
再对bootstrap.sh进行修改
# if version not passed in, default to latest released version
export VERSION=1.4.7
# if ca version not passed in, default to latest released version
export CA_VERSION=1.4.7
# current version of thirdparty images (couchdb, kafka and zookeeper) released
export THIRDPARTY_IMAGE_VERSION=0.4.22
接着运行bootstrap.sh文件,进行镜像的拉取
./bootstrap.sh
拉取完后显示拉取的镜像
#这里只显示部分
===> List out hyperledger docker images
hyperledger/fabric-ca <none> 3a1799cda5d7 4 weeks ago 252MB
hyperledger/fabric-tools <none> 432c24764fbb 4 weeks ago 1.55GB
hyperledger/fabric-ccenv <none> d7433c4b2a1c 4 weeks ago 1.43GB
hyperledger/fabric-orderer <none> ec4ca236d3d4 4 weeks ago 173MB
hyperledger/fabric-peer <none> a1e3874f338b 4 weeks ago 178MB
hyperledger/fabric-zookeeper 0.4.15 20c6045930c8 7 weeks ago 1.43GB
hyperledger/fabric-zookeeper latest 20c6045930c8 7 weeks ago 1.43GB
hyperledger/fabric-kafka 0.4.15 b4ab82bbaf2f 7 weeks ago 1.44GB
hyperledger/fabric-kafka latest b4ab82bbaf2f 7 weeks ago 1.44GB
hyperledger/fabric-couchdb 0.4.15 8de128a55539 7 weeks ago 1.5GB
hyperledger/fabric-couchdb latest 8de128a55539 7 weeks ago 1.5GB
hyperledger/fabric-baseos amd64-0.4.15 9d6ec11c60ff 7 weeks ago 145MB
hyperledger/fabric-baseos latest 9d6ec11c60ff 7 weeks ago 145MB
hyperledger/fabric-javaenv 1.4.0 3d91b3bf7118 3 months ago 1.75GB
hyperledger/fabric-javaenv latest 3d91b3bf7118 3 months ago 1.75GB
hyperledger/fabric-tools 1.4.0 0a44f4261a55 4 months ago 1.56GB
hyperledger/fabric-tools latest 0a44f4261a55 4 months ago 1.56GB
hyperledger/fabric-ccenv 1.4.0 5b31d55f5f3a 4 months ago 1.43GB
hyperledger/fabric-ccenv latest 5b31d55f5f3a 4 months ago 1.43GB
hyperledger/fabric-orderer 1.4.0 54f372205580 4 months ago 150MB
hyperledger/fabric-orderer latest 54f372205580 4 months ago 150MB
hyperledger/fabric-peer
从github上下载hyperledger-fabric-linux-amd64-1.4.12.tar.gz,并进行解压
tar -zxvf hyperledger-fabric-linux-amd64-1.4.12.tar.gz
将解压生成的bin文件夹复制到scripts下
cp -rf bin /root/gopath/src/github.com/hyperledger/fabirc/scripts
返回至主目录下,对二进制文件进行编译
#返回至主目录下
cd ..
#对二进制文件进行编译
make release
#查看编译后的结果
cd release/linux-amd64/bin
#若生成了configtxgen configtxlator cryptogen discover idemixgen orderer peer则编译成功
若如下报错
go: warning: "github.com/hyperledger/fabric/core/chaincode/shim/..." matched no packages
原因是可能是在使用 GOPROXY 的时候,开启了 GO111MODULE,导致包管理非官方所说的在 $GOPATH\src\,而是去了 $GOPATH\src\pkg\目录下,此时就需要用go mod引入这些包 require github.com/gin-gonic/gin@latest ,解决import获取不了包的问题。见
https://www.cnblogs.com/mumuzifeng/p/14056154.html
所以这里将GO111MODULE
关掉
export GO111MODULE=off
将生成的文件添加进环境变量
vi /etc/profile
#文件中最后添加以下内容
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin
#更新配置
source /etc/profile
完成上面的操作,就可以启动第一个fabric网络了。
#进入first-network文件夹
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/
#执行命令
./byfn.sh up
若最后的结果为下图,则搭建完成
===================== Query successful on peer1.org2 on channel 'mychannel' =====================
========= All GOOD, BYFN execution completed ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
#最后执行以下命令关闭网络
./byfn.sh down