目录

安装环境

0. 前提条件:

1. 下载Fabric(版本2.3.3)

2. 安装示例、二进制和 Docker 镜像(直接看方法三)

(1)方法一(未成功):按官网直接执行

(2)方法二(未成功):直接执行bootstrap.sh脚本 ,与方法一相同的错误

(3)方法三: 直接下载bootstrap.sh中的文件

3. 启动测试网络

4. 创建通道(channel)

5. 安装go依赖

6. 在通道启动一个链码

7. 与网络交互

参考文档


安装环境

        虚拟机:VMware Workstation 15 Pro

        虚拟机安装的操作系统:Centos7

0. 前提条件:

(1)安装Git  参考 Centos7 安装 git_ling1998的博客-CSDN博客

(2)安装Go  参考 Centos7 安装 Go_ling1998的博客-CSDN博客

(3)安装Docker-ce  参考 Centos7 安装 Docker_ling1998的博客-CSDN博客

(4)安装Docker Compose  参考 Centos7 安装 Docker Compose_ling1998的博客-CSDN博客

(5)安装Nodejs及npm 参考 Centos7 安装 Nodejs_ling1998的博客-CSDN博客

以上第5步Nodejs在本文中没有使用,为方便以后使用,所以提前安装了。

1. 下载Fabric(版本2.3.3)

