2017-07-31 159 views
1

我有我的chaincode,4个同伴和一个订购者准备就绪。我可以通过CLI查询链式代码,但是如何使用API​​查询它,以及如何将其部署为web应用程序。有人可以评论吗?如何部署Hyperledger Fabric应用程序

回答

1

以下是转到tutorial的文档,其中显示了如何通过Node.js SDK编写查询Fabric分类帐的应用程序。

3

提供了几个SDK,因此您可以在应用程序中使用它们。

  1. Node SDK
  2. Java SDK
  3. Go SDK
  4. Python SDK

你可以选择其中一个到你的方便,比如你在这里,你可以如何使用Java SDK的一个简短的例子查询链码:

// Get an instance of Hyperledger Fabric client 
    final HFClient client = HFClient.createNewInstance(); 

    // Set default crypto suite for HF client 
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite()); 

现在您需要设置用户内容以提供标识组织中用户的用户名和加密材料。

// Set user context 
    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; 
     } 

     // Enrollment is an interface to retrieve certificate and private key of the user in context 
     public Enrollment getEnrollment() { 
      return new Enrollment() { 
       public PrivateKey getKey() { 
         return privateKey; 
       } 

       public String getCert() { 
        return certificate; 
       } 
      }; 
     } 

     public String getMspId() { 
      return "Org1MSP"; 
     } 
    }); 

接下来需要在上下文中创建通道实例。

// Create new channel 
    final Channel channel = client.newChannel("mychannel"); 

    // Setup ordering service 
    channel.addOrderer(client.newOrderer("orderer0", "grpc://localhost:7050")); 
    // COnfigure endorsing peers 
    channel.addPeer(client.newPeer("peer0", "grpc://localhost:7051")); 

    // Finally initialize the channel 
    channel.initialize(); 

最后我们准备发送交易提案。

// Create transaction request 
    final TransactionProposalRequest proposalRequest = client.newTransactionProposalRequest(); 

    final ChaincodeID chaincodeID = ChaincodeID.newBuilder() 
      .setName("myCC") 
      .setVersion("1.0") 
      .setPath("github.com/some_package/package/chaincode/myCC") 
      .build(); 

    // chaincode name 
    proposalRequest.setChaincodeID(chaincodeID); 
    // chaincode function to execute 
    proposalRequest.setFcn("some_function"); 
    // timeout 
    proposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(10)); 
    // arguments for chaincode function 
    proposalRequest.setArgs(// Set arguments based on CC); 

    // Sending transaction proposal 
    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()); 

与其他SDK代码看起来非常相似,在这里你可以采取类似Go SDK example看看。

+0

我会补充说,还有一个Python SDK。 https://github.com/hyperledger/fabric-sdk-py – christo4ferris

+0

当然,谢谢提醒。增加:) –

+0

@ArtemBarger你可以分享有关Fabric-Java-SDK的文档的任何链接换句话说,我建议如何开始编写Hyperledger-Fabric的Java代码 –