在ubuntu18.04的fabric1.2.0网络中手动部署chaincode(完全跑通版)

首先根据官方网站给出的步骤部署好fabric网络
官方网站地址https://hyperledger-fabric.readthedocs.io/en/release-1.2/getting_started.html

在fabric/scripts/fabric-samples/first-network目录下执行如下操作:
//修改docker-compose-cli.yaml中的CORE_LOGGING_LEVEL = DEBUG
CORE_LOGGING_LEVEL = DEBUG

//用cryptogen工具产生证书
cryptogen generate
Alt text

//用configtxgen创建创世快,channel和peer
cryptogen generate –config=./crypto-config.yaml
export FABRIC_CFG_PATH=$PWD
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
Alt text

//常见通道配置事务
export CHANNEL_NAME=testchannel && configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./
channel-artifacts/channel.tx -channelID $CHANNEL_NAME
Alt text

//为org1和org2锚定peer
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx
-channelID $CHANNEL_NAME -asOrg Org1MSP
Alt text

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx
-channelID $CHANNEL_NAME -asOrg Org2MSP
Alt text

//打开网络
docker-compose -f docker-compose-cli.yaml up -d
Alt text

//配置环境变量for PEER0
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/
org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID=”Org1MSP”
exportCORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/
org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
Alt text

//在网络中加入其它的通道
docker exec -it cli bash //进入cli容器(这是进入默认的org1.peer0的)
Alt text

可以通过下面的修改默认的Environment variables for PEER0
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID=”Org1MSP”
exportCORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

//配置环境变量
export CHANNEL_NAME=testchannel

//创建通道
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx
–tls –cafile peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/
channel.tx –tls –cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/
example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Alt text

//把peer加入channel(此时是peer0.org1.example.com)
peer channel join -b mychannel.block
Alt text

修改环境变量把其他的peer加入channel
Environment variables for PEER0
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/
org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID=”Org1MSP”
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/
org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

Environment variables for PEER1
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051
CORE_PEER_LOCALMSPID=”Org2MSP” CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/
hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
Alt text

peer channel join -b mychannel.block
Alt text

//执行更新锚节点配置命令
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx
–tls –cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/
example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Alt text
如果需要加入其它组织,变更脚本变量后执行更新锚节点操作即可,至此,手动配置网络完成, 可以编写并测试链码。

//切换至:pee0.org1.example.com
CORE_PEER_LOCALMSPID=”Org1MSP”
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabri c/peer/crypto/
peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fa bric/peer/crypto/
peerOrganizations/org1.example.com/peers/peer1.org1.exa mple.com/tls/ca.crt

//查看环境变量
echo $CORE_PEER_LOCALMSPID #确认当前指定的org
echo $CORE_PEER_ADDRESS #确认当前指定的peer
export CHANNEL_NAME=mychannel #设置通道名称
Alt text

//把本地写好的链码复制到docker cli容器中
docker cp 本地路径 容器长ID:容器路径

//安装链码
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/store/
Alt text

//实例化链码
peer chaincode instantiate -o orderer.example.com:7050 \
–tls –cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/
example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
-C $CHANNEL_NAME \
-n mycc -v 1.0 \
-c ‘{“Args”:[“str”,”helloworld”]}’ \
-P “OR(‘Org1MSP.peer’,’Org2MSP.peer’)”
Alt text

//测试链码get操作
peer chaincode query -C $CHANNEL_NAME -n mycc -c ‘{“Args”:[“get”,”str”]}’
Alt text

//测试链码set操作
peer chaincode invoke -o orderer.example.com:7050 \
–tls –cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/
example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
-C $CHANNEL_NAME -n mycc \
-c ‘{“Args”:[“set”,”str”,”hi zhaoyao”]}’
Alt text

部署过程中遇到的错误及改正措施:
创建channel时出错,Orderer log如下
Alt text
解决办法:清空所有docker容器,清空所有的环境从新开始部署。

遇到什么问题可以给我留言,或者发邮件给我,我会及时给大家回复~

小主