Hyperledger Fabric v2.1:在网关连接选项中将 asLocalhost 设置为 false 的 Fabcar

Hyperledger Fabric v2.1: Fabcar with asLocalhost set to false in Gateway connection options

我正在尝试获取 Hyperledger Fabric 运行ning 的多机网络。我遇到了一些错误。通过更改 fabric-samples/fabcar/javascript/query.js 中的一行,我能够在 Fabric v2.1 的 Fabcar example 中的一台机器上重现相同的错误。我换了行

await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });

到线

await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: false } });

所以我将 discovery.asLocalhost 设置为 false 而不是 true。当我运行 node query.jsfabric-samples/fabcar/javascript/目录下。我收到以下错误。

Wallet path: /home/userName/my/code/fabric-samples/fabcar/javascript/wallet
2020-10-23T06:09:56.505Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Committer- name: orderer.example.com:7050, url:grpcs://orderer.example.com:7050
2020-10-23T06:09:56.507Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server orderer.example.com:7050 url:grpcs://orderer.example.com:7050 timeout:3000
2020-10-23T06:09:56.508Z - error: [DiscoveryService]: _buildOrderer[mychannel] - Unable to connect to the discovered orderer orderer.example.com:7050 due to Error: Failed to connect before the deadline on Committer- name: orderer.example.com:7050, url:grpcs://orderer.example.com:7050
2020-10-23T06:09:59.522Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com:7051, url:grpcs://peer0.org1.example.com:7051
2020-10-23T06:09:59.523Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org1.example.com:7051 url:grpcs://peer0.org1.example.com:7051 timeout:3000
2020-10-23T06:09:59.523Z - error: [DiscoveryService]: _buildPeer[mychannel] - Unable to connect to the discovered peer peer0.org1.example.com:7051 due to Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com:7051, url:grpcs://peer0.org1.example.com:7051
2020-10-23T06:10:02.528Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Endorser- name: peer0.org2.example.com:9051, url:grpcs://peer0.org2.example.com:9051
2020-10-23T06:10:02.528Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org2.example.com:9051 url:grpcs://peer0.org2.example.com:9051 timeout:3000
2020-10-23T06:10:02.529Z - error: [DiscoveryService]: _buildPeer[mychannel] - Unable to connect to the discovered peer peer0.org2.example.com:9051 due to Error: Failed to connect before the deadline on Endorser- name: peer0.org2.example.com:9051, url:grpcs://peer0.org2.example.com:9051
2020-10-23T06:10:02.564Z - error: [SingleQueryHandler]: evaluate: message=Query failed. Errors: [], stack=FabricError: Query failed. Errors: []
    at SingleQueryHandler.evaluate (/home/sarva/my/code/viacomrepos/temp-fabric-samples/fabric-samples/fabcar/javascript/node_modules/fabric-network/lib/impl/query/singlequeryhandler.js:45:23)
    at Transaction.evaluate (/home/sarva/my/code/viacomrepos/temp-fabric-samples/fabric-samples/fabcar/javascript/node_modules/fabric-network/lib/transaction.js:287:49)
    at Contract.evaluateTransaction (/home/sarva/my/code/viacomrepos/temp-fabric-samples/fabric-samples/fabcar/javascript/node_modules/fabric-network/lib/contract.js:115:45)
    at main (/home/sarva/my/code/viacomrepos/temp-fabric-samples/fabric-samples/fabcar/javascript/query.js:46:39)
    at processTicksAndRejections (internal/process/task_queues.js:85:5), name=FabricError
Failed to evaluate transaction: FabricError: Query failed. Errors: []

我按照 Fabcar tutorial 中的说明进行操作。以下是重现错误的步骤。

cd fabric-samples/fabcar
./startFabric.sh javascript
cd javascript
npm install
node enrollAdmin.js
node registerUser.js
[Change the line in query.js]
node query.js

如果我能弄清楚 how/where 来指定 grpcs URL,我想我可以让我的多机网络正常工作。感谢任何帮助。

根据的建议设置export GRPC_TRACE=allexport GRPC_VERBOSITY=DEBUG可以获得更详细的错误日志。

在单机情况下,您的客户端应用程序在本地计算机上 运行,但节点(对等节点和排序节点)在本地计算机上的 Docker 容器中 运行,并且有像 peer0.org1.example.com.

这样的主机名

