1、获取root权限
sudo su
2、添加环境变量,并使环境变量生效
# 设置环境变量
vim /etc/profile
# 将以下内容追加到文件末尾
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=${PWD}
# 使环境变量生效
source /etc/profile
3、进入目标文件,并使用cryptogen工具和crypto-config.yaml文件生成证书
#进入目标文件
cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/mynetwork1
#生成证书
cryptogen generate --config=./crypto-config.yaml
4、创建channel-artifacts文件夹
mkdir channel-artifacts
5、使用configtxgen工具和configtx.yaml文件生成创世区块genesis.block
configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
6、使用configtxgen工具生成通道配置文件mychannel.tx
configtxgen -profile SevenOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
7、使用configtxgen工具生成锚节点配置文件
# 生成ResourceOrg1的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/ResourceOrg1MSPanchors.tx -channelID mychannel -asOrg ResourceOrg1MSP
# 生成DetoxificationOrg2的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/DetoxificationOrg2MSPanchors.tx -channelID mychannel -asOrg DetoxificationOrg2MSP
# 生成YuanssOrg3的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/YuanssOrg3MSPanchors.tx -channelID mychannel -asOrg YuanssOrg3MSP
# 生成YuansOrg4的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/YuansOrg4MSPanchors.tx -channelID mychannel -asOrg YuansOrg4MSP
# 生成WarehouseOrg5的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/WarehouseOrg5MSPanchors.tx -channelID mychannel -asOrg WarehouseOrg5MSP
# 生成AlarmOrg6的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/AlarmOrg6MSPanchors.tx -channelID mychannel -asOrg AlarmOrg6MSP
# 生成ExternalOrg7的锚节点配置文件
configtxgen -profile SevenOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/ExternalOrg7MSPanchors.tx -channelID mychannel -asOrg ExternalOrg7MSP
8、启动网络
# 配置环境变量,CA节点必需
export BYFN_CA1_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/resourceorg1.example.com/ca && ls *_sk)
export BYFN_CA2_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/detoxificationorg2.example.com/ca && ls *_sk)
export BYFN_CA3_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/yuanssorg3.example.com/ca && ls *_sk)
export BYFN_CA4_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/yuansorg4.example.com/ca && ls *_sk)
export BYFN_CA5_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/warehouseorg5.example.com/ca && ls *_sk)
export BYFN_CA6_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/alarmorg6.example.com/ca && ls *_sk)
export BYFN_CA7_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/externalorg7.example.com/ca && ls *_sk)
# 根据指定配置文件启动容器服务
docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml -f docker-compose-couch.yaml up -d
9、查看当前的docker的启动情况
docker ps -a
10、进入cli容器创建通道(另起终端)
#连接cli服务
docker exec -it cli bash
#创建通道信息
peer channel create -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/mychannel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#将生成的文件复制到channel-artifacts文件夹中
cp mychannel.block channel-artifacts/
11、将peer节点加入通道
#将当前节点加入通道
peer channel join -b channel-artifacts/mychannel.block
#切换节点(快捷方式)
source scripts/utils.sh
setGlobals 1 1
12、更新锚节点
#切换节点
source scripts/utils.sh
setGlobals 0 1
#更新ResourceOrg1锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/ResourceOrg1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#更新DetoxificationOrg2锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/DetoxificationOrg2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#更新YuanssOrg3锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/YuanssOrg3MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#更新YuansOrg4锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/YuansOrg4MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#更新WarehouseOrg5锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/WarehouseOrg5MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#更新AlarmOrg6锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/AlarmOrg6MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#更新ExternalOrg7锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/ExternalOrg7MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
13、链码相关操作
#安装链码
peer chaincode install -p github.com/chaincode/seed_potatoes/alarm -n alarm -v 1.0
peer chaincode install -p github.com/chaincode/ecdsa/ecdsa -n ecdsa -v 1.0
#实例化链码
peer chaincode instantiate -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -n alarm -v 1.0 -c '{"Args":["init"]}' -C mychannel
peer chaincode instantiate -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -n ecdsa -v 1.0 -c '{"Args":["init"]}' -C mychannel
#初始化账本
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -n alarm -c '{"Args":["initLedger"]}' -C mychannel
#根据批次ID查询种薯基本情况
peer chaincode query -C mychannel -n alarm -c '{"Args":["querySeedPotatoesByBatchId", "B070"]}'
#添加种薯
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["createSeedPotatoes", "B071", "B071", "宣薯2号良种", "国内", "一类种薯"]}'
#记录种薯资源保存环节生长过程
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordResourceGrow", "B070", "B070", "B070R041", "丽薯7号良种", "丽薯7号", "国内", "RL019", "2022-06-06 09:31:10", "张青", "江格", "合格"]}'
#根据资源ID查询种薯资源保存环节生长过程
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryResourceGrowByResourceId", "B070R041"]}'
#记录种薯脱毒苗扩繁环节生长过程
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordDetoGrow", "B070", "B070", "B070R041", "丽薯7号良种", "丽薯7号", "B070R041D101", "CR014", "2022-07-06 17:42:18", "袁艺", "孙前", "合格"]}'
#根据扩繁ID查询种薯脱毒苗扩繁环节生长过程
peer chaincode query -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["queryDetoGrowByDetoxificateId", "B070R041D101"]}'
#记录报警信息(扩繁阶段)
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordWarn", "B070", "B070", "B070R041", "W079", "2022-07-06 17:42:18", "脱毒苗扩繁环节", "批次ID不符", "袁艺"]}'
#根据报警ID查询报警信息(扩繁阶段)
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryWarnInfoByWarnId", "W079"]}'
#记录原原种繁育环节生长过程
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordYuanssGrow", "B070", "B070", "B070R041D101", "丽薯7号良种", "丽薯7号", "B070R041D101YY102", "GH009", "2022-08-04 13:05:18", "王青山", "王廷", "合格"]}'
#根据原原种ID查询原原种繁育环节生长过程
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryYuanssGrowByYuanssId", "B070R041D101YY102"]}'
#记录报警信息(原原种繁育阶段)
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordWarn", "B070", "B070", "B070R041D101", "W081", "2022-08-04 13:05:18", "原原种培育环节", "品种不正确", "王青山"]}'
#根据报警ID查询报警信息(原原种繁育阶段)
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryWarnInfoByWarnId", "W081"]}'
#记录原种繁育环节生长过程
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordYuansGrow", "B070", "B070", "B070R041D101YY102", "丽薯7号良种", "丽薯7号", "一级", "B070R041D101YY102Y090", "L032", "2022-09-10 09:49:41", "赵十", "罗南", "合格"]}'
#根据原种ID查询原种繁育环节生长过程
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryYuansGrowByYuansId", "B070R041D101YY102Y090"]}'
#记录报警信息(原种繁育阶段)
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordWarn", "B070", "B070", "B070R041D101YY102", "W086", "2022-09-10 09:49:41", "原种培育环节", "品种不正确", "赵十"]}'
#根据报警ID查询报警信息(原种繁育阶段)
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryWarnInfoByWarnId", "W086"]}'
#记录仓储环节生长过程
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordWarehouseGrow", "B070", "B070", "B070R041D101YY102Y090", "丽薯7号良种", "丽薯7号", "一级", "B070R041D101YY102Y090WH066", "SH013", "2022-10-06 16:30:01", "罗金", "王刊", "合格"]}'
#根据仓储ID查询仓储环节生长过程
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryWarehouseByWarehouseId", "B070R041D101YY102Y090WH066"]}'
#记录报警信息(仓储阶段)
peer chaincode invoke -o orderer0.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n alarm -c '{"Args":["recordWarn", "B070", "B070", "B070R041D101YY102Y090", "W089", "2022-10-06 16:30:01", "仓储环节", "品种不正确", "罗金"]}'
#根据报警ID查询报警信息(仓储阶段)
peer chaincode query -C mychannel -n alarm -c '{"Args":["queryWarnInfoByWarnId", "W089"]}'
14、进入CouchDB数据库页面
本机:http://localhost:5984/_utils/
非本机:http://192.168.72.131:5984/_utils/#database/mychannel_resourcecc/_all_docs