Hyperledger fabric 2.2.1 环境搭建

Hyperledger fabric 2.2.1 环境搭建
	https://www.cnblogs.com/Earth-SmaThing/p/13570540.html
	最好使用root身份用户,否则很多权限被禁止(例如docker-compose命令找不到)
	基础环境搭建-----------------------------------------------------
		### docker 安装 (可参考https://my.oschina.net/ruoli/blog/1592170)
		如果服务器上有旧版的docker,需要先执行卸载操作。
		$ sudo yum remove docker \ 
									   docker-common \ 
									   docker-selinux \ 
									   docker-engine
								   
	随后开始安装docker-ce
		$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

		$ sudo yum-config-manager \
		  --add-repo \
		https://download.docker.com/linux/centos/docker-ce.repo
		或
		鉴于国内网络问题,强烈建议使用国内源,如下:
		$ sudo yum-config-manager \
		    --add-repo \
		    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

		$ sudo yum-config-manager --enable docker-ce-edge

		$ sudo yum-config-manager --enable docker-ce-test

		$ sudo yum-config-manager --disable docker-ce-edge

		$ sudo yum makecache fast

		$ sudo yum install docker-ce

	执行查询docker版本号,看是否安装成功
	docker --version

	docker启动
	service docker start

	docker开机自启(视具体情况使用)
	chkconfig docker on

	### docker-compose 安装
	需要服务器支持 curl 功能,如果服务器不支持 curl 功能,需要手动进行安装
	yum install curl

	执行如下操作下载docker-compose
	sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

	下载目录为:
	/usr/local/bin/docker-compose

	为其添加可执行权限:
	sudo chmod +x /usr/local/bin/docker-compose

	查询版本信息:
	docker-compose --version

	### Go语言安装
	安装包下载
	curl -O https://golang.google.cn/doc/install?download=go1.15.linux-amd64.tar.gz

	解压缩文件包
	tar -C /usr/local -xzf go1.15.linux-amd64.tar.gz

	配置 go 的环境变量
		修改 /etc/profile 文件使其永久生效
		1 export PATH=$PATH:/usr/local/go/bin
		2 export GOPATH=/opt/gopath

		修改文件后,执行如下命令,应用上述参数
		source /etc/profile
		
	查看当前 go 版本信息
	go version

	也可通过下述命令查看是否安装成功
	echo $PATH
Fabric源码及镜像文件处理-------------------------------------------
	下载 fabric 源码是因为要用到源码中的例子和工具,工具编译需要用到 go 语言环境,因此需要把源码目录放到 GOPATH 下,路径设置为 /opt/gopath
	可以使用 Git 命令下载源码(耗时会比较长):
	cd /opt/gopath/src/github.com/hyperledger/
	git clone https://github.com/hyperledger/fabric.git

	如果没有 git 命令,需要先执行如下命令,构建 git 环境:
	yum install git

	默认Fabric源码下载版本为 2.2.0

	Fabric Docker 镜像的下载:
	cd /opt/gopath/src/github.com/hyperledger/fabric/scripts
	./bootstrap.sh
	等待下载完成即可

	至此,Fabric源码,fabric-samples 源码、fabric 镜像的工作已经完成
