参考文档:
http://www.cnblogs.com/phpinfo/p/4104551...
http://blog.csdn.net/historyasamirror/ar...

依赖包安装

yum install ncurses-devel unixODBC unixODBC-devel

erlang环境

wget http://erlang.org/download/otp_src_18.1.tar.gz
tar -zxvf otp_src_18.1.tar.gz
cd otp_src_18.1
./configure --prefix=/usr/local/erlang
make
make install # 配置erlang环境变量
vim /etc/profile
# 增加内容:
export PATH="$PATH:/usr/local/erlang/bin" # 保存退出,并刷新变量
source /etc/profile # 测试erlang是否安装成功
# 安装完成以后,执行erl看是否能打开eshell,用’halt().’退出,注意后面的点号,那是erlang的结束符。
[root@localhost src]# erl
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false] Eshell V6.1 (abort with ^G)
2> 9+3.
12
3> halt().

安装rabbitmq依赖文件,安装rabbitmq

# 安装rabbitmq依赖包
yum install xmlto # 安装rabbitmq服务端
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.7/rabbitmq-server-3.5.7.tar.gz
tar zxvf rabbitmq-server-3.5.7.tar.gz
cd rabbitmq-server-3.5.7/
make
make install TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/local/rabbitmq/man DOC_INSTALL_DIR=/usr/local/rabbitmq/doc # 配置hosts
vim /etc/hosts
# 增加一行内容
# 当前IP地址 绑定HOSTNAME名称(vim /etc/sysconfig/network)
192.168.2.208 localhost.localdomain # 这种会提示错误(Warning: PID file not written; -detached was passed.)
/usr/local/rabbitmq/sbin/rabbitmq-server -detached 启动rabbitmq
/usr/local/rabbitmq/sbin/rabbitmqctl status 查看状态
/usr/local/rabbitmq/sbin/rabbitmqctl stop 关闭rabbitmq # 目前我自己使用
/usr/local/rabbitmq/sbin/rabbitmq-server start & 启动rabbitmq
/usr/local/rabbitmq/sbin/rabbitmqctl status 查看状态
/usr/local/rabbitmq/sbin/rabbitmqctl stop 关闭rabbitmq

启用管理插件

mkdir /etc/rabbitmq
/usr/local/rabbitmq/sbin/rabbitmq-plugins list 查看插件列表
/usr/local/rabbitmq/sbin/rabbitmq-plugins enable rabbitmq_management (启用插件)
/usr/local/rabbitmq/sbin/rabbitmq-plugins disable rabbitmq_management (禁用插件) # 重启rabbitmq
# 访问 http://127.0.0.1:15672/ # 如果有iptables
vim /etc/sysconfig/iptables # 增加一下内容
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15672 -j ACCEPT # 重启动iptable
service iptables restart

开机自启动配置

#!/bin/sh
#start rabbitMq
sudo /usr/local/rabbitmq/sbin/rabbitmq-server & > /usr/local/rabbitmq/logs/rabbitmq.log 2>&1

RabbitMQ PHP扩展安装

# 安装rabbitmq-c依赖包
yum install libtool autoconf # 安装rabbitmq-c ( 最好下载 0.5的,0.6安装可能会报错)
# 版本下载:https://github.com/alanxz/rabbitmq-c/releases/tag/v0.5.0
wget https://github.com/alanxz/rabbitmq-c/releases/download/v0.5.0/rabbitmq-c-0.5.0.tar.gz
tar -zxvf v0.5.0
cd rabbitmq-c-0.5.0/
autoreconf -i
./configure --prefix=/usr/local/rabbitmq-c
make
make install # 安装PHP扩展 amqp
wget http://pecl.php.net/get/amqp-1.6.1.tgz
tar zxvf amqp-1.6.1.tgz
cd amqp-1.6.1
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c
make
make install # 编辑php.ini文件,增加amqp扩展支持
vim /usr/local/php/etc/php.ini # 增加下面内容
; rabbitmq扩展支持
extension=amqp.so # 重启php-fpm
/etc/init.d/php-fpm restart

验证是否成功 phpinfo()查看下是否支持amqp扩展


相关配置

hostname mq        // 设置hostname名称
vim /etc/sysconfig/network // 设置hostname
vim /etc/hosts // 编辑hosts
./rabbitmqctl add_user admin admin // 添加用户
./rabbitmqctl set_user_tags admin administrator // 添加admin 到 administrator分组
./rabbitmqctl set_permissions -p / admin "*." "*." "*." // 添加权限

创建配置文件

