Ubuntu18.04安装Fabric和部署链代码------2

前置配置博客:
Ubuntu18.04安装Fabric和部署链代码------1

2 Fabric官方链代码案例部署

2.1 前提配置

从github上拉取fabric-samples官方测试案例(最新)

cd $GOPATH/src/github.com/hyperledger
git clone "https://github.com/hyperledger/fabric-samples.git"

下载Fabric二进制文件包(使用了github加速)

wget https://hub.fastgit.org/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-darwin-amd64-2.3.2.tar.gz
#解压
sudo tar -C ./ -zxf hyperledger-fabric-darwin-amd64-2.3.2.tar.gz

得到两个目录bin(可执行文件)和config(一些配置文件如core.yml)

复制到$GOPATH/src/github.com/hyperledger/fabric-samples的相同bin和config目录下(没有则创建)

mkdir $GOPATH/src/github.com/hyperledger/fabric-samples/bin
mkdir $GOPATH/src/github.com/hyperledger/fabric-samples/config

cp ./bin/* $GOPATH/src/github.com/hyperledger/fabric-samples/bin
cp ./config/* $GOPATH/src/github.com/hyperledger/fabric-samples/config

上面生成的fabric-ca-client、fabric-ca-server也复制到bin

cp $GOPATH/src/github.com/hyperledger/fabric-ca/release/linux-amd64/bin/fabric-ca-client $GOPATH/src/github.com/hyperledger/fabric-samples/bin

cp $GOPATH/src/github.com/hyperledger/fabric-ca/release/linux-amd64/bin/fabric-ca-server $GOPATH/src/github.com/hyperledger/fabric-samples/bin

查看

cd $GOPATH/src/github.com/hyperledger/fabric-samples/
ls ./bin
ls ./config

Ubuntu18.04安装Fabric和部署链代码------2_第1张图片

环境变量

vim ~/.profile
在文件最后添加以下内容
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric-samples/bin
更新配置文件
source ~/.profile

加权限(这一步重要不然不识别可执行文件)
一定要确保bin目录下文件可执行

chmod +x $GOPATH/src/github.com/hyperledger/
chmod +x $GOPATH/src/github.com/hyperledger/fabric-samples/bin/*

如果这步还是失败的话可以先用官方fabric编译make出来的bin目录下的文件
编译参考前一篇博客1.9.1 Fabric环境部署
复制命令(覆盖即可)

cp $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/* $GOPATH/src/github.com/hyperledger/fabric-samples/bin

2.2 部署官方test-network案例

启动测试网络,文档地址:
https://hyperledger-fabric.readthedocs.io/en/latest/test_network.html
创建一个Fabric网络,该网络由两个对等节点、一个排序节点组成,还包括创建一个名为mychannel的通道,Org1和Org2的成员都在通道内

记得启动docker
sudo systemctl start docker
cd $GOPATH/src/github.com/hyperledger/fabric-samples/test-network
./network.sh up -ca
./network.sh createChannel

若运行到最后并成功,则fabric环境部署完成,出现错误请检查前面有没有都做好
或者看官方最新英文文档
https://hyperledger-fabric.readthedocs.io/en/latest/test_network.html]
Ubuntu18.04安装Fabric和部署链代码------2_第2张图片

Ubuntu18.04安装Fabric和部署链代码------2_第3张图片

2.3 链代码部署运行

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

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/[email protected]/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"]}'

关闭

./network.sh down

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