[root@localhost ~]# mkdir -p $GOPATH/src/github.com/hyperledger  # 创建下载目录
[root@localhost ~]# cd $GOPATH/src/github.com/hyperledger    #进入目录
[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric.git

    下载太慢了,要等等等(可以使用科学上网工具)

    下载时可能出现下面错误,多尝试几次

2. 安装示例、二进制和 Docker 镜像(直接看方法三)

(1)方法一(未成功):按官网直接执行

curl -sSL https://bit.ly/2ysbOFE | bash -s  #官网

(2)方法二(未成功):直接执行bootstrap.sh脚本 ,与方法一相同的错误

#进入fabric/scripts目录
cd fabric/scripts
#执行bootstrap.sh
./bootstrap.sh

(3)方法三: 直接下载bootstrap.sh中的文件

        参见bootstrap.sh脚本中相关内容,得到3个下载地址

VERSION=2.3.3       #fabric版本
......
CA_VERSION=1.5.2    #fabric-ca版本
ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
......
cloneSamplesRepo() {
    ......
        #下载fabirc-samples
        git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
    ......
}
......
pullBinaries() {
    echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
    #下载fabric二制进文件
    download "${BINARY_FILE}" "https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
    ......
    #下载fabric-ca二进制文件
    download "${CA_BINARY_FILE}" "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
    ......
}
......
BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz

   关于bootstrap.sh脚本详细解析可参见Fabric v2.3 下载二进制文件和镜像shell脚本解析_ling1998的博客-CSDN博客

         首先,下载fabric-sample,尝试使用git与wget,多次执行报错后(如下图),直接在windows中下载,然后传到centos7对应的目录下(如下图目录图)。下载地址:GitHub - hyperledger/fabric-samples

         上传到fabric/scripts目录中,与bootstrap.sh脚本同层级

        

       其次,下载fabric二进制文件(版本2.3.3)使用wget报错,直接在windows下载,然后上传到centos7中解压。下载地址为:https://github.com/hyperledger/fabric/releases/download/v2.3.3/hyperledger-fabric-linux-amd64-2.3.3.tar.gz

     最后,下载fabric-ca二进制文件(版本1.5.2),需关闭防火墙,同时科学上网。如果wget报错,建议还是在windows浏览器中下载,然后上传到centos7中解压。下载地址为:​​​​​https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz

[root@localhost scripts]# systemctl stop firewalld.service  #关闭防火墙
[root@localhost scripts]# firewall-cmd --state    #查看防火墙状态
not running
[root@localhost scripts]# wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz #下载

    以上2个压缩文件都下载到了fabric/scripts目录中

 

 解压文件至fabric-samples中

[root@localhost scripts]# tar -xvf hyperledger-fabric-linux-amd64-2.3.3.tar.gz -C fabric-samples
[root@localhost scripts]# tar -xvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz.tar.gz -C fabric-samples

 

 执行./bootstrap.sh脚本

[root@localhost scripts]# ./bootstrap.sh -b  #仅拉取fabric的镜像文件

设置全局都可以使用二进制文件

[root@localhost scripts]# cd fabric-samples/bin  #进入bin目录
[root@localhost bin]# ls
configtxgen    cryptogen  fabric-ca-client  idemixgen  osnadmin
configtxlator  discover   fabric-ca-server  orderer    peer
[root@localhost bin]# cp * /usr/local/bin  #将二进制文件copy至/usr/local/bin下

3. 启动测试网络

[root@localhost bin]# cd ..                           #回到fabric-samples目录
[root@localhost fabric-samples]# cd test-network      #进入test-network目录
[root@localhost test-network]# ./network.sh           #启动测试网络脚本
-bash: ./network.sh: Permission denied                #提示执行network.sh文件时没有权限
[root@localhost test-network]# chmod u+x network.sh   #授权
[root@localhost test-network]# ./network.sh           #再次执行测试网络脚本
[root@localhost test-network]# ./network.sh up        #启动测试网络

4. 创建通道(channel)

[root@localhost test-network]# ./network.sh createChannel

5. 安装go依赖

[root@localhost fabric-samples]# cd asset-transfer-basic/chaincode-go/  #进入目录
[root@localhost chaincode-go]# ls                                       #查看目录内容
assetTransfer.go  chaincode  go.mod  go.sum                             #此时没有vendor目录
[root@localhost chaincode-go]# go env                                   #查看go环境配置
......
GOPROXY="https://proxy.golang.org,direct"                               #查看代理设置
......
[root@localhost chaincode-go]# go env -w GOPROXY=https://goproxy.cn     #更改代理
[root@localhost chaincode-go]# go mod vendor                            #安装依赖
[root@localhost chaincode-go]# ls                                       #查看目录内容
assetTransfer.go  chaincode  go.mod  go.sum  vendor                     #此时出现vendor目录

6. 在通道启动一个链码

[root@localhost chaincode-go]# cd ..
[root@localhost asset-transfer-basic]# cd ..        
[root@localhost fabric-samples]# cd test-network    #进入到test-network目录
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go   #在通道中启动一个链码(官网)

若启动链码时出现错误:./network.sh: line 270: scripts/deployCC.sh: Permission denied

则去授权,最好将目录下的所有.sh文件都授权,避免之后执行时再出现授权错误

[root@localhost test-network]# cd scripts
[root@localhost scripts]# ls -l
total 36
-rw-r--r--. 1 root root  2284 Oct 25 02:33 configUpdate.sh
-rwxr--r--. 1 root root  2562 Oct 25 02:33 createChannel.sh
-rw-r--r--. 1 root root 11675 Oct 25 02:33 deployCC.sh
-rw-r--r--. 1 root root  3476 Oct 25 02:33 envVar.sh
drwxr-xr-x. 2 root root    58 Oct 25 02:33 org3-scripts
-rw-r--r--. 1 root root  1941 Oct 25 02:33 setAnchorPeer.sh
-rw-r--r--. 1 root root  7269 Oct 25 02:33 utils.sh
[root@localhost scripts]# chmod -R +x ../scripts        #目录下所有文件都授权
[root@localhost scripts]# ls -l
total 36
-rwxr-xr-x. 1 root root  2284 Oct 25 02:33 configUpdate.sh
-rwxr-xr-x. 1 root root  2562 Oct 25 02:33 createChannel.sh
-rwxr-xr-x. 1 root root 11675 Oct 25 02:33 deployCC.sh
-rwxr-xr-x. 1 root root  3476 Oct 25 02:33 envVar.sh
drwxr-xr-x. 2 root root    58 Oct 25 02:33 org3-scripts
-rwxr-xr-x. 1 root root  1941 Oct 25 02:33 setAnchorPeer.sh
-rwxr-xr-x. 1 root root  7269 Oct 25 02:33 utils.sh
[root@localhost scripts]# cd ..
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

7. 与网络交互

(按官网上步骤执行即可,参见官网 使用Fabric的测试网络 — hyperledger-fabricdocs master 文档

[root@localhost test-network]# export PATH=${PWD}/../bin:$PATH
[root@localhost test-network]# export FABRIC_CFG_PATH=$PWD/../config/
[root@localhost test-network]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
[root@localhost test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
[root@localhost test-network]# export CORE_PEER_ADDRESS=localhost:7051
[root@localhost test-network]# 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":[]}'
2021-10-25 17:53:30.230 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'[{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},{"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},{"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10},{"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10},{"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15},{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15}]
[root@localhost test-network]# 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":"TransferAsset","Args":["asset6","Christopher"]}'
2021-10-25 17:55:15.507 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 
[root@localhost test-network]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
[root@localhost test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
[root@localhost test-network]# export CORE_PEER_ADDRESS=localhost:9051
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Christopher","Size":15}

参考文档:

使用Fabric的测试网络 — hyperledger-fabricdocs master 文档

​​​​​​关于fabric中执行./bootstrap.sh之后出错的解决方案_lakersssss24的博客-CSDN博客

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