【问题标题】:How to make connection between Fabric Node SDK and Fabric network, when deployed on different hosts?当部署在不同的主机上时,如何在 Fabric Node SDK 和 Fabric 网络之间建立连接?
【发布时间】:2021-07-23 20:50:43
【问题描述】:

我正在测试Running a Fabric Application 教程。我在一台主机上部署了网络(比如说:HOST1)。 然后,在另一台具有不同 IP 地址(HOST2)的主机上,我想运行应用程序(如教程中所示)。 为此,我复制了在运行 ./network.sh deployCC 脚本时生成的连接配置文件,并将对等端的 url 从 localhost 更改为 HOST1 的 IP 地址。因此,HOST2 上的连接配置文件如下:

{
"name": "test-network-org1",
"version": "1.0.0",
"client": {
    "organization": "Org1",
    "connection": {
        "timeout": {
            "peer": {
                "endorser": "300"
            }
        }
    }
},
"organizations": {
    "Org1": {
        "mspid": "Org1MSP",
        "peers": [
            "peer0.org1.example.com"
        ],
        "certificateAuthorities": [
            "ca.org1.example.com"
        ]
    }
},
"peers": {
    "peer0.org1.example.com": {
        "url": "grpcs://<HOST1IP>:7051",
        "tlsCACerts": {
            "pem": "-----BEGIN CERTIFICATE-----\nCERTBODY\n-----END CERTIFICATE-----\n"
        },
        "grpcOptions": {
            "ssl-target-name-override": "peer0.org1.example.com",
            "hostnameOverride": "peer0.org1.example.com"
        }
    }
},
"certificateAuthorities": {
    "ca.org1.example.com": {
        "url": "https://<HOST1IP>:7054",
        "caName": "ca-org1",
        "tlsCACerts": {
            "pem": ["-----BEGIN CERTIFICATE-----\nCERTBODY\n-----END CERTIFICATE-----\n"]
        },
        "httpOptions": {
            "verify": false
        }
    }
}

另外,我更改了 app.js 文件中 gateway.connect() 方法中的发现选项,以避免在本地网络中查找结构网络:

try {

    const ccp = buildCCPOrg1();
    const caClient = buildCAClient(FabricCAServices, ccp, 'ca.org1.example.com');
    const wallet = await buildWallet(Wallets, walletPath);
    await enrollAdmin(caClient, wallet, mspOrg1);
    await registerAndEnrollUser(caClient, wallet, mspOrg1, org1UserId, 'org1.department1');

    const gateway = new Gateway();

    try {

        await gateway.connect(ccp, {
            wallet,
            identity: org1UserId,
            discovery: { enabled: true, asLocalhost: false } 
        });
        //first exception here

        const network = await gateway.getNetwork(channelName);
        //second exception here, and app closes

        const contract = network.getContract(chaincodeName);

        console.log('\n--> Submit Transaction: InitLedger...');
        await contract.submitTransaction('InitLedger');
        console.log('*** Result: committed');
        
        //rest of method from original app.js was cut
    } finally {

        gateway.disconnect();
    }

除了这些更改之外,一切都保持默认。 应用程序可以连接到 CA 并注册和注册 admin 和 appUser(如教程中所示),但在连接到网络时会崩溃。

日志和错误的副本:

Loaded the network configuration located at \fabric-samples\test-network\conn\connection-org1.json
Built a CA Client named ca-org1
Built a file system wallet at \fabric-samples\asset-transfer-basic\application-javascript\wallet
Successfully enrolled admin user and imported it into the wallet
Successfully registered and enrolled user appUser and imported it into the wallet
2021-04-30T11:49:34.232Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com, url:grpcs://<HOST1IP>:7051, connected:false, connectAttempted:true
2021-04-30T11:49:34.232Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org1.example.com url:grpcs://<HOST1IP>:7051 timeout:3000
2021-04-30T11:49:34.264Z - info: [NetworkConfig]: buildPeer - Unable to connect to the endorser peer0.org1.example.com due to Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com, url:grpcs://<HOST1IP>:7051, connected:false, connectAttempted:true
    at checkState (\fabric-samples\asset-transfer-basic\application-javascript\node_modules\@grpc\grpc-js\build\src\client.js:69:26)
    at Timeout._onTimeout (\fabric-samples\asset-transfer-basic\application-javascript\node_modules\@grpc\grpc-js\build\src\channel.js:292:17)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7) {
connectFailed: true
}
2021-04-30T11:49:37.389Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Discoverer- name: peer0.org1.example.com, url:grpcs://<HOST1IP>:7051, connected:false, connectAttempted:true
2021-04-30T11:49:37.391Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org1.example.com url:grpcs://<HOST1IP>:7051 timeout:3000
2021-04-30T11:49:37.393Z - error: [ServiceEndpoint]: ServiceEndpoint grpcs://mchm5pza:7051 reset connection failed :: Error: Failed to connect before the deadline on Discoverer- name: peer0.org1.example.com, url:grpcs://<HOST1IP>:7051, connected:false, connectAttempted:true
2021-04-30T11:49:37.396Z - error: [DiscoveryService]: send[mychannel] - no discovery results
******** FAILED to run the application: Error: DiscoveryService has failed to return results

可以看出,用户注册成功,但无法建立网络连接。我认为可能有一些东西在掩护下,但我不知道我应该去哪里找。钱包和证书肯定没有冲突。

如何将应用从其他主机连接到此网络?

//编辑

有时你甚至不能相信自己。事实证明,tlcCACerts 是不正确的。但是在更正连接配置文件后,出现了一些新的错误:

2021-04-30T15:10:02.760Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Committer- name: orderer.example.com:7050, url:grpcs://orderer.example.com:7050, connected:false, connectAttempted:true
2021-04-30T15:10:02.760Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server orderer.example.com:7050 url:grpcs://orderer.example.com:7050 timeout:3000
2021-04-30T15:10:02.760Z - 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, connected:false, connectAttempted:true
2021-04-30T15:10:05.776Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com:7051, url:grpcs://peer0.org1.example.com:7051, connected:false, connectAttempted:true
2021-04-30T15:10:05.778Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org1.example.com:7051 url:grpcs://peer0.org1.example.com:7051 timeout:3000
2021-04-30T15:10:05.780Z - 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, connected:false, connectAttempted:true
2021-04-30T15:10:08.782Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Endorser- name: peer0.org2.example.com:9051, url:grpcs://peer0.org2.example.com:9051, connected:false, connectAttempted:true
2021-04-30T15:10:08.782Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org2.example.com:9051 url:grpcs://peer0.org2.example.com:9051 timeout:3000
2021-04-30T15:10:08.782Z - 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, connected:false, connectAttempted:true

似乎 HOST2 上的应用在 HOST1 上看不到 peers 和 orderer...

【问题讨论】:

    标签: hyperledger-fabric grpc blockchain hyperledger hyperledger-fabric-sdk-js


    【解决方案1】:

    我终于找到了第二个问题的解决方案(连接到另一台主机上的网络)。 您必须编辑您的 /etc/hosts 文件,并将 orderer 和 peers 主机名转换为部署网络的机器的 IP 地址。


    在 Linux 上:

    1. 使用管理员权限在文本编辑器中打开 /etc/hosts 文件
    2. 添加必要的主机和 IP 转换
    3. 保存文件

    在 Windows 上:

    1. 打开开始菜单
    2. 键入记事本并按 ctrl+shift+enter,以用户访问模式打开记事本
    3. 从记事本应用进入目录:c:\Windows\System32\drivers\etc
    4. 过滤所有文件类型,然后打开 hosts 文件
    5. 添加必要的主机和 IP 转换
    6. 保存文件

    【讨论】:

      【解决方案2】:

      您可能想查看这个端到端教程,其中包括构建应用程序并将其连接到远程 Hyperledger Fabric 网络:https://chainstack.com/deploy-a-hyperledger-fabric-v2-web-app-using-sdk-for-node-js/

      这里是源代码:https://github.com/chainstack/freedom-dividend-chaincode

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多