【问题标题】:Using RabbitMQ with Plone - Celery or not?将 RabbitMQ 与 Plone 一起使用 - Celery 与否?
【发布时间】:2014-07-08 15:51:31
【问题描述】:

我希望我把它发布在正确的地方。

我正在研究 RabbitMQ 在我们的 Plone 站点中的潜在用途。我们目前在 Plone 服务器中的专用工作客户端上使用异步,但我们正在考虑构建一个专用的 RabbitMQ 服务器来处理所有 Plone 消息传递和其他活动。

我的具体问题是,在 Plone 中使用 Celery 与 RabbitMQ 一起工作与仅使用 RabbitMQ 相比有什么优势?我为 Celery 集成找到了 this plone add-on,但不确定这是否是最佳途径。我注意到 Celery 有用于监控队列的 Flower 工具,这将是一个巨大的优势。

作为一个附带问题,如果您愿意,是否有人有任何关于将 RabbitMQ 与 Plone 集成以处理所有这些请求的提示或参考?我一直在做研究,并获得了 RabbitMQ 的一般要点,但我似乎无法与 Plone 活动建立联系,例如 Content Rules 和 PloneFormGen 提交。到目前为止,我看到 this add-on 我要安装并看看我是否能弄清楚,但如果可以的话,我只是想获得一些指导。

感谢您的宝贵时间!

【问题讨论】:

    标签: python rabbitmq plone celery


    【解决方案1】:

    首先,问问自己,是否需要 RabbitMQ 的功能,或者只是想用 Plone 在 Python 中做一些异步任务。

    如果您真的不需要 RabbitMQ,您可以查看 David Glick 关于如何将 Celery 与 Plone 集成的要点(并且仍然将 RabbitMQ 与 Celery 一起使用):

    您也可以查看collective.taskqueue(没有 Celery 和 RabbitMQ 的简单队列),但它还没有提供任何监控解决方案。

    如果你真的需要 RabbitMQ,跳过 Celery,试试collective.zamqp。 Celery 试图自己成为代理,并且会阻止您使用大多数 AMQP 和 RabbitMQ 的内置功能。

    RabbitMQ 附带用于监控的出色 Web 管理插件,还有用于 3rd 方监控系统(如 Zenoss)的插件。

    很抱歉 collective.zamqp 仍然缺少叙述性文档,但您可以查看 collective.zamqpdemo 以获取其配置和使用的各种示例。

    简而言之,c.zamqp 允许您根据生产者和消费者定义配置代理使用:

    from five import grok
    from zope.interface import Interface
    from collective.zamqp.producer import Producer
    from collective.zamqp.consumer import Consumer
    
    
    class CreateItemProducer(Producer):
        """Produces item creation requests"""
        grok.name("amqpdemo.create")  # is also used as default routing key
    
        connection_id = "superuser"
        serializer = "msgpack"
        queue = "amqpdemo.create"
    
        durable = False
    
    
    class ICreateItemMessage(Interface):
        """Marker interface for item creation message"""
    
    
    class CreateItemConsumer(Consumer):
        """Consumes item creation messages"""
        grok.name("amqpdemo.create")  # is also used as the queue name
    
        connection_id = "superuser"
        marker = ICreateItemMessage
    
        durable = False
    

    通过事务绑定生产者发布消息(仅在事务成功后发布消息):

        import uuid
    
        from zope.component import getUtility
        from collective.zamqp.interfaces import IProducer
    
        producer = getUtility(IProducer, name="amqpdemo.create")
        producer._register()  # register to bound to successful transaction
    
        message = {"title": u"My title"}
    
        producer.publish(message)
    

    并在熟悉的内容事件处理程序环境中使用消息:

    from zope.component.hooks import getSite
    from collective.zamqp.interfaces import IMessageArrivedEvent
    from plone.dexterity.utils import createContentInContainer
    
    @grok.subscribe(ICreateItemMessage, IMessageArrivedEvent)
    def createItem(message, event):
        """Consume item creation message"""
    
        portal = getSite()
        obj = createContentInContainer(
            portal, "Document", checkConstraints=True, **message.body)
    
        message.ack()
    

    最后,它将代理连接配置与代码解耦,实际连接参数可以在 buildout.cfg 中定义(允许所需数量的消费实例):

    [instance]
    recipe = plone.recipe.zope2instance
    ...
    zope-conf-additional =
        %import collective.zamqp
        <amqp-broker-connection>
             connection_id superuser
             heartbeat 120
    # These are defaults, but can be defined when required:     
    #        hostname localhost
    #        virtual_host /
    #        username guest
    #        password guest
        </amqp-broker-connection>
        <amqp-consuming-server>
            connection_id superuser
            site_id Plone
            user_id admin
            vhm_method_prefix /VirtualHostBase/https/example.com:443/Plone/VirtualHostRoot
        </amqp-consuming-server>
    

    c.zamqp 不能直接从 RestrictedPython 调用,因此将其集成到 PloneFormGen 需要自定义操作适配器或自定义外部方法,以便从 PFG 的 Python 脚本适配器调用。

    【讨论】:

    • 感谢您花时间提供如此详细的解释!我想我明白了你在这里所说的大部分内容......那么内容规则呢?如果我想路由通过 RabbitMQ 触发的所有内容规则电子邮件,是否需要创建自定义 CR 操作,或者是否有其他连接方式?我想我只是不知道如何让 Plone 活动真正使用 Rabbit。顺便说一句,我第一次尝试使用 c.zamqp。
    • 我没有确定的电子邮件发送队列解决方案。您当然可以创建自定义 CR 操作,但 Products.MailHost 和 zope.sendmail 中已经支持排队发送电子邮件。因此,至少在理论上,您可以创建自己的电子邮件队列,它将电子邮件传递到 RabbitMQ(使用 c.zamqp 或仅使用 rabbitpy-library 命令,因为 MailHost 已经具有电子邮件事务管理器)。然而,为此你应该仔细阅读 Products.MailHost 和 zope.sendmail 并查看 Plone root 中的 MailHost 工具。
    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2015-10-19
    相关资源
    最近更新 更多