rabbitMQ PHP 链接实例

<?php
final class RabbitMQ{
//服务器地址
private $_host;
//服务端口
private $_port;
//路由名称
private $_rout;
//用户名
private $_login;
//密码
private $_password;
//虚拟机名称
private $_vhost ;
//交换机名称
private $_exchangename;
//交换机flags
private $_flags;
//队列名称
private $_queue;
//当前类对象
private static $_obj;
//MQ服务对象
private $_MQ;
//MQ通道对象
private $_channel;
//MQ路由对象
private $_exchange;
//MQ队列对象
private $_queueobj;
private $_queueFlags;
private function __construct(){

}

private function __clone(){

}
/**
* 配置文件设置
* @param array
*/
public function config($param){
$this->_host = $param["host"];
$this->_port = $param["port"];
$this->_login = $param["user_name"];
$this->_password = $param["password"];
$this->_queue = $param["queue_name"];
$this->_route_key = $param["route_key"];
$this->_exchangename = $param["exchangename"];
$this->_vhost = $param["vhost"];
$this->_exchangeType = $param["exchangeType"];
$this->_exchangeFlags = $param["flags"];
$this->_queueFlags = $param["flags"];
}

/**
* 获取当前类对象实现单例
*/
public static function init(){
if(!self::$_obj instanceof self){
self::$_obj = new self;
}

return self::$_obj;
}

public function connect(){
$config = array(
"host" => $this->_host,
"port" => $this->_port,
"login" => $this->_login,
"password" => $this->_password,
"vhost" => $this->_vhost,
);
//创建服务器链接对象
$this->_MQ = new AMQPConnection($config);
if (!$this->_MQ->connect()) {
throw new Exception("链接MQ服务失败", 400);
}
//创建通道链接对象
$this->_channel = new AMQPChannel($this->_MQ);
//创建路由对象
$this->_exchange = new AMQPExchange($this->_channel);
//设置交换机名称
if(!empty($this->_exchangename))
$this->_exchange->setName($this->_exchangename);
//设置交换机类型
if(!empty($this->_exchangeType))
$this->_exchange->setType($this->_exchangeType);
//设置交换机flags
if(!empty($this->_exchangeFlags))
$this->_exchange->setFlags($this->_exchangeFlags);
//创建交换机
$this->_exchange->declareExchange();
//创建队列对象
$this->_queueobj = new AMQPQueue($this->_channel);
//设置队列名称
$this->_queueobj ->setName($this->_queue);
//设置队列flags;
$this->_queueobj->setFlags($this->_queueFlags);
//创建队列
$this->_queueobj ->declareQueue();
//将队列和交换机绑定道路由key
$this->_queueobj ->bind($this->_exchangename,$this->_route_key);

}

/**
* 发布消息
*/
public function publish($content){
$this->_exchange->publish($content,$this->_route_key);
}

/**
* 获取消息
*/
public function getMsg(){
$info = $this->_queueobj->get(AMQP_AUTOACK)->getBody();
return $info;
}


}

 

 

##########################

#########    类调用    #########

##########################

 


$config = array(
"host" => "10.100.13.142",
"port" => "5672",
"user_name" => "gedai",
"password" => "*****",
"queue_name" => "contract_request",
"route_key" => "contract_request",
"vhost" => "/credithc",
"exchangeType" => AMQP_EX_TYPE_DIRECT,
"flags" => AMQP_DURABLE,
"exchangename" => "CREDITHC_CS"
);
header("Content-type:text/html;charset=utf-8");
include_once("RabbitMQ.class.php");
try{
$MQ = RabbitMQ :: init();
$MQ -> config($config);
$MQ -> connect();
$MQ -> publish("test");
$ret = $MQ -> getMsg();
print_r($ret);
}catch(Exception $e){
var_dump($e);
}

 

转载于:https://www.cnblogs.com/feiyujinghong/p/6282551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值