在开始之前需要导出一个自定义变量,方便后续操作:
export FABRIC=/opt/gopath/src/github.com/hyperledger/fabric/devenv
1.在真机上执行如下操作,进入虚拟机
cd $FABRIC vagrant up//启动虚拟机 vagrant ssh//进入虚拟机
2.在虚拟机中执行如下操作,启动memberserv和peer,并保持该终端的启动,命名为T1
cd /opt/gopath/src/github.com/hyperledger/fabric/yaml docker-compose -f docker-compose-dev.yaml up
附docker-compose-dev.yaml
membersrvc: image: hyperledger/fabric-membersrvc ports: - "7054:7054" command: membersrvc vp0: image: hyperledger/fabric-peer ports: - "7050:7050" - "7051:7051" - "7053:7053" environment: - CORE_PEER_ADDRESSAUTODETECT=true - CORE_VM_ENDPOINT=unix:///var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_ID=vp0 - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054 - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054 - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054 - CORE_SECURITY_ENABLED=false - CORE_SECURITY_ENROLLID=test_vp0 - CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT links: - membersrvc command: sh -c "sleep 5; peer node start --peer-chaincodedev"
3.新开一个终端,用于启动chaincode程序,保持该终端的开启,命名为T2
cd $FABRIC vagrant ssh//进入虚拟机 cd /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02//进入要部署的chaincode所在目录 go build//编译代码,生成chaincode_example02可执行程序 CORE_CHAINCODE_ID_NAME=mycc01 CORE_PEER_ADDRESS=172.17.0.3:7051 ./chaincode_example02//注册chaincode,并为其注册一个名称CORE_CHAINCODE_ID_NAME
返回信息如下:
02:06:56.192 [shim] DEBU : Peer address: 172.17.0.3:7051
02:06:56.197 [shim] DEBU : os.Args returns: [./chaincode_example02]
02:06:56.200 [shim] DEBU : Registering.. sending REGISTER
02:06:56.228 [shim] DEBU : []Received message REGISTERED from shim
02:06:56.229 [shim] DEBU : []Handling ChaincodeMessage of type: REGISTERED(state:created)
02:06:56.229 [shim] DEBU : Received REGISTERED, ready for invocations
4.新开一个终端,用于部署chaincode程序,命名为T3
cd $FABRIC vagrant ssh//进入虚拟机 CORE_PEER_ADDRESS=172.17.0.3:7051 peer chaincode deploy -n mycc01 -c ‘{ "Args":["init"]}‘//部署chaincode
在T2终端打印:
02:07:10.906 [shim] DEBU : [mycc01]Received message INIT from shim
02:07:10.906 [shim] DEBU : [mycc01]Handling ChaincodeMessage of type: INIT(state:established)
02:07:10.907 [shim] DEBU : Entered state init
02:07:10.907 [shim] DEBU : [mycc01]Received INIT, initializing chaincode
02:07:10.908 [shim] DEBU : [mycc01]Init succeeded. Sending COMPLETED
02:07:10.909 [shim] DEBU : [mycc01]Move state message COMPLETED
02:07:10.909 [shim] DEBU : [mycc01]Handling ChaincodeMessage of type: COMPLETED(state:init)
02:07:10.909 [shim] DEBU : [mycc01]send state message COMPLETED
在T3终端打印:
Deploy chaincode: mycc01
这里返回chaincode id,生产模式下打印的是chaincode对应的hash值
之后就可以进行相应的invoke或者query操作了