hyperf | 帶你一起看 hyperf 文檔之 amqp

date: 2019-07-17 22:42:21
title: hyperf| 帶你一起看 hyperf 文檔之 amqp

hyperf 開源有一段時間了, 從開發者交流羣就能感受到熱度. 這段時間下來, 有一個明顯的現象, 某A 提了一個技術問題, 某B 直接拋一個官方文檔的對應鏈接. 這種現象實在太常見, 甚至衍生出了 歡迎進入 vip 交流羣 這樣的商機. 不得不說:

花 2 個小時認真看一遍文檔, 比遇到問題就卡住然後到處問要高效得多.

重要的事情說三遍:

  • 認真看一遍文檔
  • 認真看一遍文檔
  • 認真看一遍文檔

好了, 回到正題, 今天我們來玩 amqp.

項目準備

這些都可以在文檔中找到, 所以直接上操作.

  • 使用開發組提供的 docker

hold 不住 docker, 不用也行, 但是要能基於開發組的 Dockerfile 配置好環境, 如果既不會用 docker, 也無法自己配置好開發環境, 請一定要努力哦.

使用 docker-compose 配置的全部環境:

version: '3'
services:
    ms:
        image: hyperf/hyperf
        volumes:
            - ../:/data
        ports:
            - "9501:9501"
        environment:
            APP_ENV: dev
        tty: true
    mysql:
        image: mysql:5.7.26
        volumes:
            - ./config/my.cnf:/etc/mysql/conf.d/my.cnf
            - ./config/sql:/docker-entrypoint-initdb.d
            - ./data/mysql:/var/lib/mysql
        ports:
            - "3306:3306"
        environment:
            TZ: Asia/Shanghai
            MYSQL_ROOT_PASSWORD: root
    redis:
        image: redis:alpine
        volumes:
            - ./config/redis.conf:/etc/redis/redis.conf
            - ./data/redis:/data
        ports:
            - "6379:6379"
    rabbitmq:
        image: rabbitmq:management-alpine
        hostname: myrabbitmq
        volumes:
            - ./data/rabbitmq:/var/lib/rabbitmq/mnesia
        ports:
            - "5672:5672" # mq
            - "15672:15672" # admin

這裏多說一句, docker / Dockerfile / docker-compose 只是滿足開發環境的使用的話, 真的很簡單, 記住幾個常用的 docker 命令, 清楚 Dockerfile 幾個常用的指令(RUN CMD 等), docker-compose 只是 yaml 格式的配置文件而已.

推薦一個好習慣: 一個文檔專門記 docker / Dockerfile / docker-compose 的常用內容, 使用過程中逐漸對這個文件進行增刪查改(CRUD), 不用多久, 你就會發現自己用起 docker 來, 賊 6 !

另一個好習慣是 最佳實踐, 你要從無到有用起來很難, 但是跟着最佳實踐走, 就能又快又好 ! 當然, 再上一層樓, 你也能成爲最佳實踐.

  • 安裝項目
composer create-project hyperf/hyperf-skeleton hyperf-demo

安裝過程選擇自己需要的組件, 不清楚就先不要選, 反正之後可以通過 composer require 安裝. 其實我更想說的是:

安裝的組件自己 hold 不住, 然後到處叫, 這樣多沒意思呀.

  • 配置 composer.json
"repositories": {
    "hyperf": {
        "type": "path",
        "url": "../hyperf/src/*"
    },
    "packagist": {
        "type": "composer",
        "url": "https://mirrors.aliyun.com/composer"
    }
}

添加了 path, 從我本地加載 hyperf 組件, 方便開發, 如果不參與 hyperf 組件開發, 可以忽略這一步.

  • 添加 hyperf/amqp
# 安裝
composer require hyperf/amqp 

# 添加配置文件
php bin/hyperf.php vendor:publish hyperf/amqp
  • 修改配置, 啓動並驗證

我啓動了 mysql / redis / rabbitmq, 配置相關組件的配置, 並啓動框架進行驗證

# config
vim .env
vim config/autoload/redis.php
vim config/autoload/database.php
vim config/autoload/amqp.php

# test
php bin/hyperf.php start

好了, 項目準備好了, 正式開始擼代碼.

官方文檔 amqp demo

文檔有的, 還是直接貼:

# producer
php bin/hyperf.php gen:amqp-producer DemoProducer

# consumer
php bin/hyperf.php gen:amqp-consumer DemoConsumer

# 使用 command 盜用 DemoProducer 進行驗證
php bin/hyperf.php gen:command TestCommand

producer 發個消息:

  • 設置 command 的名字: parent::__construct('t');
  • 使用 @Inject() 註解注入
  • 發消息, 一行搞定: $this->producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s')));
<?php

declare(strict_types=1);

namespace App\Command;

use App\Amqp\Producer\DemoProducer;
use Hyperf\Amqp\Producer;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerInterface;

