Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMQ多个队列支持 #8

Closed
327 opened this issue Jul 26, 2018 · 6 comments
Closed

CMQ多个队列支持 #8

327 opened this issue Jul 26, 2018 · 6 comments

Comments

@327
Copy link
Contributor

327 commented Jul 26, 2018

作者您好,如果我需要从 CMQ 的多个队列中获取数据,在配置文件中CMQ_QUEUE 配置项该怎么填写?
是否支持多个队列?

@freyo
Copy link
Owner

freyo commented Jul 26, 2018

是支持多个队列的。

如果是 相同地域 和 账号,可以直接指定队列名称;

如果是 不同地域 或 账号,需要在 config/queue.php 里新增连接:

'connections' => [
	//...
	'your-connection-name' => [
		'driver' => 'cmq',
		'secret_key' => 'your-secret-key',
		'secret_id'  => 'your-secret-id',
		'queue' => 'your-queue-name',
		'options' => [
			'queue' => [
				'host'                 => 'https://cmq-queue-region.api.qcloud.com',
				'name'                 => 'your-queue-name',
				'polling_wait_seconds' => 0, // 0-30seconds
			],
			'topic' => [
				'enable' => false,
				'filter' => 'routing', //routing or msgtag
				'host'   => 'https://cmq-topic-region.api.qcloud.com',
				'name'   => '',
			],
		],
	];
	//...
];

分派 Job 时指定连接和队列

Job::dispatch()->onConnection('your-connection-name')->onQueue('queue-name-1');
Job::dispatch()->onConnection('your-connection-name')->onQueue('queue-name-2');

dispatch((new Job)->onConnection('your-connection-name')->onQueue('queue-name-1'));
dispatch((new Job)->onConnection('your-connection-name')->onQueue('queue-name-2'));

Job 内指定连接和队列

<?php

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
    /**
     * The name of the connection the job belongs to.
     */
    protected $connectionName = 'your-connection-name';

    /**
     * The name of the queue the job belongs to.
     *
     * @var string
     */
    protected $queue = 'your-queue-name';
}

处理指定的连接和队列


php artisan queue:work {connection-name} --queue={queue-name}

@327
Copy link
Contributor Author

327 commented Jul 26, 2018

感谢您的回复,几个队列都在一个账号、同一地域下,唯有 queue name 不同
你说:“如果是 相同地域 和 账号,可以直接指定队列名称;”
能否给个示例?是在 env 文件中指定多个CMQ_QUEUE吗?

@327
Copy link
Contributor Author

327 commented Jul 26, 2018

还是指:php artisan queue:work {connection-name} --queue={queue-name}
执行此命令时,指定不同 --queue 参数即可?

@327
Copy link
Contributor Author

327 commented Jul 26, 2018

上面的问题已经解决,感谢您。
有个新问题,向 CMQ 写入消息的生产者,并不使用 laravel 框架,甚至是别的语言,如果继续用laravel 作为消费者,那消息的 payload 是不是要按照特定的格式生成?

@freyo
Copy link
Owner

freyo commented Jul 27, 2018

.env 中的 CMQ_QUEUE 配置项只是 默认队列名称,实际使用中是可以通过 onQueue 方法或 $queue 属性动态指定的,默认连接名cmq

$payload 目前为 Laravel 默认的格式,按照此格式生成都是可以解析的,但跨语言可能比较麻烦

{
    "displayName":"App\\Jobs\\TestJob",
    "job":"Illuminate\\Queue\\CallQueuedHandler@call",
    "maxTries":null,
    "timeout":null,
    "data":{
        "commandName":"App\\Jobs\\TestJob",
        "command":"O:16:\"App\\Jobs\\TestJob\":4:{s:6:\"*job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:5:\"delay\";N;}"
    }
}

后续会支持自定义 payload

@327
Copy link
Contributor Author

327 commented Jul 27, 2018

感谢,后来琢磨出来了,laravel 会默认调用CallQueuedHandler 的 fire 函数,像上面如果指定了 call 函数,那CallQueuedHandler必须声明一个 call 函数

多谢指点,已经顺利运行

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants