无法从 nodejs/python 客户端连接到 grpc

Posted

技术标签:

【中文标题】无法从 nodejs/python 客户端连接到 grpc【英文标题】:Not able to connect to grpc from nodejs/python client 【发布时间】:2018-09-03 16:25:26 【问题描述】:

我的 grpc 服务器在 go (go-micro) 中,并且工作正常,我已经通过 micro web 和 go web 应用程序 (iris) 进行了测试。

这是本地 grpc 设置

我不断收到此错误 (Stream removed)

 Error: 2 UNKNOWN: Stream removed
    at new createStatusError ([projectROOT]/node_modules/grpc/src/client.js:64:15)
    at [projectROOT]/node_modules/grpc/src/client.js:583:15
  code: 2,
  metadata: Metadata  _internal_repr:  ,
  details: 'Stream removed' 

有时我会收到此错误 (Trying to connect an http1.x server)

 Error: 14 UNAVAILABLE: Trying to connect an http1.x server
    at new createStatusError ([projectROOT]/node_modules/grpc/src/client.js:64:15)
    at [projectROOT]/node_modules/grpc/src/client.js:583:15
  code: 14,
  metadata: Metadata  _internal_repr:  ,
  details: 'Trying to connect an http1.x server' 

我的节点代码

const grpc = require('grpc');

const AuthPB      = require('./auth_pb');
const AuthService = require('./auth_grpc_pb');

const AuthClient = new AuthService.AuthClient(
    `localhost:59685`,
    grpc.credentials.createInsecure()
);

function run(cb) 
  const AuthTokenRequest = new AuthPB.AuthTokenRequest(
    token: `some token`,
  );
  AuthClient.isLoggedIn(AuthTokenRequest, (err, authRes) => 
    if(!err) return cb(status: 1);
    cb(
      status: 0,
      err,
      message: 'Not loggedin',
    );
  );



run(console.log);

Python 代码也面临同样的问题

错误
Traceback (most recent call last):
  File "app.py", line 15, in <module>
    run()
  File "app.py", line 11, in run
    res = AuthStub.IsLoggedIn(atReq)
  File "[python3.6-path]/site-packages/grpc/_channel.py", line 484, in __call__
    return _end_unary_response_blocking(state, call, False, deadline)
  File "[python3.6-path]/site-packages/grpc/_channel.py", line 434, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, Trying to connect an http1.x server)>

.

Traceback (most recent call last):
  File "app.py", line 15, in <module>
    run()
  File "app.py", line 11, in run
    res = AuthStub.IsLoggedIn(atReq)
  File "[python3.6-path]/site-packages/grpc/_channel.py", line 484, in __call__
    return _end_unary_response_blocking(state, call, False, deadline)
  File "[python3.6-path]/site-packages/grpc/_channel.py", line 434, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNKNOWN, Stream removed)>

我的 Python 代码

import grpc
import auth_pb2
import auth_pb2_grpc

def run():
    channel = grpc.insecure_channel('localhost:59685')
    AuthStub = auth_pb2_grpc.AuthStub(channel)
    atReq = auth_pb2.AuthTokenRequest(token='some token')
    res = AuthStub.IsLoggedIn(atReq)
    print("IsLoggedin: " + res.status)

if __name__ == "__main__":
    run()

【问题讨论】:

您可能会遇到与***.com/questions/49308482/…相同的问题:go-micro 的代码生成器生成的 URL 路径与 gRPC 使用的标准路径不兼容。 我在使用基于 grpc node lib 的客户端时遇到了同样的问题,即 grpc repo 中的 grpcc 和官方客户端示例。 我不确定为什么会发生这种情况,但我知道 GRPC 会强制 HTTP/1 用于不安全的连接,并且您的客户端可能不喜欢 HTTP/1。尝试使用使用 HTTP/2 的 TLS 【参考方案1】:

当客户端尝试创建不安全的连接但服务器使用 ssl 时,我收到相同的错误消息

尝试替换

grpc.credentials.createInsecure()

grpc.credentials.createSsl()

【讨论】:

以上是关于无法从 nodejs/python 客户端连接到 grpc的主要内容,如果未能解决你的问题,请参考以下文章

phpMyAdmin 无法从 Linux 客户端连接到 Windows 7 上的远程服务器

无法从 https 客户端通过 wss 连接到 emqx mqtt 代理

无法从 Express 连接到远程 Redis

无法从 java 应用程序连接到 sqlserver

Flyway 无法从 Docker-Container 连接到 MySQL

我无法使用 DataGrip 数据库客户端从本地主机连接到 docker 中的 mysql