在 Docker 网络中,节点可以使用它们的主机名相互通信,例如peer0.org1.example.com。您的客户端(在 Docker 网络之外)无法与 peer0.org1.example.com 通信,因为该 DNS 名称不存在。相反,它需要连接到 Docker 网络中 Docker 特定 hosts/ports 映射端口上的本地主机。

您的客户端应用程序通过两种方式获取节点端点:

  1. 其本地连接配置文件中的条目。
  2. 服务发现返回的网络拓扑。

您可以在连接配置文件中设置适当的 (localhost) 端点 URL。但是,发现返回的端点 URL 将是 Docker 网络中公开的那些,例如peer0.org1.example.com。为了促进这种情况,SDK 提供了 discovery.asLocalhost 设置,启用后,会将发现返回的所有端点地址映射到本地主机的同一端口。另一种方法是将条目添加到本地主机文件,将节点名称映射到本地主机。

如果您的节点可以使用其配置的主机名在真实网络上访问,则不应启用 discovery.asLocalhost 设置。这些主机名必须在 DNS 中可解析(因此对于真正的部署,您不能使用 non-resolvable 地址,如 example.com),并且必须匹配您的 Fabric 网络配置(如果使用 TLS,则服务器证书详细信息)。

用于设置多机网络。您必须对 config.json 和 connection.yaml(基本上是连接配置文件)进行更改。

await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: false } });

本地主机 属性 应设置为“false”,因为您要设置 multi-host 区块链网络。您可以创建一个覆盖网络来连接它。

    ---
  name: mychannel.firstnetwork.connectionprofile
  x-type: "hlfv1"
  description: "connection profile for 2.1  network"
  version: "1.0"
  
  channels:
    mychannel:
      orderers:
        - orderer.example.com
      peers:
        peer0.org1.example.com:
          endorsingPeer: true
          chaincodeQuery: true
          ledgerQuery: true
          eventSource: true
        peer0.org2.example.com:
          endorsingPeer: true
          chaincodeQuery: true
          ledgerQuery: true
          eventSource: true
  
  organizations:
    Org1:
      mspid: Org1MSP
      peers:
        - peer0.org1.example.com
      certificateAuthorities:
        - ca.org1.example.com
      adminPrivateKey:
        path:  ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/8717c2e82c3c4caff76fca964bb70_sk
      signedCert:
        path:  ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/cert.pem
    
    Org2:
      mspid: Org2MSP
      peers:
        - peer0.org2.example.com
      certificateAuthorities:
        - ca.org2.example.com
      adminPrivateKey:
        path:  ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/keystore/9592495c719c2e87fc4a8_sk
      signedCert:
        path:  ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/signcerts/cert.pem
  orderers:
    orderer.example.com:
      url: grpcs://orderer.example.com:7050
      grpcOptions:
       ssl-target-name-override: orderer.example.com
      tlsCACerts:
        path:  ./caro-blockchain-commonfiles/config/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
  
  peers:
    peer0.org1.example.com:
      url: grpcs://peer0.org1.example.com:7051
      grpcOptions:
        ssl-target-name-override: peer0.org1.example.com
      tlsCACerts:
        path: ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/cacerts/localhost-7054-ca-org1.pem
  
   
    peer0.org2.example.com:
      url: grpcs://peer0.org2.example.com:9051
      grpcOptions:
        ssl-target-name-override: peer0.org2.example.com
      tlsCACerts:
        path: ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/cacerts/localhost-8054-ca-org2.pem
  
   
  
  certificateAuthorities:
    ca.org1.example.com:
      url: https://ca.org1.example.com:7054
      httpOptions:
        verify: false
      tlsCACerts:
        path: ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem
      registrar:
        - enrollId: admin
          enrollSecret: adminpw
      caName: ca-org1
    ca.org2.example.com:
      url: https://ca.org2.example.com:8054
      httpOptions:
        verify: false
      tlsCACerts:
        path: ./caro-blockchain-commonfiles/config/crypto-config/peerOrganizations/org2.example.com/ca/ca.org2.example.com-cert.pem
      registrar:
        - enrollId: admin
          enrollSecret: adminpw
      caName: ca-org2

此处的 URL 属性 如 grpcs://peer0.org1.example.com:7051 应从主机名更改为另一台计算机的 IP 到 VM ( grpcs://XX.XX.XX.XX:7051),其中对等容器为 运行。