Home Backend Development PHP Tutorial Linux tutorial: manual compilation of php-amqp extension tutorial under ubuntu

Linux tutorial: manual compilation of php-amqp extension tutorial under ubuntu

Jul 29, 2016 am 09:14 AM
amqp channel exchange gt

Linux Tutorial: Manually compile php-amqp extension tutorial under ubuntu

First of all, what is amqp? The introduction is here. In simple terms, it is the advanced Queue protocol. This extension is to enable PHP to support amqp protocol and related queueservice communication.

Advantages: It can solve the concurrency problem of server processing.
Advanced Messaging Queuing Protocol (AMQP) is an application layer protocol specification used for asynchronous messaging. As a wire-layer protocol, rather than an API (such as JMS), AMQP clients can send and receive messages arbitrarily regardless of their source. Now, there are already quite a few servers and clients for different platforms that can be put into use.

(1) Basic concepts

RabbitMQ is a popular open source messaging queue system, developed in erlang language. RabbitMQ is a standard implementation of AMQP (Advanced Message Queuing Protocol). If you are not familiar with AMQP, it will be difficult to read the RabbitMQ documentation directly. However, it only has a few key concepts, which are briefly introduced here. A few concept explanations:

Broker: Simply put, it is a message
    queue
  • server entity. Exchange: Message exchange, which specifies the rules according to which messages are routed to which
  • queue
  • . Queue: message
  • queue
  • carrier, each message will be put into one or more queues. Binding: Binding, its function is to bind exchange and queue according to routing rules.
  • Routing Key: Routing keyword, exchange delivers messages based on this keyword.
  • vhost: Virtual host. Multiple vhosts can be opened in a broker to separate permissions for different users.
  • producer: Message producer is the program that delivers messages.
  • consumer: A message consumer is a program that accepts messages.
  • channel: Message channel. In each connection of the client, multiple channels can be established, each channel represents a session task.
  • (2) Usage process

That is, Client - AMQP server - Client

The client on the left sends a message to the client on the right. The process is:


Get Conection
  • Get Channel
  • Define Exchange, Queue
  • Use a RoutingKey to Binding the Queue to an Exchange
  • By specifying an Exchange and a RoutingKey The message is sent to the corresponding Queue. When receiving, the receiver also obtains the connection, then obtains the channel, and then specifies a Queue to directly fetch the message from the Queue it cares about. It does not care about Exchange, RoutingKey or how to bind. Go up to the corresponding Queue to get the message and it will be OK
  • Since the default source of ubuntu does not have the php5-amqp package, you have to compile it manually to use amqp.
Preparation

:

Install the php compilation tool

<code>sudo apt<span>-get</span> install php5<span>-dev</span></code>
Copy after login
Install the rabbitmq library

<code>sudo apt<span>-get</span> install librabbitmq<span>-dev</span></code>
Copy after login

If your Linux distribution does not have a ready-made librabbitmq-dev package, you can compile and install it by downloading the source code

And if you don’t have To install git, please install git, because we need to get the source code from the official repository

Clone the source code and compile

<code>git <span>clone</span> git:<span>//<strong>github</strong>.com/alanxz/rabbitmq-c.git</span>
cd rabbitmq-c
git submodule init
git submodule update</code>
Copy after login

Compile library

<code>autoreconf -i && ./configure && <span>make</span> && sudo <span>make</span> install</code>
Copy after login

Then we need to download the source code of the php extension, the address is here:

http://pecl.php.net/package/amqp

The latest version is 1.4.0

<code>wget http://pecl<span>.php</span><span>.net</span>/get/amqp-<span>1.4</span><span>.0</span><span>.tgz</span>tar zxf amqp-<span>1.4</span><span>.0</span><span>.tgz</span>
cd amqp-<span>1.4</span><span>.0</span>/

phpize && ./configure --with-amqp && make && sudo make install</code>
Copy after login

Create the configuration file

<code><span>sudo</span><span>echo</span><span>"extension = amqp.so"</span> > /etc/php5/conf.d/amqp.ini</code>
Copy after login

Then restart your web server or php-fpm and print phpinfo, if you see the following The content indicates that the extension is installed

Example

Producer

<code><span>/**
 * PHP amqp(RabbitMQ) Demo - publisher
 * 生产者:发送消息
 * 逻辑:创建连接-->创建channel-->创建交换机<strong>对象</strong>-->发送消息* 
 */</span><span>//配置信息</span><span>$conn_args</span> = <span>array</span>(
    <span>'host'</span> => <span>'localhost'</span>,
    <span>'port'</span> => <span>'5672'</span>,
    <span>'login'</span> => <span>'guest'</span>,
    <span>'password'</span> => <span>'guest'</span>,
    <span>'vhost'</span>=><span>'/'</span>
);
<span>$e_name</span> = <span>'e_lamp'</span>; <span>//交换机名</span><span>$k_route</span> = <span>'key_1'</span>; <span>//路由key</span><span>//创建连接和channel</span><span>$conn</span> = <span>new</span> AMQPConnection(<span>$conn_args</span>);
<span>if</span> (!<span>$conn</span>->connect()) {
    <span>die</span>(<span>"Cannot connect to the broker!\n"</span>);
}
<span>$channel</span> = <span>new</span> AMQPChannel(<span>$conn</span>);

<span>//消息内容</span><span>$message</span> = <span>"TEST MESSAGE! 测试消息!"</span>;

<span>//创建交换机<strong>对象</strong></span><span>$ex</span> = <span>new</span> AMQPExchange(<span>$channel</span>);
<span>$ex</span>->setName(<span>$e_name</span>);

