本文分享在 Centos7下搭建 Hyperledger Fabric 2.4 环境并进行简单的网络测试。 (下面的一切 *** 作均在ROOT权限下进行 *** 作)
一、环境准备 1.Git 安装git。如果显示apt-get不是命令,就执行下面的两步下载apt-get(这个工具有点类似于yum,也是一样下载工具)
$ apt-get install git
下载apt-get
$ curl https://raw.githubusercontent.com/dvershinin/apt-get-centos/master/apt-get.sh -o /usr/local/bin/apt-get
修改apt-get权限
$ chmod 0755 /usr/local/bin/apt-get
2.Golang
安装golang(注意:如果版本过旧请先删除旧的版本再下载新的版本):
$ which go
/usr/local/go/bin/go #(自己go所在的位置)
$ rm -rf /usr/local/go/bin #删除自己旧版本的go
下载go包 (go包的下载地址:https://go.dev/dl/go1.18.linux-amd64.tar.gz)
$ curl -O https://go.dev/dl/go1.18.linux-amd64.tar.gz
解压
$ tar -zxvf go1.18.linux-amd64.tar.gz
将解压后的go移动到/usr/bin目录下
$ mv go /usr/bin
配置go环境变量
$ vim /etc/profile
#将后面的这三条命令输入到文件的最后保存
#配置go环境变量
export GOROOT=/usr/lib/go
export GOPATH=/var/goproject
export PATH=$PATH:$GOROOT/bin
$ source /etc/profile #让配置生效
检查golang是否安装成功
$ go version
3.Docker
参照官网:https://docs.docker.com/engine/install/centos/
$ yum update -y
卸载旧版本的docker
$ yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
下载工具包,以及设置仓库
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
下载docker、docker-ce
$ yum install docker-ce docker-ce-cli containerd.io
5.开启docker
$ systemctl start docker #开机启动
$ systemctl enable docker #设置开机启动
4.docker-compose
参照官网: https://docs.docker.com/compose/install/
$ which docker-compose #查找位置
/usr/bin/docker-compose
$ rm /usr/bin/docker-compose
安装docker-compose
$ curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose
给docker-compose执行权限
$ chmod +x /usr/bin/docker-compose
检查是否安装成功
$ docker-compose -v
docker-compose version 1.29.2, build 5becea4c
5.jq
安装jq,并赋予jq权限
$ yum install jq
$ chmod +x jq
二、Fabric 源码安装
这里可能需要能够安全上网,否则下载过程可能会出现问题!
1.在$PATH路径下安装fabric$ echo $GOPATH # 打印出自己本地的GOPATH
/var/goproject
$ mkdir -p $GOPATH/src/github.com/hyperledger
$ cd $GOPATH/src/github.com/hyperledger
$ git clone https://github.com/hyperledger/fabric.git
2. 执行安装脚本
现在Fabric官方已经把下载和解析Fabric包、Fabric-ca包以及fabric-samples工程以及一堆镜像的过程封装到 ./bootstrap.sh脚本中。
执行安装脚本$ cd fabric/scripts
$ ./bootstrap.sh
检查是否安装成功三、测试test-network 1. 进入test-network目录**(后面的所有命令都是在test-network目录下执行)**
$ cd fabric-samples/test-network
2. 开启测试网络
$ ./network.sh up
当我们看见下面所示的代码时,则说明网络创建成功
Creating network "fabric_test" with the default driver
Creating volume "compose_orderer.example.com" with default driver
Creating volume "compose_peer0.org1.example.com" with default driver
Creating volume "compose_peer0.org2.example.com" with default driver
Creating orderer.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a56dd0e5aa9 hyperledger/fabric-tools:latest "/bin/bash" 3 seconds ago Up Less than a second cli
f125c6ead209 hyperledger/fabric-peer:latest "peer node start" 13 seconds ago Up 3 seconds 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:9445->9445/tcp, :::9445->9445/tcp peer0.org2.example.com
02dfc885e168 hyperledger/fabric-peer:latest "peer node start" 14 seconds ago Up 2 seconds 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:9444->9444/tcp, :::9444->9444/tcp peer0.org1.example.com
3fc8414de6fc hyperledger/fabric-orderer:latest "orderer" 14 seconds ago Up 2 seconds 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:9443->9443/tcp, :::9443->9443/tcp orderer.example.com
3. 创建channel
$ ./network.sh createChannel # 这是创建默认名称的通道 mychannel
如果看见下图所示的代码时,则说明channel创建成功
Channel 'mychannel' created
4. 在channel上安装部署chaincode
这里面包含了在peer节点上安装链码,还有在channel上定义链码。
$ ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
当看到下面这些代码时,就表示成功
Chaincode is installed on peer0.org1
Chaincode is installed on peer0.org2
Query installed successful on peer0.org1 on channel
Chaincode definition approved on peer0.org1 on channel 'mychannel'
Chaincode definition approved on peer0.org2 on channel 'mychannel'
Chaincode definition committed on channel 'mychannel'
Query chaincode definition successful on peer0.org1 on channel 'mychannel'
Query chaincode definition successful on peer0.org2 on channel 'mychannel'
5.与网络进行交互
将fabric-samples/bin目录下的命令添加到环境变量中来,为了方便可以将直接将该变量写入到 /etc/profile中
$ export PATH=${PWD}/../bin:$PATH
$ export FABRIC_CFG_PATH=$PWD/../config/
# 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":[]}'
如果执行成功可以看到如下的代码
-> INFO 001 Chaincode invoke successful. result: status:200
还可以执行下面的命令来查询账本
$ 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}
]
关闭网络
./network.sh down
四、使用fabric-ca测试test-network
1. 使用-ca 标志来开启网络
$ ./network.sh up -ca
然后看到如下内容,就表示创建成功
3579817220d4 hyperledger/fabric-tools:latest "/bin/bash" 2 seconds ago Up Less than a second cli
1713c103754b hyperledger/fabric-peer:latest "peer node start" 5 seconds ago Up 2 seconds 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:9444->9444/tcp, :::9444->9444/tcp peer0.org1.example.com
c69e62ebe784 hyperledger/fabric-peer:latest "peer node start" 5 seconds ago Up 2 seconds 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:9445->9445/tcp, :::9445->9445/tcp peer0.org2.example.com
538467bee836 hyperledger/fabric-orderer:latest "orderer" 6 seconds ago Up 2 seconds 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:9443->9443/tcp, :::9443->9443/tcp orderer.example.com
2ec1a7d91ed3 hyperledger/fabric-ca:latest "sh -c 'fabric-ca-se…" 18 seconds ago Up 15 seconds 0.0.0.0:9054->9054/tcp, :::9054->9054/tcp, 7054/tcp, 0.0.0.0:19054->19054/tcp, :::19054->19054/tcp ca_orderer
5de2ee937a6f hyperledger/fabric-ca:latest "sh -c 'fabric-ca-se…" 18 seconds ago Up 15 seconds 0.0.0.0:7054->7054/tcp, :::7054->7054/tcp, 0.0.0.0:17054->17054/tcp, :::17054->17054/tcp ca_org1
820803a9abb9 hyperledger/fabric-ca:latest "sh -c 'fabric-ca-se…" 18 seconds ago Up 15 seconds 0.0.0.0:8054->8054/tcp, :::8054->8054/tcp, 7054/tcp, 0.0.0.0:18054->18054/tcp, :::18054->18054/tcp ca_org2
2. 可以通过 tree 命令来查看Org1管理员用户的MSP文件夹结构和文件
$ tree organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/
如果出现tree命令找不到就执行下面的命令安装tree,再次执行上面的命令
yum -y install tree
成功后可以看到如下的内容
organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/
└── msp
├── cacerts
│ └── localhost-7054-ca-org1.pem
├── config.yaml
├── IssuerPublicKey
├── IssuerRevocationPublicKey
├── keystore
│ └── d38467a8ec72b225fa9dbc49d663143580db970e16c78a0c099d105bfb4c5053_sk
├── signcerts
│ └── cert.pem
└── user
其中 signcerts 文件夹中存放着管理员用户的证书,keystore 文件夹中存放着私钥。
总结
以上就是全部的安装测试过程,希望大家安装测试成功。
参考资料:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)