简体   繁体   English

如何将元数据添加到Node.js grpc调用

[英]How to add metadata to nodejs grpc call

I'd like to know how to add metadata to a nodejs grpc function call. 我想知道如何将元数据添加到nodejs grpc函数调用中。 I can use channel credentials when making the client with 与客户建立联系时,我可以使用渠道凭证

var client = new proto.Document('some.address:8000',
    grpc.credentials.createInsecure()
)

Which are send when using client.Send(doc, callback) , but the go grpc server looks in the call metadata for identification information which I have to set. 使用client.Send(doc, callback)时发送的是哪些,但是go grpc服务器在调用元数据中查找我必须设置的标识信息。 I tried using grpc.credentials.combineChannelCredentials with the insecure connection and a grpc.Metadata instance but I can't find the right way to do it. 我尝试将grpc.credentials.combineChannelCredentials与不安全的连接和grpc.Metadata实例一起使用,但是找不到正确的方法。

The error I run into is TypeError: compose's first argument must be a CallCredentials object . 我遇到的错误是TypeError: compose's first argument must be a CallCredentials object I tried to follow it down but it goes into c code which loses me, I can't see what javascript type I have to give to comebineChannelCredentials to achieve what I'm looking for and the docs are a little sparse on how to achieve this. 我试图跟进它,但是它陷入了C代码,这让我comebineChannelCredentials了我,我看comebineChannelCredentials为实现我想要的功能而必须给comebineChannelCredentials提供什么javascript类型,并且文档对如何实现这一目标有点稀疏。

You can pass metadata directly as an optional argument to a method call. 您可以将元数据直接作为可选参数传递给方法调用。 So, for example, you could do this: 因此,例如,您可以这样做:

var meta = new grpc.Metadata();
meta.add('key', 'value');
client.send(doc, meta, callback);

For sake of completeness I'm going to extend on @murgatroid99 answer. 为了完整起见,我将扩展@ murgatroid99的答案。

In order to attach metadata to a message on the client you can use: 为了将元数据附加到客户端上的消息您可以使用:

var meta = new grpc.Metadata();
meta.add('key', 'value');
client.send(doc, meta, callback);

On the server side int your RPC method being called, when you want to grab your data you can use: 在服务器端int上,您的RPC方法被调用,当您想要获取数据时,可以使用:

function(call, callback){ 
   var myVals = call.metadata.get("key"); 
   //My vals will be an array, so if you want to grab a single value:
   var myVal = myVals[0]; 
}

I eventually worked it out through introspecting the grpc credentials code and modifying the implementation to expose an inner function. 我最终通过内省grpc凭证代码并修改实现以公开内部功能来解决。 In the grpc module in the node_modules , file grpc/src/node/src/credentials.js add the line grpc在模块node_modules ,文件grpc/src/node/src/credentials.js添加行

exports.CallCredentials = CallCredentials;

after CallCredentials is imported. 导入CallCredentials之后。 Then, in your code, you can write something like 然后,在您的代码中,您可以编写如下内容

var meta = grpc.Metadata();
meta.add('key', 'value');
var extra_creds = grpc.credentials.CallCredentials.createFromPlugin(
  function (url, callback) {
    callback(null, meta);
  }
)

Then use extra_creds in the client builder 然后在客户端构建器中使用extra_creds

var creds = grpc.credentials.combineChannelCredentials(
  grpc.credentials.createSsl(),
  extra_creds,
)

Now you can make your client 现在您可以使您的客户

var client = new proto.Document(
  'some.address:8000',
  creds,
)

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

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