#在/usr/rabbitmq/sbin/rabbitmq-defaults 查看config文件路径
# 创建配置文件
touch/usr/rabbitmq/sbin
#vm_memory_high_watermark 内存低水位线,若低于该水位线,则开启流控机制,阻止所有请求,默认值是0.4,即内存总量的40%,
#vm_memory_high_watermark_paging_ratio 内存低水位线的多少百分比开始通过写入磁盘文件来释放内存
vi /usr/rabbitmq/sbin/rabbitmq.config 输入
[
{rabbit, [{vm_memory_high_watermark_paging_ratio, 0.75},
{vm_memory_high_watermark, 0.7}]}
].

创建环境文件

touch /etc/rabbitmq/rabbitmq-env.conf
#输入
RABBITMQ_NODENAME=FZTEC-240088 节点名称
RABBITMQ_NODE_IP_ADDRESS=127.0.0.1 监听IP
RABBITMQ_NODE_PORT=5672 监听端口
RABBITMQ_LOG_BASE=/data/rabbitmq/log 日志目录
RABBITMQ_PLUGINS_DIR=/data/rabbitmq/plugins 插件目录
RABBITMQ_MNESIA_BASE=/data/rabbitmq/mnesia 后端存储目录

操作命令

 查看exchange信息
/usr/rabbitmq/sbin/rabbitmqctl list_exchanges name type durable auto_delete arguments 查看队列信息
/usr/rabbitmq/sbin/rabbitmqctl list_queues name durable auto_delete messages consumers me
查看绑定信息
/usr/rabbitmq/sbin/rabbitmqctl list_bindings
查看连接信息
/usr/rabbitmq/sbin/rabbitmqctl list_connections

php的server端脚本

<?php
$routingkey='key';
//设置你的连接
$conn_args = array('host' => 'localhost', 'port' => '5672', 'login' => 'guest', 'password' => 'guest');
$conn = new AMQPConnection($conn_args);
if ($conn->connect()) {
echo "Established a connection to the broker \n";
}
else {
echo "Cannot connect to the broker \n ";
}
//你的消息
$message = json_encode(array('Hello World3!','php3','c++3:'));
//创建channel
$channel = new AMQPChannel($conn);
//创建exchange
$ex = new AMQPExchange($channel);
$ex->setName('exchange');//创建名字
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE);
//$ex->setFlags(AMQP_AUTODELETE);
//echo "exchange status:".$ex->declare();
echo "exchange status:".$ex->declareExchange();
echo "\n";
for($i=0;$i<100;$i++){
if($routingkey=='key2'){
$routingkey='key';
}else{
$routingkey='key2';
}
$ex->publish($message,$routingkey);
}
/*
$ex->publish($message,$routingkey);
创建队列
$q = new AMQPQueue($channel);
设置队列名字 如果不存在则添加
$q->setName('queue');
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
echo "queue status: ".$q->declare();
echo "\n";
echo 'queue bind: '.$q->bind('exchange','route.key');
将你的队列绑定到routingKey
echo "\n"; $channel->startTransaction();
echo "send: ".$ex->publish($message, 'route.key'); //将你的消息通过制定routingKey发送
$channel->commitTransaction();
$conn->disconnect();
*/

php客户端脚本

<?php
$bindingkey='key2';
//连接RabbitMQ
$conn_args = array( 'host'=>'127.0.0.1' , 'port'=> '5672', 'login'=>'guest' , 'password'=> 'guest','vhost' =>'/');
$conn = new AMQPConnection($conn_args);
$conn->connect();
//设置queue名称,使用exchange,绑定routingkey
$channel = new AMQPChannel($conn);
$q = new AMQPQueue($channel);
$q->setName('queue2');
$q->setFlags(AMQP_DURABLE);
$q->declare();
$q->bind('exchange',$bindingkey);
//消息获取
$messages = $q->get(AMQP_AUTOACK) ;
if ($messages){
var_dump(json_decode($messages->getBody(), true ));
}
$conn->disconnect();
?>

翻译了部分mq常量设置,不正确的地方,大家以试验为准

