从Java SDK连接到Hyperledger Fabric网络失败

时间:2019-12-02 10:05:07

标签: java yaml hyperledger-fabric

我目前正在基于不同物理机器上的两个节点开发HF网络,每个节点代表一个组织。我可以从CLI执行链代码的所有命令,但我想连接到通道并从组织的应用程序中调用事务。调用gateway.getNetwork("mychannel");时出现问题: Channel by the name mychannel already exists

当然,由于网络已经建立并且正在运行,因此该通道已经存在(我通过CLI管理通道创建)。但是,我没有在这里实例化任何通道,我只是在尝试连接到现有通道...或者我做错了什么? 在该应用程序的Java代码摘录下面,是我得到的错误输出以及网关的connection.yaml文件的一部分。

Java代码:

...
Path networkConfigPath = Paths.get("/home/rfiorini/work/src/github.com/hyperledger/fabric-samples/fabric-multi-network/connection.yaml");

Gateway.Builder builder = Gateway.createBuilder();
builder.identity(wallet, "Admin@org1.example.com").networkConfig(networkConfigPath).discovery(true);

// create a gateway connection
try (Gateway gateway = builder.connect()) {

// get the network and chaincode
Network network = gateway.getNetwork("mychannel");
...

错误输出:

org.hyperledger.fabric.gateway.GatewayRuntimeException: org.hyperledger.fabric.sdk.exception.InvalidArgumentException: Channel by the name mychannel already exists
        at org.hyperledger.fabric.gateway.impl.GatewayImpl.getNetwork(GatewayImpl.java:278)
        at org.example.ClientApp.main(ClientApp.java:33)
        at org.example.ClientTest.testApp(ClientTest.java:12)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.hyperledger.fabric.sdk.exception.InvalidArgumentException: Channel by the name mychannel already exists
        at org.hyperledger.fabric.sdk.HFClient.newChannel(HFClient.java:181)
        at org.hyperledger.fabric.gateway.impl.GatewayImpl.getNetwork(GatewayImpl.java:270)
        ... 31 more

connection.yaml:

client:
    organization: Org1
    connection:
        timeout:
            peer:
                endorser: '300'
            orderer: '300'

channels:
  mychannel:
    orderers:
      - orderer0.example.com

    peers:
      peer0.org1.example.com:
      peer1.org1.example.com:
      peer0.org2.example.com:
      peer1.org2.example.com:

organizations:
  Org1:
    mspid: Org1MSP

    peers:
      - peer0.org1.example.com
      - peer1.org1.example.com

  Org2:
    mspid: Org2MSP

    peers:
      - peer0.org2.example.com
      - peer1.org2.example.com

orderers:
  orderer0.example.com:
    url: grpc://localhost:7050

    grpcOptions:
      ssl-target-name-override: orderer.example.com

peers:
  peer0.org1.example.com:
    url: grpc://localhost:7051

    grpcOptions:
      ssl-target-name-override: peer0.org1.example.com
      request-timeout: 120001

  peer1.org1.example.com:
    url: grpc://localhost:8051

    grpcOptions:
      ssl-target-name-override: peer1.org1.example.com
      request-timeout: 120001

  peer1.org2.example.com:
    url: grpc://10.5.16.104:8051

    grpcOptions:
      ssl-target-name-override: peer1.org2.example.com
      request-timeout: 120001

  peer0.org2.example.com:
    url: grpc://10.5.16.104:7051

    grpcOptions:
      ssl-target-name-override: peer0.org2.example.com
      request-timeout: 120001

1 个答案:

答案 0 :(得分:0)

我们在这里有相同的问题,解决方法:

  • 删除您的connection.yaml中的频道条目
  • 重新启动客户端应用程序
  • 完成。

希望如此。