2019-03-08 12:04:46 +08:00
# AMQP
2019-10-22 09:35:48 +08:00
[https://github.com/hyperf/amqp ](https://github.com/hyperf/amqp )
2019-03-08 12:04:46 +08:00
## Default Config
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
< ?php
return [
'default' => [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/',
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
],
'params' => [
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
'context' => null,
'keepalive' => false,
'heartbeat' => 0,
],
],
];
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
## Deliver Message
Use generator command to create a producer.
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
php bin/hyperf.php gen:amqp-producer DemoProducer
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
We can modify the Producer annotation to replace exchange and routingKey.
Payloload is the data that is finally delivered to the message queue, so we can rewrite the _construct method easyly,just make sure payload is assigned.
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
< ?php
declare(strict_types=1);
namespace App\Amqp\Producers;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use App\Models\User;
/**
* DemoProducer
* @Producer (exchange="hyperf", routingKey="hyperf")
*/
class DemoProducer extends ProducerMessage
{
public function __construct($id)
{
$user = User::where('id', $id)->first();
$this->payload = [
'id' => $id,
'data' => $user->toArray()
];
}
}
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
Get the Producer instance through container, and you can deliver the message. It is not reasonable for the following examples to use Application Context directly to get the Producer. For the specific use of container, see the di module.
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
< ?php
use Hyperf\Amqp\Producer;
use App\Amqp\Producers\DemoProducer;
use Hyperf\Utils\ApplicationContext;
$message = new DemoProducer(1);
$producer = ApplicationContext::getContainer()->get(Producer::class);
$result = $producer->produce($message);
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
## Consume Message
Use generator command to create a consumer.
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
php bin/hyperf.php gen:amqp-consumer DemoConsumer
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
we can modify the Consumer annotation to replace exchange, routingKey and queue.
And $data is parsed metadata.
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
< ?php
declare(strict_types=1);
namespace App\Amqp\Consumers;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
/**
* @Consumer (exchange="hyperf", routingKey="hyperf", queue="hyperf", nums=1)
*/
class DemoConsumer extends ConsumerMessage
{
public function consume($data): string
{
print_r($data);
return Result::ACK;
}
}
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
The framework automatically creates the process according to Consumer annotations, and the process will be pulled up again after unexpected exit.