NestJS gRPC路由404

问题描述 投票:0回答:2

我正在使用NestJS gRPC教程项目:

https://github.com/nestjs/nest/blob/master/sample/04-grpc/

[hero.proto文件内容:

syntax = "proto3";

package hero;

service HeroService {
  rpc FindOne (HeroById) returns (Hero) {}
}

message HeroById {
  int32 id = 1;
}

message Hero {
  int32 id = 1;
  string name = 2;
}

据我了解,将路径定义为:

/hero.HeroService/FindOne

hero.controller.ts内容:

@Controller()
export class HeroController implements OnModuleInit {
  @GrpcMethod('HeroService')
  findOne(data: HeroById): Hero {
    const items: Hero[] = [{ id: 1, name: 'John' }, { id: 2, name: 'Doe' }];
    return items.find(({ id }) => id === data.id);
  }
}

理论上应在以下位置公开该方法:

http://localhost:3001/hero.HeroService/FindOne

但是当被调用时,会产生此错误:

错误:“未找到”消息:“无法发布/hero.HeroService/FindOne”statusCode:404

我知道我要打NestJS,因为我有中间件日志记录。如果我命中了GET路由,则可以正常工作并返回正确的数据,但是GRPC路由都是404。我想念什么?

我尝试过@Controller('hero')尝试为路径添加前缀。让NestJS注销所有gRPC端点供我调试似乎并不容易...

完整的项目源代码在这里:

https://github.com/kmturley/angular-nest-grpc

------更新-------

已安装grpc_cli使用:

brew tap grpc/grpc
brew install --with-plugins grpc

然后尝试使用以下方法查看端点:

export GRPC_VERBOSITY=DEBUG
grpc_cli ls localhost:3001

并得到错误:

查询服务端点时收到错误。

或使用:

grpc_cli call localhost:3001 FindOne "id: 1" --protofiles=src/hero/hero.proto

我收到错误:

ServerReflectionInfo rpc failed. Error code: 14, details: OS Error
angular grpc nestjs
2个回答
0
投票

通过在grpc-client.options.ts中添加额外的一行以定义URL,解决了该问题:

export const grpcClientOptions: ClientOptions = {
  transport: Transport.GRPC,
  options: {
    url: 'localhost:50051',
    package: 'hero',
    protoPath: join(__dirname, './hero/hero.proto'),
  },
};

然后我可以使用:在该端口上连接到它]

grpc_cli call localhost:50051 FindOne "id: 1" --protofiles=backend/src/hero/hero.proto

您可以在这里看到它的工作:

https://github.com/kmturley/angular-nest-grpc


0
投票

grpc传输的默认端口在nestjs中为5000,请参见https://github.com/nestjs/nest/blob/v6.10.14/packages/microservices/constants.ts#L8

© www.soinside.com 2019 - 2024. All rights reserved.