关于json:Logstash-如何通过RabbitMQ触发Celery任务

Logstash - How to trigger Celery tasks through RabbitMQ

有人可以向我解释如何通过Logstash触发Celery任务吗?
可能吗?

如果我尝试通过" php-amqplib"库在PHP中执行此操作,它将正常工作:(无需使用Logstash)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$connection = new AMQPStreamConnection(
    'rabbitmq.local',
    5672,
    'guest',
    'guest'
);
$channel = $connection->channel();
$channel->queue_declare(
    'celery',
    false,
    true,
    false,
    false
);
$taskId = rand(1000, 10000);
$props = array(
    'content_type' => 'application/json',
    'content_encoding' => 'utf-8',
);

$body = array(
    'task'      => 'process_next_task',
    'lang'      => 'py',
    'args'      => array('ktest' => 'vtest'),
    'kwargs'    => array('ktest' => 'vtest'),
    'origin'    => '@'.'mytest',
    'id'        => $taskId,
);

$msg = new AMQPMessage(json_encode($body), $props);
$channel->basic_publish($msg, 'celery', 'celery');

根据芹菜文档:

http://docs.celeryproject.org/en/latest/internals/protocol.html

我正在尝试以json格式发送请求,这是我的Logstash过滤器:

1
2
3
4
5
6
7
8
9
10
11
ruby
{
    remove_field => ['headers', '@timestamp', '@version', 'host', 'type']
    code =>"
        event.set('properties',
        {
            :content_type => 'application/json',
            :content_encoding => 'utf-8'
        })
   "
}

芹菜的答案是:

1
2
[2017-05-05 14:35:09,090: WARNING/MainProcess] Received and deleted unknown message.  Wrong destination?!
{content_type:None content_encoding:None delivery_info:{'exchange': 'celery', 'routing_key': 'celery', 'redelivered': False, 'consumer_tag': 'None4', 'delivery_tag': 66} headers={}}

基本上,Celery无法解码我的消息格式或更好的消息...我无法将请求设置为JSON格式:)

这让我发疯,在此先感谢您提供任何线索:)

忘记了,这是我在Logstash中的输出插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
rabbitmq
            {
                key             =>"celery"
                exchange        =>"celery"
                exchange_type   =>"direct"
                user            =>"${RABBITMQ_USER}"
                password        =>"${RABBITMQ_PASSWORD}"
                host            =>"${RABBITMQ_HOST}"
                port            =>"${RABBITMQ_PORT}"
                durable         => true
                persistent      => true
                codec           => json

            }

根据此问题提供的信息,您不能这样做。

当您在ruby过滤器中处理事件时,实际上是在将要放置在邮件正文中的内容上进行播放,同时您希望设置Rabbitmq标头和邮件的属性。

直到解决了该功能为止,除非您自己实现,否则我认为您将无法实现它。 毕竟,该插件在github上可用。


正如奥利维尔(Olivier)所说,目前无法实现,但我已经向官方项目创建了请求请求。
https://github.com/logstash-plugins/logstash-output-rabbitmq/pull/59

如果您正在寻找工作版本,请查看我的克隆:

https://github.com/useless-stuff/logstash-output-rabbitmq

您应该对该代码感到非常害怕:)

我完全是一名Ruby开发人员

但这有效:)