全选复制放进笔记
/**
* Passing in this constant as a flag will forcefully disable all other flags.
* Use this if you want to temporarily disable the amqp.auto_ack ini setting.
* 传递这个参数作为标志将完全禁用其他标志,如果你想临时禁用amqp.auto_ack设置起效
*/
define('AMQP_NOPARAM', 0); /**
* Durable exchanges and queues will survive a broker restart, complete with all of their data.
* 持久化交换机和队列,当代理重启动后依然存在,并包括它们中的完整数据
*/
define('AMQP_DURABLE', 2); /**
* Passive exchanges and queues will not be redeclared, but the broker will throw an error if the exchange or queue does not exist.
* 被动模式的交换机和队列不能被重新定义,但是如果交换机和队列不存在,代理将扔出一个错误提示
*/
define('AMQP_PASSIVE', 4); /**
* Valid for queues only, this flag indicates that only one client can be listening to and consuming from this queue.
* 仅对队列有效,这个人标志定义队列仅允许一个客户端连接并且从其消费消息
*/
define('AMQP_EXCLUSIVE', 8); /**
* For exchanges, the auto delete flag indicates that the exchange will be deleted as soon as no more queues are bound
* to it. If no queues were ever bound the exchange, the exchange will never be deleted. For queues, the auto delete
* flag indicates that the queue will be deleted as soon as there are no more listeners subscribed to it. If no
* subscription has ever been active, the queue will never be deleted. Note: Exclusive queues will always be
* automatically deleted with the client disconnects.
* 对交换机而言,自动删除标志表示交换机将在没有队列绑定的情况下被自动删除,如果从没有队列和其绑定过,这个交换机将不会被删除.
* 对队列而言,自动删除标志表示如果没有消费者和你绑定的话将被自动删除,如果从没有消费者和其绑定,将不被删除,独占队列在客户断
* 开连接的时候将总是会被删除
*/
define('AMQP_AUTODELETE', 16); /**
* Clients are not allowed to make specific queue bindings to exchanges defined with this flag.
* 这个标志标识不允许自定义队列绑定到交换机上
*/
define('AMQP_INTERNAL', 32); /**
* When passed to the consume method for a clustered environment, do not consume from the local node.
* 在集群环境消费方法中传递这个参数,表示将不会从本地站点消费消息
*/
define('AMQP_NOLOCAL', 64); /**
* When passed to the {@link AMQPQueue::get()} and {@link AMQPQueue::get()} methods as a flag,
* the messages will be immediately marked as acknowledged by the server upon delivery.
* 当在队列get方法中作为标志传递这个参数的时候,消息将在被服务器输出之前标志为acknowledged (已收到)
*/
define('AMQP_AUTOACK', 128); /**
* Passed on queue creation, this flag indicates that the queue should be deleted if it becomes empty.
* 在队列建立时候传递这个参数,这个标志表示队列将在为空的时候被删除
*/
define('AMQP_IFEMPTY', 256); /**
* Passed on queue or exchange creation, this flag indicates that the queue or exchange should be
* deleted when no clients are connected to the given queue or exchange.
* 在交换机或者队列建立的时候传递这个参数,这个标志表示没有客户端连接的时候,交换机或者队列将被删除
*/
define('AMQP_IFUNUSED', 512); /**
* When publishing a message, the message must be routed to a valid queue. If it is not, an error will be returned.
* 当发布消息的时候,消息必须被正确路由到一个有效的队列,否则将返回一个错误
*/
define('AMQP_MANDATORY', 1024); /**
* When publishing a message, mark this message for immediate processing by the broker. (High priority message.)
* 当发布消息时候,这个消息将被立即处理.
*/
define('AMQP_IMMEDIATE', 2048); /**
* If set during a call to {@link AMQPQueue::ack()}, the delivery tag is treated as "up to and including", so that multiple
* messages can be acknowledged with a single method. If set to zero, the delivery tag refers to a single message.
* If the AMQP_MULTIPLE flag is set, and the delivery tag is zero, this indicates acknowledgement of all outstanding
* messages.
* 当在调用AMQPQueue::ack时候设置这个标志,传递标签将被视为最大包含数量,以便通过单个方法标示多个消息为已收到,如果设置为0
* 传递标签指向单个消息,如果设置了AMQP_MULTIPLE,并且传递标签是0,将所有未完成消息标示为已收到
*/
define('AMQP_MULTIPLE', 4096); /**
* If set during a call to {@link AMQPExchange::bind()}, the server will not respond to the method.The client should not wait
* for a reply method. If the server could not complete the method it will raise a channel or connection exception.
* 当在调用AMQPExchange::bind()方法的时候,服务器将不响应请求,客户端将不应该等待响应,如果服务器无法完成该方法,将会抛出一个异常
*/
define('AMQP_NOWAIT', 8192); /**
* If set during a call to {@link AMQPQueue::nack()}, the message will be placed back to the queue.
* 如果在调用AMQPQueue::nack方法时候设置,消息将会被传递回队列
*/
define('AMQP_REQUEUE', 16384); /**
* A direct exchange type.
* direct类型交换机
*/
define('AMQP_EX_TYPE_DIRECT', 'direct'); /**
* A fanout exchange type.
* fanout类型交换机
*/
define('AMQP_EX_TYPE_FANOUT', 'fanout'); /**
* A topic exchange type.
* topic类型交换机
*/
define('AMQP_EX_TYPE_TOPIC', 'topic'); /**
* A header exchange type.
* header类型交换机
*/
define('AMQP_EX_TYPE_HEADERS', 'headers'); /**
* socket连接超时设置
*/
define('AMQP_OS_SOCKET_TIMEOUT_ERRNO', 536870947);

 

 https://segmentfault.com/a/1190000004627137

