2017-07-31 111 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代碼 –