超详细的Hyperledger Fabric2.2 环境搭建部署
一、系统版本:
//系统版本
[root@ecs-344386 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
//内核版本
[root@ecs-344386 ~]# cat /proc/version
Linux version 3.10.0-1160.53.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Fri Jan 14 13:59:45 UTC 2022
//操作系统位数
[root@ecs-344386 ~]# uname -r
3.10.0-1160.53.1.el7.x86_64
二、安装docker
https://www.cnblogs.com/Horsonce/p/16798391.html
三、安装docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.27.3/docker-compose-'uname -s'-'uname -m' -o /usr/local/bin/docker-compose
//如果慢的话使用以下网址
curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
-rw-r--r-- 1 root root 12219168 10月 7 20:59 docker-compose
//下载完成后,将文件更改为可执行权限:
sudo chmod +x /usr/local/bin/docker-compose
chmod +x就是赋予用户文件的执行权限
//下载包 拉到/usr/local/bin下
docker-compose -v
[root@ecs-344386 bin]# docker-compose -v
docker-compose version 1.27.3, build 4092ae5d
四、安装go环境
下载安装包go1.15.14.linux-amd64.tar.gz,拉去linux系统/usr/local
cd /usr/local
//解压,生成go文件
wget https://golang.google.cn/dl/go1.15.14.linux-amd64.tar.gz
100%[====================================>] 121,105,361 1.30MB/s 用时 85s
2022-10-07 21:51:07 (1.36 MB/s) - 已保存 “go1.15.14.linux-amd64.tar.gz” [121105361/121105361])
tar -zxvf go1.15.14.linux-amd64.tar.gz
//rm -rf go1.15.14.linux-amd64.tar.gz
//创建文件夹子/usr/local/gopath中,用来作为GOPATH
//设置GOPATH、GOROOT、PATH,该目录下创建三个文件夹
mkdir gopath
cd gopath
mkdir src bin pkg
vim /etc/profile //打开文件夹,a插入模式,//注意:gopath不用添加到PATH
//末尾添加,注意等号别加空格及/:
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/gopath
//执行配置文件
source /etc/profile
//查看版本号
go version
go version go1.15.14 linux/amd64
//查看go环境
go env
//换国内代理,不然后面Fabric运行会报错
go env -w GOPROXY=https://goproxy.cn
export GOPROXY=https://goproxy.cn
五、安装工具包(git、curl、gcc、pip)
yum install -y git
[root@ecs-344386 local]# git version
git version 1.8.3.1
yum install -y curl
[root@ecs-344386 local]# curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.53.1 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
yum install -y gcc
[root@ecs-344386 local]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
yum -y install epel-release
sudo yum -y install python-pip
pip --version #查看pip版本
[root@ecs-344386 local]# pip --version
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)
#若要更新pip,可以使用命令pip install --upgrade pip
六、安装node
# 第一步:下载
wget https://nodejs.org/dist/latest-v14.x/node-v14.20.1-linux-x64.tar.gz
# 第二步:
tar -xzvf node-v14.20.1-linux-x64.tar.gz
mv node-v14.20.1-linux-x64 node
# 第三步:建立软连接,变为全局
ln -s /usr/local/node/bin/node /usr/bin/node
ln -s /usr/local/node/bin/npm /usr/bin/npm
ln -s /usr/local/node/bin/npx /usr/bin/npx
# 第四步:查看版本
node -v
npm -v
npx -v
# 第五步:修改环境变量
vim /etc/profile
# node
export NODE_HOME=/usr/local/node
export PATH=$NODE_HOME/bin:$PATH
export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH
# 生效node
source /etc/profile
注意:如果这里出现问题,请关闭防火墙
创建文件夹并拉取源码
方法一: echo $GOPATH # 打印出自己本地的GOPATH /usr/local/gopath mkdir -p $GOPATH/src/github.com/hyperledger cd $GOPATH/src/github.com/hyperledger #拷贝Fabric源码,码云上比github.com快 git clone https://gitee.com/mirrors/hyperledger-fabric.git mv hyperledger-fabric fabric cd fabric [root@ecs-344386 fabric]# git checkout -b v2.2.3 切换到一个新分支 'v2.2.3' cd scripts ./bootstrap.sh 2.2.3 1.4.7 -s 如果这里失败请转向手动安装
方法二:手动完成
1、下载fabric-samples源码:
cd /usr/local/gopath/src/github.com/hyperledger/fabric/scripts
git clone https://gitee.com/madridcity/fabric-samples
cd fabric-samples
git checkout -q v2.2.3
参考:(将fabric和fabric-ca解压(解压出来的bin需要合并在一起)得到bin和config文件夹,放置到fabric-sample)
下载https://github.com/hyperledger/fabric/releases/download/v2.2.3/hyperledger-fabric-linux-amd64-2.2.3.tar.gz
https://github.com/hyperledger/fabric-ca/releases/download/v1.4.7/hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz
复制到fabric-samples目录下,并解压:
tar -xzvf hyperledger-fabric-linux-amd64-2.2.3.tar.gz
tar -xzvf hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz
3.获取docker镜像:
./bootstrap.sh -sb
List out hyperledger docker images
hyperledger/fabric-tools 2.2 b6acbfaac0ef 17 months ago 431MB
hyperledger/fabric-tools 2.2.3 b6acbfaac0ef 17 months ago 431MB
hyperledger/fabric-tools latest b6acbfaac0ef 17 months ago 431MB
hyperledger/fabric-peer 2.2 17cd44c37a94 17 months ago 52.6MB
hyperledger/fabric-peer 2.2.3 17cd44c37a94 17 months ago 52.6MB
hyperledger/fabric-peer latest 17cd44c37a94 17 months ago 52.6MB
hyperledger/fabric-orderer 2.2 345ca8c4b4b1 17 months ago 36.5MB
hyperledger/fabric-orderer 2.2.3 345ca8c4b4b1 17 months ago 36.5MB
hyperledger/fabric-orderer latest 345ca8c4b4b1 17 months ago 36.5MB
hyperledger/fabric-ccenv 2.2 bd77faf79f92 17 months ago 514MB
hyperledger/fabric-ccenv 2.2.3 bd77faf79f92 17 months ago 514MB
hyperledger/fabric-ccenv latest bd77faf79f92 17 months ago 514MB
hyperledger/fabric-baseos 2.2 f98af14c474f 17 months ago 6.86MB
hyperledger/fabric-baseos 2.2.3 f98af14c474f 17 months ago 6.86MB
hyperledger/fabric-baseos latest f98af14c474f 17 months ago 6.86MB
hyperledger/fabric-ca 1.4 743a758fae29 2 years ago 154MB
hyperledger/fabric-ca 1.4.7 743a758fae29 2 years ago 154MB
hyperledger/fabric-ca latest 743a758fae29 2 years ago 154MB
测试网络:
(1)进入下面文件
cd /usr/local/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
./network.sh up
(2)创建一个通道
./network.sh createChannel
(3)使用network.sh
创建频道后,您可以使用以下命令在通道上启动链码:
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
如果出现问题,在/usr/local/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network目录下执行./network.sh down关掉联盟链相关容器,然后重新执行./network.sh up
(4)与网络交互
使用以下命令将这些二进制文件添加到您的CLI路径:
export PATH=${PWD}/../bin:$PATH
您还需要将fabric-samples
代码库中的FABRIC_CFG_PATH
设置为指向其中的core.yaml
文件:
export FABRIC_CFG_PATH=$PWD/../config/
现在,您可以设置环境变量,以允许您作为Org1操作peer
CLI:
# Environment variables for 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/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
运行以下命令用一些资产来初始化账本:
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 basic --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":"InitLedger","Args":[]}'
运行以下指令来获取添加到通道账本的资产列表:
如果成功,您将看到以下输出:
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[
{"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300},
{"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400},
{"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500},
{"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600},
{"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700},
{"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800}
]
参考资料:
1、https://hyperledger-fabric.readthedocs.io/zh_CN/release-2.2/
2、https://blog.csdn.net/weixin_44644635/article/details/109587574
3、https://blog.csdn.net/khahad/article/details/123967819
4、https://blog.csdn.net/ling1998/article/details/121278826
5、https://zhuanlan.zhihu.com/p/512144678