我正在使用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”
状态码: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