我正在使用nest js framework与nuts消息代理.
首先,我有Gateway Service个路由.
Controller个
@ApiOperation({ description: 'Cheking phone and code' })
@Get('/checkPhoneCode')
@ApiQuery({ name: 'phone' })
@ApiQuery({ name: 'code' })
checkPhoneCode(@Query() phone_with_code: PhoneWithCode) {
console.log('gateway check phone');
return this.authenticationService.checkPhoneCode(phone_with_code);
}
Service
async checkPhoneCode(phone_with_code: PhoneWithCode) {
console.log('sending nuts request to check phone');
return this.nats.send('api-gateway.authentication.checkPhoneCode',
phone_with_code);
}
我也有Auth Service个等待坚果调用执行操作.
Controller个
@MessagePattern('api-gateway.authentication.checkPhoneCode')
checkPhoneCode(phone_with_code: PhoneWithCodeDto) {
console.log('controller check phone');
return this.authenticationService.checkPhoneCode(phone_with_code);
}
Service
public async checkPhoneCode(phoneCodeDto: PhoneWithCodeDto) {
const { phone } = phoneCodeDto;
const { code } = phoneCodeDto;
validator.isMobilePhone(phone);
console.log('sending request');
const response = await firstValueFrom(
this.httpService.get(
`https://example.com/method/v1.0/account/checkphonecode/?phone=${phone}&code=${code}`,
),
);
if (!response) {
return 'error while checking phone code';
}
if(!response.data.correct){
return response.data;
}
return response.data;
// return this.generatePhoneVaildToken(phone);
}
当我调用网关服务时,auth方法执行两次. 我添加了控制台日志(log)来调试,我看到了什么:
api5dev-api-gateway | gateway check phone
api5dev-api-gateway | sending nuts request to check phone
api5dev-authentication | controller check phone
api5dev-authentication | sending request
api5dev-authentication | controller check phone
api5dev-authentication | sending request
为什么AUTH CONTROLLER呼叫了两次?
-UPD,对不起.
在这个问题中,我提出了两次调用控件的例子,nats收到消息后,从服务. 但实际上似乎是整个应用的问题. 我可以在NATS中添加额外的设置来避免这种情况吗? 我目前的配置很简单,就像官方文档中介绍的那样.
目前我使用的是简单的NatS配置:
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.NATS,
options: {
servers: ['nats://nats:4222'],
},
});
发送的示例
async getBannersByCityId(cityId: string) {
const params = { cityId };
return this.nats.send('api-gateway.common.getBannersById',
params);
}
接收示例
@MessagePattern('api-gateway.common.getBannersById')
getBannersByCityId(getBanners: GetBannerByCityIdDto) {
console.log('banner list');
return this.bannerService.getBannersByCityId(getBanners);
}
客户端设置:
ClientsModule.register([
{
name: 'COMMON_SERVICE',
transport: Transport.NATS,
},
]),
在那个配置中,我发现getBannersByCityId(getBanners: GetBannerByCityIdDto)emits 了两次
也许我需要配置附加标头https://docs.nats.io/using-nats/developer/develop_jetstream/model_deep_dive?