简体   繁体   English

在Hyperledger Fabric V1.0的本地开发环境中使用REST API支持

[英]Use REST API support in local development environment for Hyperledger Fabric V1.0

I have setup a HyperLedger Fabric V1.0 network with 4 organisations each having 1 peer by following the steps Building Your First Network . 我已按照“ 构建第一个网络”的步骤, 建立了一个4个组织, 每个 组织 有1个对等方HyperLedger Fabric V1.0网络

Now I have 我现在有

  1. org1.example.com - with peer : peer0.org1.example.com and msp : Org1MSP org1.example.com-peerpeer0.org1.example.commspOrg1MSP
  2. org2.example.com - with peer : peer0.org2.example.com and msp : Org2MSP org2.example.com-peerpeer0.org2.example.commspOrg2MSP
  3. org3.example.com - with peer : peer0.org3.example.com and msp : Org3MSP org3.example.com-peerpeer0.org3.example.commspOrg3MSP
  4. org4.example.com - with peer : peer0.org4.example.com and msp : Org4MSP org4.example.com-peerpeer0.org4.example.commspOrg4MSP

And now I can install the chaincode to peers and instantiate the chaincode on the channel. 现在,我可以将链码安装到对等端,并在通道上实例化链码。 I can also able to invoke and query chain code by using the commands mentioned here like 我还可以通过使用此处提到的命令来调用和查询链式代码

Invoke : 调用
peer chaincode invoke -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"]}' 对等链代码调用-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”]}'

Query : 查询
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' 对等链代码查询-C $ CHANNEL_NAME -n mycc -c'{“ Args”:[“ query”,“ a”]}'

I was previously using Hyperledger Fabric V0.6 service provided by IBM Bluemix and my java applications were invoking the chain code through the Rest API . 我以前使用的是IBM Bluemix提供的Hyperledger Fabric V0.6服务,而我的Java应用程序则通过Rest API调用链代码。

How can I use the Rest API here in this local network setup using docker image ?, then my java applications can interact with my chaincode. 如何使用docker image在此本地网络设置中在此处使用Rest API ,然后我的Java应用程序可以与我的链码进行交互。
Since I am not so familiar with this local network setup, please suggest me how can I make it work. 由于我不太熟悉此本地网络设置,因此请建议我如何使它工作。

Note: 注意:
I am using Windows 7 machin e and network is setup by running the commands in docker quick start terminal 我正在使用Windows 7机器,并通过在Docker快速启动终端中运行命令来设置网络

Thanks in advance.. 提前致谢..

There is no REST API in Hyperledger Fabric v.1.0.0, however there is Java SDK which could be used to interact with peers. Hyperledger Fabric v.1.0.0中没有REST API,但是有Java SDK可用于与对等方进行交互。 You can setup your java project with following maven dependencies: 您可以使用以下Maven依赖项来设置Java项目:

<dependency>
  <groupId>org.hyperledger.fabric-sdk-java</groupId>
  <artifactId>fabric-sdk-java</artifactId>
  <version>1.0.0</version>
</dependency>

Now you can use SDK APIs to invoke/query your chaincodes: 现在,您可以使用SDK API来调用/查询您的链码:

Get instance of HF client 获取HF客户端实例

    final HFClient client = HFClient.createNewInstance();

Setup crypto materials for client 为客户设置加密资料

    // 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";
        }
    });

Now channel configuration: 现在通道配置:

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

    channel.addOrderer(client.newOrderer("orderer0", "grpc://localhost:7050"));
    channel.addPeer(client.newPeer("peer0", "grpc://localhost:7051"));

    channel.initialize();

Create transaction proposal: 创建交易建议:

    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"});

Send 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM