MacOS下的AMQP服务器以及PHP扩展搭建

简介: MacOS下的AMQP服务器以及PHP扩展搭建

环境说明

  • macOS版本, macOS Sierra 10.12.3 (16D32)
  • PHP集成环境, XAMPP 7.0.15-0
  • Apache 2.4.25,
  • MariaDB 10.1.21
  • PHP 7.0.15

前期准备

修改本地path文件

sudo vi /etc/paths

在文件的最上面添加/Applications/XAMPP/xamppfiles/bin

重启中端

bogon:~ xiaoyu$ php -v
PHP 7.0.15 (cli) (built: Jan 20 2017 07:22:25) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

安装RabbitMQ扩展

wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-mac-standalone-3.6.6.tar.xz
tar -xf rabbitmq-server-mac-standalone-3.6.6.tar.xz 
cd rabbitmq_server-3.6.6/
sbin/rabbitmq-server

报错 ERROR: epmd error for host bogon: timeout (timed out)

修改hosts , 增加 127.0.0.1 bogon

继续

bogon:rabbitmq_server-3.6.6 xiaoyu$ sbin/rabbitmq-server
              RabbitMQ 3.6.6. Copyright (C) 2007-2016 Pivotal Software, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /Applications/XAMPP/rabbitmq_server-3.6.6/var/log/rabbitmq/rabbit@bogon.log
  ######  ##        /Applications/XAMPP/rabbitmq_server-3.6.6/var/log/rabbitmq/rabbit@bogon-sasl.log
  ##########
              Starting broker...
 completed with 0 plugins.

添加到本地PATH

sudo vi /etc/paths

添加所在sbin路径/Applications/XAMPP/rabbitmq_server-3.6.6/sbin

重启终端

再次启动服务

rabbitmq-server

安装 RabbitMQ management UI

这个图形化界面是一个插件

bogon:~ xiaoyu$ rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
  mochiweb
  webmachine
  rabbitmq_web_dispatch
  amqp_client
  rabbitmq_management_agent
  rabbitmq_management
Applying plugin configuration to rabbit@bogon... failed.
 * Could not contact node rabbit@bogon.
   Changes will take effect at broker restart.
 * Options: --online  - fail if broker cannot be contacted.
            --offline - do not try to contact broker.

重启服务后,就会变成completed with 6 plugins.

访问图形化登录界面的地址为http://localhost:15672/

用户管理

# 创建用户
rabbitmqctl add_user demo pwd
# 赋予超级管理员权限
rabbimqctl set_user_tags  demo administrator

之后就可以登录图形化管理界面了

用户标签

在创建用户的之后,需要通过标签的形式给用户赋予对应的权限

administrator | monitoring | policymaker | management |

Comma-separated list of tags to apply to the user. Currently supported by the management plugin:

  • management
    User can access the management plugin
  • policymaker
    User can access the management plugin and manage policies and parameters for the vhosts they have access to.
  • monitoring
    User can access the management plugin and see all connections and channels as well as node-related information.
  • administrator
    User can do everything monitoring can do, manage users, vhosts and permissions, close other user’s connections, and manage policies and parameters for all vhosts.
    Note that you can set any tag here; the links for the above four tags are just for convenience.

安装AMQP扩展

sudo pecl install amqp
#失败
configure: error: Please reinstall the librabbitmq distribution itself or (re)install librabbitmq development package if it available in your system

安装 rabbitmq-c

git clone git://github.com/alanxz/rabbitmq-c.git
cd rabbitmq-c
git submodule init
git submodule update
#报错
bogon:rabbitmq-c xiaoyu$  autoreconf -i && ./configure && make && sudo make install
Can't exec "aclocal": No such file or directory at /usr/local/Cellar/autoconf/2.69/share/autoconf/Autom4te/FileUtils.pm line 326.
autoreconf: failed to run aclocal: No such file or directory

安装 aclocal

