暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Canal 直接在laravel里面安装使用了

suger 2022-10-05
333
# 安装组件canal-php

 composer require xingwenge/canal_php

# 编写脚本监听

<?php

 

namespace App\\\\Console\\\\Commands;

use xingwenge\\\\canal_php\\\\CanalClient;

use xingwenge\\\\canal_php\\\\CanalConnectorFactory;

use xingwenge\\\\canal_php\\\\Fmt;

use Illuminate\\\\Console\\\\Command;

 

ini_set('display_errors', 'On');

error_reporting(E_ALL);

class CanalDemo extends Command

{

    /**

     * The name and signature of the console command.

     *

     * @var string

     */

    protected $signature = 'CanalDemo';

 

    /**

     * The console command description.

     *

     * @var string

     */

    protected $description = '测试canal';

 

    /**

     * Create a new command instance.

     *

     * @return void

     */

    public function __construct()

    {

        parent::__construct();

    }

 

    /**

     * Execute the console command.

     *

     * @return mixed

     */

    public function handle()

    {

        try {

            $client = CanalConnectorFactory::createClient(CanalClient::TYPE_SOCKET_CLUE);

            # $client = CanalConnectorFactory::createClient(CanalClient::TYPE_SWOOLE);

 

            $client->connect("172.17.0.5", 11111);//此处修改为自己的配置

            $client->checkValid();

            $client->subscribe("1001", "test", ".*\\\\\\\\..*");//对应启动容器时test的队列名

            while (true) {

                $message = $client->get(100);

                if ($entries = $message->getEntries()) {

                    foreach ($entries as $entry) {

                        Fmt::println($entry);

                    }

                }

                sleep(1);

            }

 

            $client->disConnect();

        } catch (\\\\Exception $e) {

            echo $e->getMessage(), PHP_EOL;

        }

 

    }

 

}
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论