rabbitmq 我如何通过Krakend API的异步代理发布消费的消息?

h5qlskok  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(206)

这是我的krakend.json配置

"async_agent": [
    {
      "name": "test-agent",
      "connection": {
        "max_retries": 10,
        "backoff_strategy": "exponential-jitter"
      },
      "consumer": {
        "topic": "krakend",
        "workers": 5
      },
      "backend": [
        {
          "url_pattern": "/greeted",
          "method": "POST",
          "host": [ "http://127.0.0.1:2999" ],
          "disable_host_sanitize": false
        }
      ],
      "extra_config": {
        "async/amqp": {
          "host": "amqp://guest:guest@localhost:5672/",
          "name": "krakend",
          "exchange": "ApiGatewayExchange",
          "durable": true,
          "delete": false,
          "exclusive": false,
          "no_wait": false,
          "auto_ack": false
        }
      }
    }
  ]

从服务发送消息,如下所示:

export class AppService {
  constructor(@Inject('GREETING_SERVICE') private client: ClientProxy){}

  getHello(): ResponseDTO {
    const responseDTO: ResponseDTO = {
      action: 'Hello',
      service: 'from service A'
    }
    this.client.emit('', responseDTO);
    return responseDTO;
  }
}

并导入了***GREETING_SERVICE***配置,如下所示:

imports: [
    ClientsModule.register([
      {
        name: 'GREETING_SERVICE',
        transport: Transport.RMQ,
        options: {
          urls: ['amqp://test:test@localhost:5672/'],
          queue: 'krakend'
        }
      }
    ])
  ],

最后,这是 * 另一个 * 服务(我们将其称为service-c)中的端点,它从使用者那里获取该消息:

@Post('greeted')
  TestHello(@Body() data: any) {
    console.log(data)
    return data
  }

消息按照krakend文件中async_agent的设置被成功使用,但是消息并没有作为主体发布到那个端点,当我对应该传递的数据做console.log时,它只打印{}
我做错什么了吗?我已经挠头好几个小时了。

svmlkihl

svmlkihl1#

您的krakend.json配置的异步部分在我看来不错,但我怀疑您可能遇到的问题。
现在大多数的javascript框架都需要你传递特定的头来发挥它们的魔力,比如Content-TypeAccept。你必须考虑到KrakenD会传递一组非常精简的头给你的NestJS应用程序(据我所知是Accept-EncodingUser-Agent)。
我对NestJS不熟悉,但我敢打赌,您需要通过Content-Type,并且您已经准备就绪。以下是我的配置建议:

"async_agent": [
        {
            "name": "test-agent",
            "connection": {
                "max_retries": 10,
                "backoff_strategy": "exponential-jitter"
            },
            "consumer": {
                "topic": "krakend",
                "workers": 5
            },
            "backend": [
                {
                    "url_pattern": "/greeted",
                    "method": "POST",
                    "host": [
                        "http://127.0.0.1:2999"
                    ],
                    "disable_host_sanitize": false,
                    "extra_config": {
                        "modifier/martian": {
                            "header.Modifier": {
                                "scope": [
                                    "request"
                                ],
                                "name": "Content-Type",
                                "value": "application/json"
                            }
                        }
                    }
                }
            ],
            "extra_config": {
                "async/amqp": {
                    "host": "amqp://guest:guest@localhost:5672/",
                    "name": "krakend",
                    "exchange": "ApiGatewayExchange",
                    "durable": true,
                    "delete": false,
                    "exclusive": false,
                    "no_wait": false,
                    "auto_ack": false
                }
            }
        }
    ]
}

希望这对你有帮助

相关问题