exchange的使用BROKER_URL只能是rabbitmq,redis使用exchange和未使用效果一样,只能为direct
1、fanout 广播式,它不需要指定路由就会把所有发送到该Exchange的消息路由到所有与它绑定的Queue中
配置文件如下,可以套用我的celery queue文章,来测试
CELERY_QUEUES = (
Queue('for_adds',Exchange('for_adds',type='fanout')),
Queue('for_send_emails', Exchange('for_adds',type='fanout')),
Queue('add', Exchange('for_adds',type='fanout')),
)
CELERY_ROUTES = {
'celery_test.tasks.add': {'exchange':'for_adds'},
'celery_test.tasks.send_mail': {'exchange':'for_adds',},
'celery_test.tasks.adds': {'exchange':'for_adds',},
}
2、direct
direct类型的Exchange路由规则也很简单,它会把消息路由到那些binding key与routing key完全匹配的Queue中
CELERY_QUEUES = (
Queue('for_adds',Exchange('for_adds',type='direct'), routing_key='adds'),
Queue('for_send_emails', Exchange('for_adds',type='direct'), routing_key='email'),
Queue('add', Exchange('for_adds',type='direct'), routing_key='add'),
)
CELERY_ROUTES = {
'celery_test.tasks.add': {'exchange':'for_adds','routing_key':'add'},
'celery_test.tasks.send_mail': {'exchange':'for_adds','routing_key':'email'},
'celery_test.tasks.adds': {'exchange':'for_adds','routing_key':'add'},
}
3、topic
topic类型的Exchange在匹配规则上进行了扩展,它与direct类型的Exchage相似,也是将消息路由到binding key与routing key相匹配的Queue中,但这里的匹配规则有些不同,它约定
routing key为一个句点号“. ”分隔的字符串(我们将被句点号“. ”分隔开的每一段独立的字符串称为一个单词),如“*.task.*”、"*.*.email"、“*.add”
binding key与routing key一样也是句点号“. ”分隔的字符串
binding key中可以存在两种特殊字符“*”与“#”,用于做模糊匹配,其中“*”用于匹配一个单词,“#”用于匹配多个单词(可以是零个)
配置文件如下
CELERY_QUEUES = (
Queue('for_adds',Exchange('for_adds',type='topic'), routing_key='*.task.*'),
Queue('for_send_emails', Exchange('for_adds',type='topic'), routing_key='*.*.email'),
Queue('add', Exchange('for_adds',type='topic'), routing_key='*.add'),
)
CELERY_ROUTES = {
'celery_test.tasks.add': {'exchange':'for_adds','routing_key':'q.task.email'},
'celery_test.tasks.send_mail': {'exchange':'for_adds','routing_key':'a.task.e'},
'celery_test.tasks.adds': {'exchange':'for_adds','routing_key':'b.add'},
}
该路由的设置,adds会到add队列,send_mail会到for_adds队列,add会到for_adds和for_send_emails队列