介绍:该例程已经配置好docker,通过GoLand得docker工具启动容器后,通过CLI安装、实例化和调用链码。
1.克隆fabric-samples
git clone https://github.com/hyperledger/fabric-samples.git
2.使用GoLand打开chaincode-docker-devmode的docker-compose-simple.yaml
version: '2'
services:
orderer:
container_name: orderer
image: hyperledger/fabric-orderer
environment:
- FABRIC_LOGGING_SPEC=debug
- ORDERER_GENERAL_LISTENADDRESS=orderer
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=orderer.block
- ORDERER_GENERAL_LOCALMSPID=DEFAULT
- ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/msp
- GRPC_TRACE=all=true,
- GRPC_VERBOSITY=debug
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ./msp:/etc/hyperledger/msp
- ./orderer.block:/etc/hyperledger/fabric/orderer.block
ports:
- 7052:7052
peer:
container_name: peer
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer
- CORE_PEER_ADDRESS=peer:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer:7051
- CORE_PEER_LOCALMSPID=DEFAULT
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_VM_ENDPOINT=unix:/
- FABRIC_LOGGING_SPEC=DEBUG
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
volumes:
- /var/run/:/host/var/run/
- ./msp:/etc/hyperledger/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start --peer-chaincodedev=true
ports:
- 7051:7051
- 7053:7053
depends_on:
- orderer
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:/
- FABRIC_LOGGING_SPEC=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer:7051
- CORE_PEER_LOCALMSPID=DEFAULT
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
working_dir: /opt/gopath/src/chaincodedev
command: /bin/bash -c './script.sh'
volumes:
- /var/run/:/host/var/run/
- ./msp:/etc/hyperledger/msp
- ./../chaincode:/opt/gopath/src/chaincodedev/chaincode
- ./:/opt/gopath/src/chaincodedev/
depends_on:
- orderer
- peer
##如果chaincode容器可以顺利开启,则配置正确,然后用用goland得本地运行代替chaincode容器
chaincode:
container_name: chaincode
image: hyperledger/fabric-ccenv
tty: true
privileged: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:/
- FABRIC_LOGGING_SPEC=DEBUG
- CORE_PEER_ID=chaincode
- CORE_CHAINCODE_ID_NAME=mycc:1
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_ADDRESS=peer:7051
- CORE_PEER_LOCALMSPID=DEFAULT
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
working_dir: /opt/gopath/src/chaincode
command: /bin/sh -c 'sleep 6000000'
volumes:
- /var/run/:/host/var/run/
- ./msp:/etc/hyperledger/msp
- ../chaincode:/opt/gopath/src/chaincode
depends_on:
- orderer
- peer
3.启动Docker
4.进入CLI终端
##安装链码
peer chaincode install -p chaincode/sacc/ -n mycc -v 1
##如果安装失败就配置代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
##实例化
peer chaincode instantiate -n mycc -v 1 -c '{"Args":["a","10"]}' -C myc
成功后生成链码的运行容器
5.配置链码Debug配置&&运行
以下地址peer容器的ip(进入终端 命令ifconfig查看)
#CORE_CHAINCODE_ID_NAME对应刚刚安装和实例化得链码名称:版本号
#Environment
CORE_PEER_ADDRESS =17*.18.0.*:7051;CORE_CHAINCODE_ID_NAME=mycc:0;CORE_PEER_TLS_ENABLED=false
#Program arguments
-peer.address 17*.18.0.*:7052
6.设置断点&CLI调用链码
peer chaincode invoke -n mycc -c '{"Args":["set", "a", "30"]}' -C myc
触发断点
如果不行得话先把链码容器关掉
参考:
HyperLedger Fabric chaincode 开发模式 docker-devmode
【fabric源码】Goland/Vscode调试Hyperledger Fabric 2.0源码、单机网络(傻瓜式调试)【附源码】_wx5ed8a22dd1db1_51CTO博客
|