【问题标题】:Nest grpc client is not communicating with the grpc serverNest grpc 客户端未与 grpc 服务器通信
【发布时间】:2020-10-18 04:02:33
【问题描述】:

我试图在我的 Nest js 项目中实现一个简单的 grpc 服务,但不幸的是我的客户端没有与服务器通信。我在分享 下面的一些代码。如果有人能给我一些关于出了什么问题的见解,那将非常有帮助。

--------- GRPC 服务器 --------

Main.ts 文件如下所示。

 async function setUpGrpc(grpcUrl){
   const grpcApp = await NestFactory.createMicroservice<MicroserviceOptions>(GrpcModule, {
   transport: Transport.GRPC,
   options: {
    package: 'protos.user',
    url : grpcUrl,
    protoPath: join(__dirname, '/protos/user.proto'),
    },
   });
   return grpcApp;
}

async function bootstrap() {
   const app = await NestFactory.create(AppModule);
   const configService = app.get(ConfigService);
   const port = configService.get('PORT');

   const grpcUrl = configService.get('GRPC_URL');
   console.log(grpcUrl);
   const grpcApp = await setUpGrpc(grpcUrl);

   await grpcApp.listen(()=>{
     console.log("Grpc is listening")
   });
   await app.listen(port);
  }
bootstrap();

Grpc 模块

@Module({
  imports: [
    SequelizeDbModule,
  ],
  controllers: [GrpcUserController],
  providers: [],
})
export class GrpcModule {}

GrpcUserController

interface IUserInfo {
  userName: string;
  passWord: string;
  tenantId: string;
}
interface IUser {
  id: string;
  name: string;
  email: string;
  phone: string;
  tenantId: string;
}

@Controller()
export class GrpcUserController {
  constructor() {}

   @GrpcMethod('UserValidationService', 'ValidateUser')
   validateUser(userInfo: IUserInfo, metaData: any) : IUser {
    console.log('Call is made');

    return {name:'Test',phone:'Test',email:"Test@Test",tenantId:"TestTenant",id:'TestId'};
  }
}

我的 Proto 文件如下所示。

syntax = "proto3";

package protos.user;

service UserValidationService {
  rpc ValidateUser (UserInfo) returns (User) {}
}

message UserInfo {
  string userName = 1;
  string passWord = 2;
  string tenantId = 3;
}

message User {
  string id = 1;
  string name = 2;
  string email = 3;
  string phone = 4;
  string tenantId = 5;
}

-------- 客户--------

使用相同的原型文件

我把客户端做成了嵌套库

@Injectable()
export class UserGrpcClientService implements OnModuleInit {

  userValidationService:IUserValidationService;

  @Client( {
    transport: Transport.GRPC,
    options: {
      package: 'protos.user',
      url : "localhost:5000",
      protoPath: join(__dirname, '/protos/user.proto'),
    },
  })
  client : ClientGrpc
  constructor(){}

  onModuleInit() {
     this.userValidationService = this.client.getService<IUserValidationService('UserValidationService');
    console.log(this.userValidationService);
  }

    validateUser(userInfo: IUserInfo) : Observable<any>{

     console.log("Inside validateUser");
     console.log(JSON.stringify(userInfo));
     return  this.userValidationService.validateUser({userName:userInfo.userName,
                                                passWord:userInfo.passWord,
                                                tenantId:userInfo.tenantId}); 
  }
}   

接口

export interface IUserInfo {
  userName: string;
  passWord: string;
  tenantId: string;
}
export interface IUser {
  id: string;
  name: string;
  email: string;
  phone: string;
  tenantId: string;
}

export interface IUserValidationService{
  validateUser (UserInfo : IUserInfo) : Observable<any>
}

我已将此客户端库集成到另一个正在运行的嵌套应用程序中。

  @Post('auth/token')
  @HttpCode(200)
  async token(
    @Body() tokenRequest: TokenRequestDTO,
    @Headers() headers,
  ): Promise<Object> {
    this.logger.info('Inside token request');

    this.helper.validateRequestBody(tokenRequest);
    this.helper.validateHeader(headers);

    var response =  this.userGrpcClient.validateUser({
      userName: 'test',
      passWord: 'test',
      tenantId: 'test',
    })

    console.log(response);
    return await this.authService.provideToken(tokenRequest, headers);
  }

当我运行它时,没有错误。我记录的响应显示 可观察 { _isScalar: false, _subscribe: [Function] }

【问题讨论】:

  • 得到同样的错误。有什么解决办法吗?
  • 您好,您必须订阅。 GRPC 客户端将返回一个 observable

标签: grpc nestjs


【解决方案1】:

事实证明,当使用 userGrpcClient 调用 validateUser 函数时,它会返回一个 observable,我必须订阅它。

【讨论】:

    猜你喜欢
    • 2016-10-04
    • 2018-04-12
    • 2021-07-31
    • 2020-09-09
    • 2017-03-30
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多