NodeJs gRPC Server

时间:2017-05-29 19:22:43

标签: node.js grpc

没有错误,没有回应我的客户。我的message.proto:

syntax = "proto3";

message TstCoordinates {
    required int32 id = 1;
    required string firstname = 2;
    required string lastname = 3;
    required string email = 4;
    required string areacode = 5;
    required string phone = 6;
    required string extension = 7;  
}

message TstId {
    required int32 id = 1;
}

message Empty {}

service TstService{
    rpc SendCoordinates (TstId) returns (TstCoordinates);
    rpc List (Empty) returns (TstCoordinates);
}

我的gRPC服务器:

'use strict';

const fs = require('fs');
const grpc = require('grpc');
const serviceDef = grpc.load("message.proto");
const PORT = 7777;

const cacert = fs.readFileSync('certs/ca.crt'),
      cert = fs.readFileSync('certs/server.crt'),
      key = fs.readFileSync('certs/server.key'),
      kvpair = {
          'private_key': key,
          'cert_chain': cert
      };
const creds = grpc.ServerCredentials.createSsl(cacert, [kvpair]);

var tstcoordinates = [
    {
        id: 1,
        firstname: "Bill",
        lastname: "Williams",
        email: "williams@example.com",
        areacode: "444",
        phone: "555-1212",
        extension: "378"
    },
    {
        id: 2,
        firstname: "Happy",
        lastname: "Golucky",
        email: "lucky@example.com",
        areacode: "444",
        phone: "555-1212",
        extension: "382"
    }
];

var server = new grpc.Server();

server.addService(serviceDef.TstService.service, {
    list: function(call, callback) {
        console.log("in list");
        callback(null, tstcoordinates[0]);
    },
    sendCoordinates: function(call, callback) {
        console.log("in sendCoordinates");
        callback(null, tstcoordinates[1] );
        return;
    }
});

server.bind(`0.0.0.0:${PORT}`, creds);
console.log(`Starting gRPC server on port ${PORT}`);
server.start();

我的客户:

'use strict';

const fs = require('fs');
const process = require('process');
const grpc = require('grpc');
const serviceDef = grpc.load("message.proto");

const PORT = 7777;

const cacert = fs.readFileSync('certs/ca.crt'),
       cert = fs.readFileSync('certs/client.crt'),
       key = fs.readFileSync('certs/client.key'),
       kvpair = {
          'private_key': key,
          'cert_chain': cert
      };

const creds = grpc.credentials.createSsl(cacert, key, cert);

const client = new serviceDef.TstService(`hyperledger-devenv:${PORT}`,creds);
console.log("secure connection established with gRPC server");

lst();
snd();

function printResponse(error, response) {
    console.log("in printResponse");
    if (error)
        console.log('Error: ', error);
    else
        console.log(response);
}

function lst() {
    console.log("in list");
    client.list({}, function(error, response) {
        console.log("in list call");
        printResponse(error, response);
    });
}

function snd() {
    console.log("in snd");
    client.sendCoordinates({'id': 1}, function(error, response) {
        console.log("in snd call");
        printResponse(error, response);
    });
}

当我做一个" curl localhost:7777"命令,服务器显示SSL握手错误,所以我知道它正在监听。客户端显示:

secure connection established with gRPC server
in list
in snd

这就是全部。任何一方都没有错误。我试过没有SSL,得到完全相同的结果。

我的版本: node --version v6.9.5

protoc --version libprotoc 2.6.1

npm --version 3.10.10

非常感谢任何帮助。

TIA

1 个答案:

答案 0 :(得分:0)

你是对的murgatroid99。只需将computername添加到我的/ etc / hosts文件中,我就解决了这个问题。

127.0.0.1 hyperledger-devenv

名称" hyperledger-devenv"是我在Vagrant的配置中放置的价值" Vagrantfile"用于虚拟映像名称。这在我的SSH提示中显示出来。可以使用hostname命令显示真实的计算机名称。

对于那些感兴趣的人,我在博客文章中记录了我的所有步骤: https://bertrandszoghy.wordpress.com/2017/05/30/send-and-receive-protocol-buffers-securely-with-grpc-in-nodejs/

非常感谢!