网络环境测试---------------------------------------------------------
	启动 Fabric 网络  必须root身份 否则network.sh文件里面的docker-compse命令无法找到
	cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
	./network.sh up

	建立通道
	./network.sh createChannel

	在通道上启动链码
	./network.sh deployCC

	如果链码启动失败,手动添加 go 代理
	go env -w GO111MODULE=on
	go env -w GOPROXY=https://goproxy.cn,direct
	再次启动链码即可

	在test-network 目录进行如下环境变量设置
	export PATH=${PWD}/../bin:${PWD}:$PATH
	export FABRIC_CFG_PATH=$PWD/../config/

	继续设置如下环境变量,允许以 peer Org1 的形式操作 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

	完成后,source /etc/profile

	获取已添加到通道分类账中的汽车列表
	peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
	上述命令可能会出现报错提示,如下(如果是提示某些文件不存在,我的一个解决办法是,关闭网络,启动网络前,source /etc/profile):
	Error: endorsement failure during query. response: status:500 message:"make sure the chaincode fabcar has been successfully defined on channel mychannel and try again: chaincode fabcar not found"
	查看报错信息,我们可以知道,链码 fabcar 没有在通道上被定义,所以我们进入对应目录,执行启动指令:
	cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar
	./startFabric.sh
	等待执行结束即可 切记执行命令如果还报错如下
		报错:
			Error: failed to create deliver client for orderer: orderer client failed to connect to orderer.example.com:7050: failed to create new connection: connection error: desc = “transport: error while dialing: dial tcp 192.168.1.20:7050: connect: no route to host”
		那么需要进行关闭防火墙操作然后再次执行./startFabric.sh(注意语言环境)
			systemctl status firewalld.service          #查看防火墙状态
			systemctl stop firewalld.service            #停止firewall
			systemctl disable firewalld.service         #禁止firewall开机启动

	我们现在可以重新进入 test-network 目录,执行命令
	peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
	可以看到,正常返回结果了
	[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
	
	上述调试工作相继完成后,我们继续如下操作(仍然在 test-network 目录下),执行关闭网络指令:
	./network.sh down
	接着,编辑profile文件,添加如下内容(因为资产转移链码的背书策略要求事务由 Org1 和 Org2 签名,chaincode invoke 命令需要同时针对 peer0.org1.example.com 和 peer0.org2.example.com 使用 --peeradresses 标志,而且,由于为网络启用了 TLS, 该命令还需要使用 --tlsRootCertFiles 标志为每个对等方引用 TLS 证书。):
	# Environment variables for Org2 注意${PWD}路径所指 所以还是在当前test-network目录下执行如下

		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
		保存退出

		source /etc/profile
		
	启动测试网络:
	./network.sh up
	./network.sh createChannel
	./network.sh deployCC
	运行如下命令初始化资产分类账:
		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":[]}'
	如果有如下报错信息,可能是链码没有在通道中被成功定义,关闭测试网络后,重新执行启动网络操作:
		Error: endorsement failure during invoke. response: status:500 message:"make sure the chaincode basic has been successfully defined on channel mychannel and try again: chaincode basic not found"
	上述初始化指令的执行成功返回结果信息如下:
		2020-08-28 11:20:52.062 CST [chaincodeCmd] chaincodeInvokeOrQuery -> 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}]
	当网络成员希望转移或更改分类账上的资产时,会调用链码。使用以下命令更改分类账上的资产所有者:
		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"]}'
	返回如下提示信息,即表示更改成功:
		2020-08-28 11:21:29.252 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
	使用如下命令查询刚刚的转移信息:
		peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
	返回结果如下:
		{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}
	
启动过程问题	
		Error: got unexpected status: BAD_REQUEST -- error authorizing update: error validating ReadSet: readset expected key [Group]  /Channel/Application at version 0, but got version 1
		此问题是历史数据未清除干净,可能造成的原因:由于在本机尝试过fabric的e2e_cli,fabric-
		samples的first-network启动部署测试,多个启动时未正常关闭,根源是由于有一个已经存在的
		channel,阻止进一步的执行而引起的,执行如下后再次启动即可
		 ./network.sh down

智能合约部署到使用全过程:
	https://hyperledger-fabric.readthedocs.io/en/latest/deploy_chaincode.html
sdk方式(非CLI peer方式访问chaincode network和智能合约)
	https://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html
商业票据-案例完整运行流程
	https://hyperledger-fabric.readthedocs.io/en/latest/tutorial/commercial_paper.html
Fabric快速入门讲解培训
	https://baijiahao.baidu.com/s?id=1653880880028490036&wfr=spider&for=pc
阿里云源配置
	https://www.freesion.com/article/4261196453/			
fabric的一些基础知识点讲解 加深理解上述环境运行过程
	https://www.cnblogs.com/qiangjiyi/p/9562480.html
	https://www.cnblogs.com/efish/p/hyperledger-fabric-practice.html
	https://www.taohui.pub/2018/05/26/%e5%8c%ba%e5%9d%97%e9%93%be%e5%bc%80%e6%ba%90%e5%ae%9e%e7%8e%b0hyperledger-fabric%e6%9e%b6%e6%9e%84%e8%af%a6%e8%a7%a3/
	https://www.taohui.pub/2018/05/22/%e5%8c%ba%e5%9d%97%e9%93%be%e5%bc%80%e6%ba%90%e5%ae%9e%e7%8e%b0fabric%e5%bf%ab%e9%80%9f%e9%83%a8%e7%bd%b2%e5%8f%8acli%e4%bd%93%e9%aa%8c/
hyperledger explorer 环境搭建
	https://www.cnblogs.com/Earth-SmaThing/p/13582615.html
IBM分步详解 Fabric 区块链网络的部署 * 推荐细品
	https://developer.ibm.com/zh/articles/cl-lo-hyperledger-fabric-practice-analysis/
	https://developer.ibm.com/zh/series/category/blockchain/?fa=date%3ADESC&fb=
阿里云fabric
	https://help.aliyun.com/document_detail/88774.html
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值