初识rabbitMQ

composer安装php-amqplib

配置composer.json

{
 "require": {
    "php-amqplib/php-amqplib": ">=2.6.1"
  }
}

 安装

composer install

send.php 

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('channel1', false, false, false, false);
$msg = new AMQPMessage('nicce to meet you !');
$channel->basic_publish($msg, '', 'channel1');
echo "信息发送成功!'\n";
$channel->close();
$connection->close();

receive.php

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('channel1', false, false, false, false);
echo "信息接收中.按CTRL+C可退出\n";
$callback = function ($msg) {
  echo '收到信息:', $msg->body, "\n";
};
$channel->basic_consume('channel1', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
    $channel->wait();
}

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值