0

我正在一个角度应用程序中尝试 grpc-web hello world 程序,通过 nginx 访问本地 python 服务器,我在浏览器中收到 ERR_INVALID_HTTP_RESPONSE 错误。我认为该请求甚至没有命中 nginx 代理。虽然我可以通过 python 客户端直接和通过代理访问服务器。

helloworld.proto

syntax = "proto3";

option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

客户端.ts

  const greeterService = new GreeterClient(
      'http://localhost:8086',
      null,
      null
    );

    const request = new HelloRequest();
    request.setName('Hello World!');

    const call = greeterService.sayHello(
      request,
      { 'custom-header-1': 'value1' },
      (err: grpcWeb.Error, response: HelloReply) => {
        console.log(response.getMessage());
      }
    );
    call.on('status', (status: grpcWeb.Status) => {
      console.log(status);
    });

蟒蛇服务器.py

class Greeter(helloworld_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port('[::]:50052')
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    logging.basicConfig()
    serve()

nginx.conf

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';

     server {
        listen 8086 http2;
 
        access_log logs/access.log main;
 
        location /helloworld.Greeter {
            grpc_pass grpc://localhost:50052;
        }
    }
    
}

events{}

面临的错误

任何指针我该如何解决这个问题?谢谢

4

1 回答 1

0

我认为nginx不支持 grpc-web (在通常情况下,即使可能有一些步骤可以尝试使其工作,请参阅https://github.com/grpc/grpc-web/issues/381#issuecomment- 439783765)。它只支持 grpc,这就是 python 客户端工作的原因。对于您可以使用的 grpc-web envoy,请参阅此处的配置:https ://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/examples/echo/envoy.yaml

于 2020-10-06T02:33:44.407 回答