【发布时间】:2021-10-19 23:44:15
【问题描述】:
我正在学习有关 NestJS 和 gRPC 的新知识。我只是遇到了一个问题。我创建了一个身份验证微服务和一个网关,但我得到一个空响应。
网关和微服务被触发。但我得到了一个空对象。如果我在没有 gRPC 的网关中进行硬编码,那么它可以工作。有谁知道我忘记了什么?
网关
import { Body, Controller, OnModuleInit, Post } from '@nestjs/common';
import { Client, ClientGrpc } from '@nestjs/microservices';
import { authOptions } from 'src/grpc.options';
import { AuthService } from './grpc.interface';
@Controller('auth')
export class AuthController implements OnModuleInit {
@Client(authOptions)
private client: ClientGrpc;
private authService: AuthService;
onModuleInit() {
this.authService = this.client.getService<AuthService>('AuthService');
}
@Post('login')
async login(@Body('username') username: string, @Body('password') password: string)
{
return this.authService.login({ username, password });
}
}
身份验证微服务
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
interface LoginArgs {
username: string;
password: string;
}
interface LoginReturn {
access_token: string;
}
@Controller('auth')
export class AuthController {
@GrpcMethod('AuthService', 'Login')
login(data: LoginArgs, metadata: any): LoginReturn
{
return {
access_token: '123',
}
}
}
原型
syntax = "proto3";
package auth;
service AuthService {
rpc Login (LoginArgs) returns (LoginReturn);
}
message LoginArgs {
string username = 1;
string password = 2;
}
message LoginReturn {
string access_token = 1;
}
【问题讨论】: