![](/img/trans.png)
[英]Issue while using fabric client java sdk (since there is no Rest API support) in local development environment for Hyperledher Fabric V1.0
[英]Use REST API support in local development environment for Hyperledger Fabric V1.0
我已按照“ 构建第一个网络”的步骤, 建立了一个由4个组织, 每个 组织 有1个对等方的HyperLedger Fabric V1.0网络 。
我现在有
- org1.example.com-与peer : peer0.org1.example.com和msp : Org1MSP
- org2.example.com-与peer : peer0.org2.example.com和msp : Org2MSP
- org3.example.com-与peer : peer0.org3.example.com和msp : Org3MSP
- org4.example.com-与peer : peer0.org4.example.com和msp : Org4MSP
现在,我可以将链码安装到对等端,并在通道上实例化链码。 我还可以通过使用此处提到的命令来调用和查询链式代码
调用 :
对等链代码调用-o orderer.example.com:7050 --tls $ CORE_PEER_TLS_ENABLED --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”:[“ invoke”,“ a”,“ b”,“ 10”]}'查询 :
对等链代码查询-C $ CHANNEL_NAME -n mycc -c'{“ Args”:[“ query”,“ a”]}'
我以前使用的是IBM Bluemix提供的Hyperledger Fabric V0.6服务,而我的Java应用程序则通过Rest API调用链代码。
如何使用docker image在此本地网络设置中在此处使用Rest API ,然后我的Java应用程序可以与我的链码进行交互。
由于我不太熟悉此本地网络设置,因此请建议我如何使它工作。
注意:
我正在使用Windows 7机器,并通过在Docker快速启动终端中运行命令来设置网络
提前致谢..
Hyperledger Fabric v.1.0.0中没有REST API,但是有Java SDK可用于与对等方进行交互。 您可以使用以下Maven依赖项来设置Java项目:
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>1.0.0</version>
</dependency>
现在,您可以使用SDK API来调用/查询您的链码:
获取HF客户端实例
final HFClient client = HFClient.createNewInstance();
为客户设置加密资料
// Set default crypto suite for HF client
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(new User() {
public String getName() {
return "testUser";
}
public Set<String> getRoles() {
return null;
}
public String getAccount() {
return null;
}
public String getAffiliation() {
return null;
}
public Enrollment getEnrollment() {
return new Enrollment() {
public PrivateKey getKey() {
// Load your private key
}
public String getCert() {
// Read client certificate
}
};
}
public String getMspId() {
return "Org1MSP";
}
});
现在通道配置:
final Channel channel = client.newChannel("mychannel");
channel.addOrderer(client.newOrderer("orderer0", "grpc://localhost:7050"));
channel.addPeer(client.newPeer("peer0", "grpc://localhost:7051"));
channel.initialize();
创建交易建议:
final TransactionProposalRequest proposalRequest = client.newTransactionProposalRequest();
final ChaincodeID chaincodeID = ChaincodeID.newBuilder()
.setName("myCC")
.setVersion("1.0")
.setPath("github.com/yourpackage/chaincode/")
.build();
proposalRequest.setChaincodeID(chaincodeID);
proposalRequest.setFcn("fcn");
proposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(10));
proposalRequest.setArgs(new String[]{"arg1", "arg2"});
发送提案
final Collection<ProposalResponse> responses = channel.sendTransactionProposal(proposalRequest);
CompletableFuture<BlockEvent.TransactionEvent> txFuture = channel.sendTransaction(responses, client.getUserContext());
BlockEvent.TransactionEvent event = txFuture.get();
System.out.println(event.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.