我们可以在Nestjs中使用MessagePattern和REST方法允许API使用吗?
我有一个BFF需要向Servicea发送一些请求。
Servicea提供了我们可以处理的一些API(get,post,...)。 例如:
@Get('greeting')
getGreetingMessage(@Param('name') name: string): string {
return `Hello ${name}`;
}
在Nestjs中的微服务体系结构中,我看到BFF中的最佳实践将请求发送到其他Servcies是使用有效载荷的消息模式,例如cmd
。 例如,
constructor(
@Inject('SERVICE_A') private readonly clientServiceA: ClientProxy,
) {}
getGreetingFromServiceA() {
const startTs = Date.now();
const pattern = { cmd: 'greeting' };
const payload = {};
return this.clientServiceA
.send<string>(pattern, payload)
.pipe(
map((message: string) => ({ message, duration: Date.now() - startTs })),
);
}
这样做我必须支持Servicea中的MessagePattern,
@MessagePattern({cmd: 'greeting'})
getGreetingMessage(name: string): string {
return `Hello ${name}`;
}
所以我的问题是有没有办法将MessagePattern添加到Servicea中的API?因此,我可以通过BFF从静置获取请求或MessagePattern使用两种不同的方式致电它们。 我正在考虑使用2个文档(GET和MessagePattersn) 这样
@Get('greeting')
@MessagePattern({cmd: 'greeting'})
getGreetingMessage(@Param('name') name: string): string {
return `Hello ${name}`;
}
,如果没有,我该如何使用ProxyClient将HTTP请求发送到BFF中的其他微服务?
I have a BFF needs to send some requests to ServiceA.
ServiceA is providing some API (GET, POST, ...) that we can deal with.
For example:
@Get('greeting')
getGreetingMessage(@Param('name') name: string): string {
return `Hello ${name}`;
}
In MicroService Architecture in NestJs I see the best practice in BFF to send requests to other servcies is to use Message patterns like cmd
with payloads.
For example
constructor(
@Inject('SERVICE_A') private readonly clientServiceA: ClientProxy,
) {}
getGreetingFromServiceA() {
const startTs = Date.now();
const pattern = { cmd: 'greeting' };
const payload = {};
return this.clientServiceA
.send<string>(pattern, payload)
.pipe(
map((message: string) => ({ message, duration: Date.now() - startTs })),
);
}
So to do that I have to support MessagePattern in ServiceA like:
@MessagePattern({cmd: 'greeting'})
getGreetingMessage(name: string): string {
return `Hello ${name}`;
}
So my question is Is there a way to append MessagePattern to exisiting APIs in ServiceA? so I can call them with 2 different ways either by Rest GET Request or MessagePattern from BFF.
I'm thinking about using 2 docerators (Get and MessagePattern)
Like that
@Get('greeting')
@MessagePattern({cmd: 'greeting'})
getGreetingMessage(@Param('name') name: string): string {
return `Hello ${name}`;
}
If no, so how can I use a proxyClient to make http requests to other microservice in the BFF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,在Nestjs中,不可能将多个装饰器定义为控制器中的相同方法,但我们将其制造为支持不同的通信协议的混合应用程序,以便我们可以通过TCP或HTTP来调用它,等等在此示例中href =“ https://docs.nestjs.com/faq/hybrid-application” rel =“ nofollow noreferrer”> https://docs.nestjs.com/faq/faq/hybrid-application
Actually it is not possible in NestJS to define more than one decorator to the same method in the controller but we make it a hybrid application which supports different communication protocols so we can call it via TCP or HTTP and so on like in this example https://docs.nestjs.com/faq/hybrid-application