无法使用 grpcurl 发送请求

Can't send a request using grpcurl

我有一个使用NestJs+gRPC的服务器,我在PostgreSQL中存储数据,获取数据等没有问题。我无法发送 grpcurl 请求:((

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice<MicroserviceOptions>(grpcClientOptions);

  await app.startAllMicroservicesAsync();
  await app.listen(3000);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
(async () => await bootstrap())();

export const grpcClientOptions: ClientOptions = {
  transport: Transport.GRPC,
  options: {
    url: '0.0.0.0:5000',
    package: 'user',
    protoPath: join(__dirname,'user/user.proto'),
    loader: {
      keepCase: true,
      longs: Number,
      defaults: false,
      arrays: true,
      objects: true,
    },
  },
};

原型文件看起来像

syntax = "proto3";

package user;

service UserService {
  rpc FindOne (UserById) returns (User) {}
}

message UserById {
  string id = 1;
}

message User {
  int32 id = 1;
  string name = 2;
  string password = 3;
  string email = 4;
  string createdAt = 5;
  string updatedAt = 6;
}

和用户控制器

  @Get(':id')
  getById(@Param('id') id: string): Observable<User> {
    console.log(id);
    return this.userService.findOne({ id : +id });
  }

  @GrpcMethod('UserService','FindOne')
  async findOne(data: UserById): Promise<User> {
    const { id } = data;
    console.log(id);
    return this.userModel.findOne({
      where: {
        id : id
      },
    });
  }

当我从浏览器发送请求时,它工作正常,但我不能使用 grpcurl。 enter image description here

感谢转发!

我怀疑 (!) 问题是您在 Windows 但在 src/user 和 [=21= 之间使用 forward-slash (/) ] (!?) 文件路径分隔符应该是 back-slash (\).

请在 Microsoft Test gRPC services with gRPCurl 上查看此 link 并尝试 -d '{\"id\": 1}'

winodws 上的 Grpcurl 不同于 Linux/Mac。 在 winodws 上,不需要包含 json 消息单引号。 例如

grpcurl.exe --plaintext -d {\"message\":\"How\u0020are\u0020you\"} localhost:9090  GreetingService.greeting

注意:我们需要使用 \u0020 转义空格并使用反斜杠转义双引号'\'。