前言
默认的state DB为goleveldb,功能有限,现在把它该归CouchDB.它提供了丰富的查询功能
拉取coundb image
docker pull hyperledger/fabric-couchdb:x86_64-1.0.5
docker tag hyperledger/fabric-couchdb:x86_64-1.0.5 hyperledger/fabric-couchdb
使用couchDB启动docker-compose
CHANNEL_NAME=mumubin TIMEOUT=1000 docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml up
利用上节教程使所有peers添加至channel
chaincode改用marbles02
安装chaincode(请把环境变量都改为peer0.org1.example.com的)
peer chaincode install -o orderer.example.com:7050 -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02
实例化marbles02
peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -v 1.0 -c '{"Args":["init"]}' -P "OR ('Org0MSP.member','Org1MSP.member')"
Invoke 创建一些marble并交易
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble1","blue","35","tom"]}'
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble2","red","50","tom"]}'
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble3","blue","70","tom"]}'
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -c '{"Args":["transferMarble","marble2","jerry"]}'
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -c '{"Args":["transferMarblesBasedOnColor","blue","jerry"]}'
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n marbles -c '{"Args":["delete","marble1"]}'
CouchDB可以通过浏览器访问
ssh映射端口
ssh -L5984:127.0.0.1:5984 root@10.0.0.157
打开浏览器查询CouchDB
通过query接口程序
peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["readMarble","marble2"]}'
Query Result: {"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50}
peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}'
Query Result: [{"TxId":"823617c460250583550d5998d1919653181b4044bea2219947da7d50795c5c8e", "Value":{"docType":"marble","name":"marble1","color":"blue","size":35,"owner":"tom"}, "Timestamp":"2018-01-18 05:50:16.228066078 +0000 UTC", "IsDelete":"false"},{"TxId":"b734917a08f8211bdd554a210c0e1ae8f02067584e3dadd9a96c0bec05b831bf", "Value":{"docType":"marble","name":"marble1","color":"blue","size":35,"owner":"jerry"}, "Timestamp":"2018-01-18 05:50:43.550703802 +0000 UTC", "IsDelete":"false"},{"TxId":"78d8a148aa07d913a45372a9d6ae4ef5ad0369364bc88cc3b855940219028dfd", "Value":null, "Timestamp":"2018-01-18 05:50:48.935805386 +0000 UTC", "IsDelete":"true"}]
2018-01-18 05:52:59.188 UTC [main] main -> INFO 007 Exiting.....
peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["queryMarblesByOwner","jerry"]}'
Query Result: [{"Key":"marble2", "Record":{"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50}},{"Key":"marble3", "Record":{"color":"blue","docType":"marble","name":"marble3","owner":"jerry","size":70}}]
注意
如果数据持久化想在peer 容器或者CouchDB容器进行,需要挂载一个目录至docker.
- 挂载至peer container,修改docker-compose-base.yaml
volumes:
- /var/hyperledger/couchdb0:/opt/couchdb/data
- 挂载至CouchDB container,修改docker-compose-couch.yaml
volumes:
- /var/hyperledger/peer0:/var/hyperledger/production
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。