vu / zf2-amqp-carapace
ZF2 PHP Wrapper for Vu/AMQPCarapace
Requires
- php: >=5.4
- vu/amqp-carapace: 0.1.0
- vu/zf2-test-extensions: 1.0
- zendframework/zend-servicemanager: 2.*
- zendframework/zend-stdlib: 2.*
Requires (Dev)
- phake/phake: v1.0.3
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-24 07:32:20 UTC
README
简介
Zf2AMQPCarapace 是 "vuhl/AMQPCarapace" 的包装器,它使得在 PHP 中使用 AMQP 非常简单。请参考下面的示例以亲自体验。
重要说明
- 目前 Zf2AMQPCarapace 只具备发布功能。消费和其他功能可能在将来添加。
- 目前不支持消息上的强制标志。您应该保持此标志为 false,否则将抛出异常。
使用 Composer 安装
将 "vu/zf2-amqp-carapace" 添加到您的 composer.json 文件中的 require 部分,并运行相应的安装或更新。有关 Composer 的更多信息,请访问 他们的网站。
基本用法
设置配置
在您的 Zf2 配置文件之一中,添加连接、传输、消息和应用程序的基本配置。您可以为每个类别设置任意数量的设置。
- 连接:与创建 AMQP 连接相关的设置
- 传输:定义特定交换的设置,如要触发的交换、路由键等
- 消息:基本预定义消息设置
- 应用程序:为特定应用程序预定义连接、传输和消息设置。这使用您已定义的连接、传输和消息设置。
示例
'AMQPCarapace' => [ 'connection' => [ 'connection_rabbit' => [ 'host' => '0.0.0.0', 'port' => 0000, 'user' => 'username', 'password' => 'password', ] ], 'transport' => [ 'php_exchange' => [ 'exchange' => 'php_exchange', 'routing_key' => 'php_test', ], 'other_exchange' => [ 'exchange' => 'other_php_exchange', 'routing_key' => 'rawr' ] ], 'message' => [ 'text/plain' => [ 'content_type' => 'text/plain', 'content_encoding' => 'UTF-8', ], 'json' => [ 'content_type' => 'application/json', 'content_encoding' => 'UTF-8' ] ], 'application' => [ //Every application config MUST include a valid connection, transport, and message config name 'MyApp' => [ 'connection' => 'connection_rabbit', 'transport' => 'php_exchange', 'message' => 'text/plain' ], 'AnotherApp' => [ 'connection' => 'connection_rabbit', 'transport' => 'other_exchange', 'message' => 'json' ] ] ]
创建 AMQPPublisher
发布消息最简单的方法是使用 PublisherFactory。PublisherFactory 的主要目的是使用配置中 'application' 部分的预定义设置来创建 AMQPPublisher。不过,您也可以传入自定义设置来创建 AMQPPublisher。示例
//Easiest way using predefined application settings $publisher_factory = new Vu\Zf2AMQPCarapace\Publisher\PublisherFactory(); $myapp_amqp_publisher = $publisher_factory->getPublisher('MyApp'); //You may also create an AMQPPublisher manually using Connection, Transport, and Message objects $connection_factory = new Vu\Zf2AMQPCarapace\Factory\Connection(); $amqp_connection = $connection_factory->create('connection_rabbit'); //Uses predefined connection config //Create transport and message objects using Transport and Message factories in the same manner... //More information on Connection, Transport, and Message factories are listed further down on this page $manual_amqp_publisher = $publisher_factory->makePublisher($amqp_connection, $amqp_transport, $amqp_message); //Lastly, you may create an AMQPPublisher by passing in an array of configuration $config = [ 'connection' => [/* connection settings */], 'transport' => [/* transport settings */], 'message' => [/* message settings */] ]; $manual_amqp_publisher = $publisher_factory->makePublisherFromConfiguration($config);
使用 AMQPPublisher
一旦您设置了 AMQPPublisher,您将能够轻松地将消息发布到您定义的任何交换。AMQPPublisher 将为您创建和关闭 AMQP 通道。
//Publish a single message $my_first_message = "This will be the first message I publish!"; $myapp_amqp_publisher->basicPublish($my_first_message); $myapp_amqp_publisher->basicPublish("I also want to publish this message"); //Publish multiple messages $array_of_strings = [ "First!", "Second...", "Tres!!" ]; $myapp_amqp_publisher->batchBasicPublish($array_of_strings);
链式调用 PublisherFactory 和 AMQPPublisher
PublisherFactory 和 AMQPPublisher 可以链式调用。
$publisher_factory->getPublisher('AnotherApp')->basicPublish("Here is my message!");
使用连接、传输和消息工厂
连接、传输和消息工厂都实现了 IFactory 接口,该接口指定了两个方法 - create 和 createFromArray。它们将返回相应的 AMQPCarapace 连接、传输或消息对象。
-
create - 从预定义的配置中创建,如上所述。示例
$factory->create('config_name');
-
createFromArray - 从传入的数组中创建配置。示例
$factory->createFromArray([/* config settings*/]);
AMQPCarapace
有关独立 AMQPCarapace 的更多信息,请参阅 vuhl\AMQPCarapace 页面