`aclocal’ is part of automake package, try to install it first.

所以需要重新安装automake

brew install automake

继续安装rabbitmq-c

autoreconf -i
#报错 
Makefile.am:6: error: Libtool library used but 'LIBTOOL' is undefined
brew install libtool
autoreconf -i
#报错
Can't exec "libtoolize": No such file or directory at /usr/local/share/autoconf/Autom4te/FileUtils.pm line 344, <GEN3> line 4.
autoreconf: failed to run libtoolize: No such file or directory
autoreconf: libtoolize is needed because this package uses Libtool
#添加PATH /usr/local/Cellar/libtool/2.4.6_1/bin
brew install boost double-conversion gflags glog libevent
sudo ln -s /usr/local/bin/glibtoolize /usr/local/bin/libtoolize

继续

autoreconf -i
./configure && make && sudo make install
#报错
./librabbitmq/amqp_openssl_bio.h:26:10: fatal error: 'openssl/bio.h' file not found
#include <openssl/bio.h>
         ^
1 error generated.
make[1]: *** [librabbitmq/librabbitmq_librabbitmq_la-amqp_openssl.lo] Error 1
make: *** [all] Error 2
#xcode安装openssl
xcode-select --install
xcode-select -p #找到xcode的路径
找不到 对应文件夹,失败
#编译安装openssl
git clone  https://github.com/openssl/openssl.git
cd openssl
./config
make
make test
make install 
## 编译安装报错
Cannot find "ERR_get_error(3)" in podpath: cannot find suitable replacement path, cannot resolve link
经过下面验证可以用

继续

./configure && make && sudo make install

继续安装AMQP

sudo pecl install amqp
#成功
Build process completed successfully
Installing '/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20151012/amqp.so'
install ok: channel://pecl.php.net/amqp-1.8.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=amqp.so" to php.ini

修改配置文件

重启服务器

php -m
# 扩展安装成功
[PHP Modules]
**amqp**
bcmath
bz2
calendar
Core
.....

编写测试代码

创建项目

创建 composer.json文件

{
  "require": {
      "php-amqplib/php-amqplib": "2.6.*"
  }
}

在项目所在目录执行composer install

测试demo

配置文件config.php

<?php
require_once __DIR__ . '/vendor/autoload.php';
define('HOST', 'bogon');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
//If this is enabled you can see AMQP output on the CLI
define('AMQP_DEBUG', true);

demo文件test.php

<?php
include(__DIR__ . '/config.php');
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$exchange = 'basic_get_test';
$queue = 'basic_get_queue';
$connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();
/*
    The following code is the same both in the consumer and the producer.
    In this way we are sure we always have a queue to consume from and an
        exchange where to publish messages.
*/
/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($queue, false, true, false, false);
/*
    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$channel->exchange_declare($exchange, 'direct', false, true, false);
$channel->queue_bind($queue, $exchange);
$toSend = new AMQPMessage('test message', array('content_type' => 'text/plain', 'delivery_mode' => 2));
$channel->basic_publish($toSend, $exchange);
$message = $channel->basic_get($queue);
$channel->basic_ack($message->delivery_info['delivery_tag']);
var_dump($message->body);
$channel->close();
$connection->close();

命令行运行代码php test.php,得到debug信息,和正确的返回

< [hex]: 
0000  41 4D 51 50 00 00 09 01                            AMQP.... 
waiting for 10,10
waiting for a new frame
> 10,10: Connection.start
Start from server, version: 0.9, properties: capabilities=(publisher_confirms=1, exchange_exchange_bindings=1, basic.nack=1, consumer_cancel_notify=1, connection.blocked=1, consumer_priorities=1, authentication_failure_close=1, per_consumer_qos=1, direct_reply_to=1), cluster_name=rabbit@bogon, copyright=Copyright (C) 2007-2016 Pivotal Software, Inc., information=Licensed under the MPL.  See http://www.rabbitmq.com/, platform=Erlang/OTP, product=RabbitMQ, version=3.6.6, mechanisms: AMQPLAIN, PLAIN, locales: en_US
< 10,11:: Connection.start_ok
< [hex]: 
0000  01 00 00 00 00 01 31 00  0A 00 0B 00 00 00 F3 07   ......1. ......?.
0010  70 72 6F 64 75 63 74 53  00 00 00 07 41 4D 51 50   productS ....AMQP
0020  4C 69 62 08 70 6C 61 74  66 6F 72 6D 53 00 00 00   Lib.plat formS...
0030  03 50 48 50 07 76 65 72  73 69 6F 6E 53 00 00 00   .PHP.ver sionS...
0040  03 32 2E 36 0B 69 6E 66  6F 72 6D 61 74 69 6F 6E   .2.6.inf ormation
0050  53 00 00 00 00 09 63 6F  70 79 72 69 67 68 74 53   S.....co pyrightS
0060  00 00 00 00 0C 63 61 70  61 62 69 6C 69 74 69 65   .....cap abilitie
0070  73 46 00 00 00 8C 1C 61  75 74 68 65 6E 74 69 63   sF...?.a uthentic
0080  61 74 69 6F 6E 5F 66 61  69 6C 75 72 65 5F 63 6C   ation_fa ilure_cl
0090  6F 73 65 74 01 12 70 75  62 6C 69 73 68 65 72 5F   oset..pu blisher_
00A0  63 6F 6E 66 69 72 6D 73  74 01 16 63 6F 6E 73 75   confirms t..consu
00B0  6D 65 72 5F 63 61 6E 63  65 6C 5F 6E 6F 74 69 66   mer_canc el_notif
00C0  79 74 01 1A 65 78 63 68  61 6E 67 65 5F 65 78 63   yt..exch ange_exc
00D0  68 61 6E 67 65 5F 62 69  6E 64 69 6E 67 73 74 01   hange_bi ndingst.
00E0  0A 62 61 73 69 63 2E 6E  61 63 6B 74 01 12 63 6F   .basic.n ackt..co
00F0  6E 6E 65 63 74 69 6F 6E  2E 62 6C 6F 63 6B 65 64   nnection .blocked
0100  74 01 08 41 4D 51 50 4C  41 49 4E 00 00 00 23 05   t..AMQPL AIN...#.
0110  4C 4F 47 49 4E 53 00 00  00 05 67 75 65 73 74 08   LOGINS.. ..guest.
0120  50 41 53 53 57 4F 52 44  53 00 00 00 05 67 75 65   PASSWORD S....gue
0130  73 74 05 65 6E 5F 55 53  CE                        st.en_US ?
< 10,11:: Connection.start_ok
waiting for 10,20, 10,30
waiting for a new frame
> 10,30: Connection.tune
< 10,31:: Connection.tune_ok
< [hex]: 
0000  01 00 00 00 00 00 0C 00  0A 00 1F FF FF 00 02 00   ........ ...??...
0010  00 00 00 CE                                        ...?
< 10,31:: Connection.tune_ok
< 10,40:: Connection.open
< [hex]: 
0000  01 00 00 00 00 00 08 00  0A 00 28 01 2F 00 00 CE   ........ ..(./..?
< 10,40:: Connection.open
waiting for 10,41
waiting for a new frame
> 10,41: Connection.open_ok
Open OK! known_hosts: 
using channel_id: 1
< 20,10:: Channel.open
< [hex]: 
0000  01 00 01 00 00 00 05 00  14 00 0A 00 CE            ........ ....?
< 20,10:: Channel.open
waiting for 20,11
waiting for a new frame
> 20,11: Channel.open_ok
Channel open
< 50,10:: Queue.declare
< [hex]: 
0000  01 00 01 00 00 00 1B 00  32 00 0A 00 00 0F 62 61   ........ 2.....ba
0010  73 69 63 5F 67 65 74 5F  71 75 65 75 65 02 00 00   sic_get_ queue...
0020  00 00 CE                                           ..?
< 50,10:: Queue.declare
waiting for 50,11
waiting for a new frame
> 50,11: Queue.declare_ok
< 40,10:: Exchange.declare
< [hex]: 
0000  01 00 01 00 00 00 21 00  28 00 0A 00 00 0E 62 61   ......!. (.....ba
0010  73 69 63 5F 67 65 74 5F  74 65 73 74 06 64 69 72   sic_get_ test.dir
0020  65 63 74 02 00 00 00 00  CE                        ect..... ?
< 40,10:: Exchange.declare
waiting for 40,11
waiting for a new frame
> 40,11: Exchange.declare_ok
< 50,20:: Queue.bind
< [hex]: 
0000  01 00 01 00 00 00 2B 00  32 00 14 00 00 0F 62 61   ......+. 2.....ba
0010  73 69 63 5F 67 65 74 5F  71 75 65 75 65 0E 62 61   sic_get_ queue.ba
0020  73 69 63 5F 67 65 74 5F  74 65 73 74 00 00 00 00   sic_get_ test....
0030  00 00 CE                                           ..?
< 50,20:: Queue.bind
waiting for 50,21
waiting for a new frame
> 50,21: Queue.bind_ok
< 60,40:: Basic.publish
< [hex]: 
0000  01 00 01 00 00 00 17 00  3C 00 28 00 00 0E 62 61   ........ <.(...ba
0010  73 69 63 5F 67 65 74 5F  74 65 73 74 00 00 CE 02   sic_get_ test..?.
0020  00 01 00 00 00 1A 00 3C  00 00 00 00 00 00 00 00   .......< ........
0030  00 0C 90 00 0A 74 65 78  74 2F 70 6C 61 69 6E 02   ..?..tex t/plain.
0040  CE 03 00 01 00 00 00 0C  74 65 73 74 20 6D 65 73   ?....... test mes
0050  73 61 67 65 CE                                     sage?
< 60,70:: Basic.get
< [hex]: 
0000  01 00 01 00 00 00 17 00  3C 00 46 00 00 0F 62 61   ........ <.F...ba
0010  73 69 63 5F 67 65 74 5F  71 75 65 75 65 00 CE      sic_get_ queue.?
< 60,70:: Basic.get
waiting for 60,71, 60,72
waiting for a new frame
> 60,71: Basic.get_ok
waiting for a new frame
waiting for a new frame
< 60,80:: Basic.ack
< [hex]: 
0000  01 00 01 00 00 00 0D 00  3C 00 50 00 00 00 00 00   ........ <.P.....
0010  00 00 01 00 CE                                     ....?
< 60,80:: Basic.ack
string(12) "test message"
< 20,40:: Channel.close
< [hex]: 
0000  01 00 01 00 00 00 0B 00  14 00 28 00 00 00 00 00   ........ ..(.....
0010  00 00 CE                                           ..?
< 20,40:: Channel.close
waiting for 20,41
waiting for a new frame
> 20,41: Channel.close_ok
< 10,50:: Connection.close
< [hex]: 
0000  01 00 00 00 00 00 0B 00  0A 00 32 00 00 00 00 00   ........ ..2.....
0010  00 00 CE                                           ..?
< 10,50:: Connection.close
waiting for 10,51
waiting for a new frame
> 10,51: Connection.close_ok
closing input
closing socket

到此,环境配置完成

总结

本次配置环境涉及到了

  • 不同bash下的环境变量的修改
  • 软件环境的相互依赖关系
  • 编译的报错以及排查
  • 出现问题之后的问题搜索技巧
  • 只要可能出问题就一定出问题的墨菲定律

参考资料

相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
10天前
|
设计模式 算法 数据库连接
PHP中的设计模式:提高代码的可维护性与扩展性本文旨在探讨PHP中常见的设计模式及其应用,帮助开发者编写出更加灵活、可维护和易于扩展的代码。通过深入浅出的解释和实例演示,我们将了解如何使用设计模式解决实际开发中的问题,并提升代码质量。
在软件开发过程中,设计模式是一套经过验证的解决方案模板,用于处理常见的软件设计问题。PHP作为流行的服务器端脚本语言,也有其特定的设计模式应用。本文将重点介绍几种PHP中常用的设计模式,包括单例模式、工厂模式和策略模式,并通过实际代码示例展示它们的具体用法。同时,我们还将讨论如何在实际项目中合理选择和应用这些设计模式,以提升代码的可维护性和扩展性。
|
5天前
|
存储 关系型数据库 API
深入理解后端技术:构建高效、可扩展的服务器端应用
本文将探讨后端开发的核心概念和技术,包括服务器端编程、数据库管理、API设计和安全性等方面。通过深入浅出的方式,让读者了解如何构建高效、可扩展的后端系统。我们将从基本的后端框架开始,逐步深入到高级主题,如微服务架构和容器化部署。无论您是初学者还是有经验的开发人员,都能在本文中找到有价值的信息和实用的建议。
|
13天前
|
设计模式 数据库连接 PHP
PHP中的设计模式:如何提高代码的可维护性与扩展性在软件开发领域,PHP 是一种广泛使用的服务器端脚本语言。随着项目规模的扩大和复杂性的增加,保持代码的可维护性和可扩展性变得越来越重要。本文将探讨 PHP 中的设计模式,并通过实例展示如何应用这些模式来提高代码质量。
设计模式是经过验证的解决软件设计问题的方法。它们不是具体的代码,而是一种编码和设计经验的总结。在PHP开发中,合理地使用设计模式可以显著提高代码的可维护性、复用性和扩展性。本文将介绍几种常见的设计模式,包括单例模式、工厂模式和观察者模式,并通过具体的例子展示如何在PHP项目中应用这些模式。
|
9天前
|
设计模式 存储 算法
PHP中的设计模式:策略模式的深入解析与应用在软件开发的浩瀚海洋中,PHP以其独特的魅力和强大的功能吸引了无数开发者。作为一门历史悠久且广泛应用的编程语言,PHP不仅拥有丰富的内置函数和扩展库,还支持面向对象编程(OOP),为开发者提供了灵活而强大的工具集。在PHP的众多特性中,设计模式的应用尤为引人注目,它们如同精雕细琢的宝石,镶嵌在代码的肌理之中,让程序更加优雅、高效且易于维护。今天,我们就来深入探讨PHP中使用频率颇高的一种设计模式——策略模式。
本文旨在深入探讨PHP中的策略模式,从定义到实现,再到应用场景,全面剖析其在PHP编程中的应用价值。策略模式作为一种行为型设计模式,允许在运行时根据不同情况选择不同的算法或行为,极大地提高了代码的灵活性和可维护性。通过实例分析,本文将展示如何在PHP项目中有效利用策略模式来解决实际问题,并提升代码质量。
|
2月前
|
缓存 安全 Java
Java服务器端技术:Servlet与JSP的集成与扩展
Java服务器端技术:Servlet与JSP的集成与扩展
23 3
|
2月前
|
安全 前端开发 PHP
PHP与现代Web开发:构建高效和可扩展的应用程序
【8月更文挑战第29天】在这篇文章中,我们将深入探讨PHP如何适应现代Web开发的需求。我们将通过实际案例分析,揭示PHP的核心优势,并展示如何利用这些优势来构建高性能、可扩展的Web应用。文章不仅提供理论知识,还包括具体的代码示例,旨在帮助开发者更好地理解和运用PHP解决实际问题。
|
2月前
|
SQL 关系型数据库 MySQL
PHP与数据库交互的艺术:深入探讨PDO扩展
【8月更文挑战第28天】在数字信息时代的海洋里,PHP作为一艘灵活的帆船,承载着无数网站和应用的梦想。而PDO扩展,则是这艘帆船上不可或缺的导航仪,指引着数据安全与效率的航向。本文将带你领略PHP与数据库交互的艺术,深入浅出地探索PDO的世界,从连接数据库到执行复杂的查询,每一步都清晰可见。我们将一起航行在这段奇妙的旅程上,解锁数据的奥秘,体验编程的乐趣。
15 1
|
2月前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
2月前
|
关系型数据库 MySQL 数据库
【Mac os系统】安装MySQL数据库
本文详细介绍了在Mac OS系统上安装MySQL数据库的步骤,包括下载、安装、配置环境变量、启动服务、授权设置以及解决常见问题,并提供了一些常用的MySQL命令。
93 0
【Mac os系统】安装MySQL数据库
|
3月前
|
Linux 虚拟化 iOS开发
部署06--MacOS安装VMware Fusion安装
部署06--MacOS安装VMware Fusion安装
下一篇
无影云桌面