RabbitMQ+PHP 消息队列环境配置的更多相关文章

  1. RabbitMQ分布式消息队列服务器(一、Windows下安装和部署)

    RabbitMQ消息队列服务器在Windows下的安装和部署-> 一.Erlang语言环境的搭建 RabbitMQ开源消息队列服务是使用Erlang语言开发的,因此我们要使用他就必须先进行Erl ...

  2. C#调用RabbitMQ实现消息队列

    前言 我在刚接触使用中间件的时候,发现,中间件的使用并不是最难的,反而是中间件的下载,安装,配置才是最难的. 所以,这篇文章我们从头开始学习RabbitMq,真正的从头开始. 关于消息队列 其实消息队 ...

  3. rabbitmq学习(二):rabbitmq(消息队列)的作用以及rabbitmq之直连交换机

    前言 上篇介绍了AMQP的基本概念,组成及其与rabbitmq的关系.了解了这些东西后,下面我们开始学习rabbitmq(消息队列)的作用以及用java代码和rabbitmq通讯进行消息发布和接收.因 ...

  4. Linux RabbitMQ的安装、环境配置、远程访问 , Windows 下安装的RabbitMQ远程访问

    Linux  RabbitMQ的安装和环境配置 1.安装 RabbitMQ是使用Erlang语言编写的,所以安装RabbitMQ之前,先要安装Erlang环境 #对原来的yum官方源做个备份 1.mv ...

  5. rabbitmq高级消息队列

    rabbitmq使用 什么是消息队列 消息(Message)是指在应用间传送的数据.消息可以非常简单,比如只包含文本字符串,也可以很复杂,可以包含嵌入对象. 消息队列是一种应用间的通信方式,消息发送后 ...

  6. RabbitMQ 延时消息队列

    消息延时在日常随处可见: 1.订单创建10min之后不发起支付,自动取消. 2.30min定时推送一次邮件信息. 最常用到方式后台定时任务轮训,量小的时候可以使用,量大会出现数据读取会性能问题.Rab ...

  7. RabbitMQ .NET消息队列使用入门(一)【简单示例】

    首先下载安装包,我都环境是win7 64位: 去官网下载 otp_win64_19.0.exe 和rabbitmq-server-3.6.3.exe安装好 然后开始编程了: (1)创建生产者类: cl ...

  8. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

  9. SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

随机推荐

  1. MVC中CheckBox

    一.单个Checkbox 1.View文件 <%= Html.CheckBoxFor(model => model.IsNeverExpired)%> 2.生成的HTML为 < ...

  2. 在ASP.NET下做了一个实验MVC的小东西

    星期五下班前一时兴起,对MVC发表了一点看法.后来就想干脆弄个小东西来验证一些自己的想法帮组理清思路.所要源代码的免了,3个小时的急就章实在是乱得可以,既没有好的架构,也没有任何代码质量可言,主要是实 ...

  3. 瞬间从IT屌丝变大神——注释规则

    注释的主要规则如下: 公共组件和各栏目的维护者都需要在文件头部加上注释说明: /** *文件用途说明 *作者姓名 *联系方式*制作日期 **/ 大的模块注释方法: //======= //代码用途 / ...

  4. 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子

    一:自定义实现InputFormat *数据源来自于内存 *1.InputFormat是用于处理各种数据源的,下面是实现InputFormat,数据源是来自于内存. *1.1 在程序的job.setI ...

  5. Unity3D-美术相关

    1.导入动画 (1)使用预分解的动画模型,导入后面板中会含有一个可用的动画片段列表. (2)使用未分解的动画模型,自行添加动画片段. (3)使用多个动画文件,模型与动画分离.对于goober.fbx模 ...

  6. 第二百六十七天 how can I 坚持

    晚上有点小郁闷,小纠结,感觉不应该买房,不知道什么吸引着我一定要买呢,曾经坚持不买房的我,为什么成了这个样子. 搞不懂啊. 元旦就要就看房了, 如果真的要买了,明年的压力就会很大了. 经济到底会成为啥 ...

  7. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  8. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  9. react native 遇到的坑

    1.项目中新加入组件,应执行npm install命令 2.项目执行react-native run-android 报错,应进入android目录,执行gradlew.bat clean命令 3.L ...

  10. C++的辅助工具介绍

    1 文档类  (1) Doxygen  参考站点:http://www.doxygen.org  Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和PHP)的.开放源码的 ...