GithubHelp home page GithubHelp logo

mq_server's Introduction

mq服务

composer require cheng_util/mqserver

配置

$config = [
        'main_ip' => '192.168.10.5',
        'rbmq' => [
            'host' => '192.168.10.5',
            'port' => 5672,
            'user' => 'guest',
            'pass' => 'guest',
            'vhost' => '/',
            'debug' => true,
        ],
        'coroutine' => true,    //使用swoole协程处理mq接收到的数据
        'swoole_udp' => [   //swoole upd的配置
            'port' => 9502,
            'host' => '127.0.0.1'
        ]
    ];

启动消费服务

/**
 * 默认消费队列,纯rbmq处理
 */
 
require_once __DIR__.'/../../../vendor/autoload.php';
ConsumeServer::getInstance()->setConfig($config);
ConsumeServer::getInstance()->startDefaultConsume(function($message){   //使用闭包 config的coroutine必须为false
            $message->ack();
            var_dump(json_decode($message->body, true));
        });
        

/**
 * 延时队列,纯rbmq处理
 */
 
require_once __DIR__.'/../../../vendor/autoload.php';
ConsumeServer::getInstance()->setConfig($config);
        ConsumeServer::getInstance()->startDelayConsume(function($message){   //使用闭包 config的coroutine必须为false
            $message->ack();
            var_dump($message->body);
        });
        

发送mq消息

require_once __DIR__.'/../../../vendor/autoload.php';
ProducerServer::getInstance()->setConfig($config);

$time = time();
//发送普通消息 第二个参数是延时,0则为普通数据
$res = ProducerServer::getInstance()->pushMessage([
    'type' => 'default_message',
    'msg' => '普通内容',
    'time_stamp' => $time,
    'time_date' => date('Y-m-d H:i:s', $time)
], 3, true);

var_dump($res);


//协程发消息
ProducerServer::getInstance()->pushMessageCoroutine([
                'type' => 'deafult_message',
                'msg' => '普通内容',
                'time_stamp' => $time,
                'time_date' => date('Y-m-d H:i:s', $time)
            ]);

单独发送udp消息,不经过mq

// 单独发udp消息
SwooleUdpClientService::getInstance()->setConfig($config);
$body = SwooleUdpClientService::getInstance()->buildBody('ccc', ['asdf' => date('Y-m-d H:i:s')]);
SwooleUdpClientService::getInstance()->CoroutineSend($body);

demo说明:

1、普通消费

DemoDefaultConsumeServer.php 启动普通消费队列

DemoDelayConsumeServer 启动延时队列

2、swoole的协程消费

先启动对应的mq服务,普通或者延时,然后启动DemoSwooleUdpServer.php 这个udp协程处理

mq_server's People

Contributors

cc2415 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.