<span>//发送消息</span><span>//$channel->startTransaction(); //开始事务</span><span>for</span>(<span>$i</span>=<span>0</span>; <span>$i</span><<span>5</span>; ++<span>$i</span>){
    <span>echo</span><span>"Send Message:"</span>.<span>$ex</span>->publish(<span>$message</span>, <span>$k_route</span>).<span>"\n"</span>;
}
<span>//$channel->commitTransaction(); //提交事务</span><span>$conn</span>->disconnect();</code>
Copy after login

Consumer

<code><span>/**
 * PHP amqp(RabbitMQ) Demo - consumer
 * 消费者:接收消息
 * 逻辑:创建连接-->创建channel-->创建交换机-->创建<strong>队列</strong>-->绑定交换机/<strong>队列</strong>/路由键-->接收消息*
 */</span><span>//配置信息</span><span>$conn_args</span> = <span>array</span>(
    <span>'host'</span> => <span>'localhost'</span>,
    <span>'port'</span> => <span>'5672'</span>,
    <span>'login'</span> => <span>'guest'</span>,
    <span>'password'</span> => <span>'guest'</span>,
    <span>'vhost'</span>=><span>'/'</span>
);
<span>$e_name</span> = <span>'e_lamp'</span>; <span>//交换机名</span><span>$q_name</span> = <span>'q_lamp'</span>; <span>//<strong>队列</strong>名</span><span>$k_route</span> = <span>'key_1'</span>; <span>//路由key</span><span>//创建连接和channel</span><span>$conn</span> = <span>new</span> AMQPConnection(<span>$conn_args</span>);
<span>if</span> (!<span>$conn</span>->connect()) {
    <span>die</span>(<span>"Cannot connect to the broker!\n"</span>);
}
<span>$channel</span> = <span>new</span> AMQPChannel(<span>$conn</span>);

<span>//创建交换机</span><span>$ex</span> = <span>new</span> AMQPExchange(<span>$channel</span>);
<span>$ex</span>->setName(<span>$e_name</span>);
<span>$ex</span>->setType(AMQP_EX_TYPE_DIRECT); <span>//direct类型</span><span>$ex</span>->setFlags(AMQP_DURABLE); <span>//持久化</span><span>echo</span><span>"Exchange Status:"</span>.<span>$ex</span>-><span>declare</span>().<span>"\n"</span>;

<span>//创建<strong>队列</strong></span><span>$q</span> = <span>new</span> AMQPQueue(<span>$channel</span>);
<span>$q</span>->setName(<span>$q_name</span>);
<span>$q</span>->setFlags(AMQP_DURABLE); <span>//持久化</span><span>echo</span><span>"Message Total:"</span>.<span>$q</span>-><span>declare</span>().<span>"\n"</span>;

<span>//绑定交换机与<strong>队列</strong>,并指定路由键</span><span>echo</span><span>'Queue Bind: '</span>.<span>$q</span>->bind(<span>$e_name</span>, <span>$k_route</span>).<span>"\n"</span>;

<span>//阻塞模式接收消息</span><span>echo</span><span>"Message:\n"</span>;
<span>while</span>(<span>True</span>){
    <span>$q</span>->consume(<span>'processMessage'</span>);
    <span>//$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答</span>
}
<span>$conn</span>->disconnect();

<span>/**
* 消费回调函数
* 处理消息
*/</span><span><span>function</span><span>processMessage</span><span>(<span>$envelope</span>, <span>$queue</span>)</span> {</span><span>$msg</span> = <span>$envelope</span>->getBody();
    <span>echo</span><span>$msg</span>.<span>"\n"</span>; <span>//处理消息</span><span>$queue</span>->ack(<span>$envelope</span>->getDeliveryTag()); <span>//手动发送ACK应答</span>
}</code>
Copy after login

The above has introduced the Linux tutorial on manually compiling the php-amqp extension tutorial under ubuntu, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to create a no-reply email address in Exchange Server/Microsoft 365 How to create a no-reply email address in Exchange Server/Microsoft 365 Feb 19, 2024 pm 04:03 PM

This article will show you how to set up a no-reply email address in Exchange Server/Microsoft365. No-reply email addresses can be a huge productivity boost when you need to automatically send emails without receiving a reply. Create a no-reply email address in Exchange Server/Microsoft365 Follow these steps to create a no-reply email address in Exchange Server/Microsoft365: Log in to the Microsoft365 admin center using your administrator account. Click the navigation menu and select Exchange under Admin Center. Exchange will now open

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How Swoole supports asynchronous AMQP operations How Swoole supports asynchronous AMQP operations Jun 25, 2023 am 08:22 AM

As the volume of Internet business continues to grow, the demand for high concurrency and high performance is getting higher and higher, and Swoole, as a network communication framework for PHP, is increasingly favored by developers. Among them, Swoole supports asynchronous AMQP, which is one of the more common application scenarios. So let's take a look at how Swoole supports asynchronous AMQP operations. First, we need to clarify what AMQP is. AMQP (AdvancedMessageQueuingProtocol) Advanced

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What does channel mean in Go language? What does channel mean in Go language? Dec 14, 2023 pm 02:21 PM

Channel in Go language is a mechanism for communication and data synchronization between coroutines. Can be thought of as a special data type, similar to a queue or pipe, used to transfer data between different coroutines. Channel provides two main operations: send and receive. Both send and receive operations in a channel are blocking, which means that if no sender or receiver is ready, the operation will be blocked until a coroutine is ready to perform the corresponding operation, etc.

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

See all articles