/**
 * @Command
 */
class TestCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @Inject()
     * @var Producer
     */
    protected $producer;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('t');
    }

    public function configure()
    {
        $this->setDescription('Hyperf Demo Command');
    }

    public function handle()
    {
        $this->producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s')));
    }
}

愉快的玩耍起來:

# produce
php bin/hyperf.php t

# consume
php bin/hyperf.php start # 會使用 swoole process 啓動 DemoConsumer

# 也可以訪問 rabbitmq admin 控制檯
http://localhost:15672

擼一擼 rabbitmq 官網 tutorial

跟着 rabbitmq 官網 tutorial, 見識一下 hyperf 中的 amqp 有多簡單

// consumer
/**
 * @Consumer()
 */
class DemoConsumer extends ConsumerMessage
{
    protected $exchange = 'hello';
    protected $type = Type::FANOUT;
    protected $queue = 'hello';

    public function consume($data): string
    {
        var_dump($data);
        return Result::ACK;
    }
}

// producer
/**
 * @Producer()
 */
class DemoProducer extends ProducerMessage
{
    protected $exchange = 'hello';
    protected $type = Type::FANOUT;
    protected $routingKey = 'hello';
    public function __construct($data)
    {
        $this->payload = $data;
    }
}

設置一下 nums 參數, 就可以多進程.

// Consumer
/**
 * @Consumer(nums=2)
 */
class DemoConsumer extends ConsumerMessage
{
    protected $exchange = 'task';
    protected $type = Type::FANOUT;
    protected $queue = 'task';

    public function consume($data): string
    {
        var_dump($data);
        return Result::ACK;
    }
}

// producer
/**
 * @Producer()
 */
class DemoProducer extends ProducerMessage
{
    protected $exchange = 'task';
    protected $type = Type::FANOUT;
    protected $routingKey = 'task';
    public function __construct($data)
    {
        $this->payload = $data;
    }
}

和上面的 hello world 一致

終於看到 routing_key 的作用了

// consumer
/**
 * @Consumer()
 */
class DemoConsumer extends ConsumerMessage
{
    protected $exchange = 'routing';
    protected $type = Type::DIRECT;
    // 這個 consumer 只消費 error 級別的日誌
    protected $queue = 'routing.error';
    protected $routingKey = 'error';

    public function consume($data): string
    {
        var_dump($data);
        return Result::ACK;
    }
}

/**
 * @Consumer()
 */
class Demo2Consumer extends ConsumerMessage
{
    protected $exchange = 'routing';
    protected $type = Type::DIRECT;
    // 這個 consumer 消費所有級別的日誌
    protected $queue = 'routing.all';
    protected $routingKey = [
        'info',
        'warning',
        'error',
    ];

    public function consume($data): string
    {
        var_dump($data);
        return Result::ACK;
    }
}

// producer
/**
 * @Producer()
 */
class DemoProducer extends ProducerMessage
{
    protected $exchange = 'routing';
    protected $type = Type::DIRECT;
    public function __construct($data, $routingKey)
    {
        $this->routingKey = $routingKey;
        $this->payload = $data;
    }
}

// produce
$this->producer->produce(new DemoProducer('info'. date('Y-m-d H:i:s'), 'info'));
$this->producer->produce(new DemoProducer('warning'. date('Y-m-d H:i:s'), 'warning'));
$this->producer->produce(new DemoProducer('error'. date('Y-m-d H:i:s'), 'error'));
var_dump('done');

和的, 和上面的 routing 差不多

// consume
/**
 * @Consumer()
 */
class DemoConsumer extends ConsumerMessage
{
    protected $exchange = 'topics';
    protected $type = Type::TOPIC;
    protected $queue = 'topics.t1';
    // protected $routingKey = '#'; // all
    // protected $routingKey = 'kern.*';
    // protected $routingKey = '*.critical';
    // protected $routingKey = 'kern.critical';
    protected $routingKey = [
        'kern.*',
        '*.critical',
    ];

    public function consume($data): string
    {
        var_dump($data);
        return Result::ACK;
    }
}

// produce
/**
 * @Producer()
 */
class DemoProducer extends ProducerMessage
{
    protected $exchange = 'topics';
    protected $type = Type::TOPIC;
    public function __construct($data, $routingKey)
    {
        $this->routingKey = $routingKey;
        $this->payload = $data;
    }
}

可以看到, 想要用 amqp, 自動生成好代碼後, 改一改屬性就成, so easy~

再聊 amqp

amqp 難不難用? 至少基礎的使用還是很好掌握的, 下面有一張圖可供參考

producer consumer connection vhost channel exchange queue routing_key bind publish consume msg 幾個概念瞭解了, 基礎使用就能很順手. 而在 hyperf 中, 得益於對一些常用使用的方式的封裝, 自動生成代碼 + 改改類屬性 就能把 amqp 用起來 !

寫在最後

無他, 唯手熟爾.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章