ubuntu20环境下fabric最新版配置——2023.03

新建ubuntu虚拟机

镜像下载地址
Index of /ubuntu-releases/20.04.5/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

https://blog.csdn.net/David_Aulier/article/details/106491083

虚拟机配置如图

联网并更新软件源

网络采用NAT模式,地址设置为虚拟机子网范围内地址。DNS设置为8.8.8.8或者114.114.114.114

更换软件源为清华源

解决虚拟机分辨率问题与虚拟机和实体机之间复制

ubuntu桌面直接复制
sudo apt update
sudo apt upgrade
sudo apt-get autoremove open-vm-tools
sudo apt-get install open-vm-tools
sudo apt-get insall open-vm-tools-desktop

然后重新启动虚拟机即可

引自 https://blog.csdn.net/Fwjian12345/article/details/107083024
使用xshell进行连接

进行xshell连接前要先在虚拟机下载ssh服务

下载vim
sudo apt-get remove vim-common
sudo apt-get install vim

安装ssh
sudo apt-get install openssh-server
service ssh start

ps -e|grep ssh   //查看ssh服务是否开启,存在sshd和ssh-agent就OK了

sudo vim /etc/ssh/sshd_config 
添加PermitRootLogin yes
sudo vim /etc/ssh/ssh_config 
PasswordAuthentication  yes   前面的#去掉

service ssh restart

前置工具准备

git、curl、wget、jq下载
sudo apt-get install git
sudo apt-get install curl 
sudo apt-get install wget
sudo apt-get install jq
Go下载——go1.17.1
go安装包
链接: https://pan.baidu.com/s/1RjOAej-2lrLdx7oLFfjQtw?pwd=8888
提取码:8888
--来自百度网盘超级会员V4的分享
安装依赖
sudo apt install libtool libltdl-dev

wget https://studygolang.com/dl/golang/go1.17.1.linux-amd64.tar.gz

sudo tar -C /usr/local -zxvf  go1.17.1.linux-amd64.tar.gz

sudo vim ~/.bashrc

配置环境变量
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

保存并退出 vim
source ~/.bashrc

go version

设置GOPROXY代理

go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOPRIVATE=.gitlab.com,.gitee.com
go env -w GOSUMDB="sum.golang.google.cn"
Node下载——18.15.0 9.5.0
wget https://nodejs.org/dist/v18.15.0/node-v18.15.0-linux-x64.tar.xz
sudo tar xvf node-v18.15.0-linux-x64.tar.xz -C /opt

sudo vim /etc/profile

export NODEJS_HOME=/opt/node-v18.15.0-linux-x64
export PATH=$PATH:$NODEJS_HOME/bin

source /etc/profile
Docker下载——20.10.6
#删除旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
下载工具
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
安装证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
#更新并安装docker-ce
sudo apt-get update
#查看可以安装的版本
apt-cache madison docker-ce
sudo apt-get install docker-ce=5:20.10.6~3-0~ubuntu-focal docker-ce-cli=5:20.10.6~3-0~ubuntu-focal containerd.io
#设置开机启动
sudo systemctl start docker
sudo systemctl enable docker
#添加自己的账户到docker组
sudo groupadd docker
sudo usermod -aG docker $USER
#重启虚拟机
#进入root角色  
sudo su root
cd /etc/docker/
sudo touch daemon.json
sudo vim daemon.json

{
    "registry-mirrors": [
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com",
        "https://d309gl61.mirror.aliyuncs.com"
    ]
}

sudo systemctl daemon-reload
sudo systemctl restart docker
Docker Compose下载——1.27.2
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose -v
curl拉取github时下载速度可能很慢,网上有一种解决方法为替换GitHub.com为hub.fastgit.xyz

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
替换为
sudo curl -L "https://hub.fastgit.xyz/docker/compose/releases/download/1.27.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
不知道是否有效,可以先用这个尝试一下,不行再按照github的下载
fabric和官方代码下载——2.2.2 1.4.9
mkdir  -p  go/src/github.com/hyperledger
cd go/src/github.com/hyperledger/

git clone https://gitee.com/z-gwei/fabric.git
cd ./fabric

git branch -a   #查看可选版本
git checkout release-2.2

若是开启代理,网络流畅,可以直接修改版本后执行脚本自动下载
若是下载速度过慢可以分别手动下载,按照下面步骤依次执行

修改bootstrap.sh文件内容

#版本修改为2.2.2与1.4.9

#下载示例代码
#将cloneSamplesRepo() 函数中的克隆地址注释并修改为下面这条代码
#git clone -b main https://github.com/hyperledger/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
git clone -b main https://gitee.com/z-gwei/fabric-samples.git && cd fabric-samples && git checkout v${VERSION}
#执行脚本只下载samples
./bootstrap.sh -db


#进入fabric-samples路径,下载fabric与fabric-ca二进制文件
https://github.com/hyperledger/fabric/releases/download/v2.2.2/hyperledger-fabric-linux-amd64-2.2.2.tar.gz
https://github.com/hyperledger/fabric-ca/releases/download/v1.4.9/hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz
#将下载得到文件解压在fabric-samples文件夹中
tar xzvf hyperledger-fabric-linux-amd64-2.2.2.tar.gz
tar xzvf hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz


#下载镜像,退回上一级
./bootstrap.sh -b -s

Github下载加速-在线工具箱 (mintimate.cn)将上面两个文件链接在工具箱中执行,使用这个网址下载速度可以在100-200k左右。

或者从我的网盘处下载
链接: https://pan.baidu.com/s/1Ge6fDO6VFf_aqv2ok-R5Ag?pwd=8888
提取码:8888
复制这段内容后打开百度网盘手机App,操作更方便哦

将一下环境变量加入~/.profile,执行source ~/.profile

export PATH=$PATH:/home/czh/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin

export PATH=$PATH:/home/czh/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/config

启动测试网络

cd fabric-samples/test-network
关闭可能正在运行的网络
./network.sh down
创建fabric网络
./network.sh up

显示如下图则表示创建成功

创建fabric网络

创建通道

./network.sh createChannel

该步骤执行的操作:

configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel

peer channel create -o localhost:7050 -c mychannel --ordererTLSHostnameOverride orderer.example.com -f ./channel-artifacts/mychannel.tx --outputBlock ./channel-artifacts/mychannel.block --tls --cafile /home/zgw/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

peer channel join -b ./channel-artifacts/mychannel.block

peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
部署chaincode链码
go语言

名称为chaincode-go,语言选择为go

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

最开始是没有go环境的,脚本会自动执行安装下载

node语言

名称为chaincode-javascript,语言选择为javascript

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-javascript/ -ccl javascript
与网络交互
首先确保自己位于test-network目录下,设置cli路径。
export PATH=${PWD}/../bin:$PATH

export FABRIC_CFG_PATH=$PWD/../config/
设置为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"]}'

修改一笔所有者:

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"]}'

更换org2的环境变量:

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051

查询账本:

peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
关闭网络
./network.sh down
本文参考链接
https://blog.csdn.net/qq_41348927/article/details/117856382

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值