我正在使用 Fabric-sample 中的 ./byfn.sh 脚本来启动我的区 block 链。 我想从区 block 链中调用和查询 PHP 网站的数据。
我确实知道我只能使用来自 CLI 容器的 API 调用或使用 JavaSDK/NodeSDK、RESTapi。
如何以最简单的方式从区 block 链查询数据? 我正在查看 NodeSDK 和 RestApi,但没有看到太多可以帮助我的指南,因此出现了这个问题。 抱歉新手问题。
谢谢!
最佳答案
调用 Hyperledger Fabric 对等点并与之交互的正确方法是通过 SDK,以下是可用 SDK 的列表:
- Java SDK
- GoLang SDK
- NodeJS SDK
- Python SDK
更新
根据@christo4ferris的建议,还有一个额外的项目:
(这可能是您感兴趣的)
例如,您可以使用 golang SDK 并执行以下操作:
读取配置文件
var err 错误 conf, err := config.InitConfig("config.yaml") 如果错误!= nil { fmt.Println(错误) 返回 }
初始化结构客户端
cl := Fabricclient.NewClient(conf) bccspFactory.InitFactories(conf.CSPConfig()) cl.SetCryptoSuite(bccspFactory.GetDefault())
加载客户端证书和 key
privKey := filepath.Join(conf.CryptoConfigPath(), " key 路径") pubKey := filepath.Join(conf.CryptoConfigPath(), "证书路径")
读取并设置 MSP ID
mspID, err := conf.MspID("org1") 如果错误!= nil { fmt.Println(错误) 返回 }
将客户端用户设置到上下文中
user, err := fabapi.NewPreEnrolledUser(conf, privKey, pubKey, "user1", mspID, cl.GetCryptoSuite()) 如果错误!= nil { fmt.Println(错误) 返回 } cl.SetUserContext(用户)
设置排序节点
ordererConf, err := conf.OrdererConfig("orderer0") 如果错误!= nil { fmt.Println(错误) 返回 }
o, err := orderer.NewOrderer(fmt.Sprintf("%s:%d", ordererConf.Host, ordererConf.Port), filepath.Join(conf.CryptoConfigPath(), "订购者证书的路径"), “orderer.example.com”,conf) 如果错误!= nil { fmt.Println(错误) 返回 }
设置并初始化 channel 和背书节点
peers, err := conf.PeersConfig("org1") 如果错误!= nil { fmt.Println(错误) 返回 }
p, err := peer.NewPeer(fmt.Sprintf("%s:%d", Peers[0].Host, Peers[0].Port), conf) 如果错误!= nil { fmt.Println(错误) }
ch, err := cl.NewChannel("mychannel") 如果错误!= nil { fmt.Println(错误) 返回 }
ch.AddOrderer(o) ch.AddPeer(p) ch.SetPrimaryPeer(p) cl.SaveUserToStateStore(user, true)
准备交易提案请求,发送并提交订购
txRequest := apitxn.ChaincodeInvokeRequest{ 目标:[]apitxn.ProposalProcessor{p}, FCN:“myFCN”, 参数:[]string{"myargs"}, TransientMap: 映射[字符串][]字节{}, ChaincodeID:“helloworld”, }
proposalResponse, _, err := ch.SendTransactionProposal(txRequest) 如果错误!= nil { fmt.Println(错误) 返回 }
fmt.Printf("%v\n",proposalResponse[0].ProposalResponse)
tx, err := ch.CreateTransaction(proposalResponse) 如果错误!= nil { fmt.Println(错误) 返回 }
txResponse, err := ch.SendTransaction(tx) 如果错误!= nil { fmt.Println(错误) 返回 }
fmt.Println(txResponse[0])
这在所有 SDK 中都适用。
关于hyperledger-fabric - 如何在 CLI 容器之外查询 Hyperledger Fabric 区 block 链数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46072704/