国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Introduction
"Hello World"
(using the php-amqplib Client)
The php-amqplib client library
Sending(發(fā)送)
Sending doesn't work!(發(fā)布出去消息咋辦)
Receiving(接收消息)
Home Backend Development PHP Tutorial rabbitmq php tutorial -一

rabbitmq php tutorial -一

Jun 13, 2016 am 11:53 AM
messages nbsp quot rabbitmq the

rabbitmq php tutorial -1

Introduction


RabbitMQ is a message broker. In essence, it accepts messages from?producers, and delivers them to?consumers. In-between, it can route, buffer, and persist the messages according to rules you give it.

RabbitMQ 是一個(gè)消息代理。說白了,它從“生產(chǎn)者”接收消息,并將這些消息發(fā)送給“消費(fèi)者”。在這個(gè)過程中,它可以根據(jù)你給定的規(guī)則對消息進(jìn)行路由、緩沖和持久化。

RabbitMQ, and messaging in general, uses some jargon.(常用術(shù)語)

  • Producing?means nothing more than sending. A program that sends messages is a?producer. We'll draw it like that, with "P":

  • 生產(chǎn)無非就是發(fā)送。一個(gè)發(fā)送消息的程序就是一個(gè)“生產(chǎn)者”,我們用“P”來表示它。
  • ?

  • A queue?is the name for a mailbox. It lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can be stored only inside a?queue. A?queue?is not bound by any limits, it can store as many messages as you like - it's essentially an infinite buffer. Many?producers?can send messages that go to one queue - many?consumers?can try to receive data from one?queue. A queue will be drawn like this, with its name above it:

  • 隊(duì)列就像是郵箱的名字。它存于RabbitMQ內(nèi)部。盡管消息流貫穿于RabbitMQ和你的應(yīng)用,但它只能存儲(chǔ)于隊(duì)列當(dāng)中。隊(duì)列不受任何限制,隨便你存多少消息,看你心情-隊(duì)列本質(zhì)上是一個(gè)無窮大的緩沖區(qū)。 多個(gè)“消費(fèi)者”可以發(fā)送消息給一個(gè)隊(duì)列-多個(gè)“消費(fèi)者”也可以從一個(gè)隊(duì)列接收消息。我們這樣來表示一個(gè)隊(duì)列(如下),上面是它的名字。
  • ?

  • Consuming?has a similar meaning to receiving. A?consumer?is a program that mostly waits to receive messages. On our drawings it's shown with "C":

  • 消費(fèi)意思就是接收?!毕M(fèi)者“通常是一個(gè)等待接受消息的程序。我們用“C”來表示。
  • ?

Note that the producer, consumer, and broker do not have to reside on the same machine; indeed in most applications they don't.

注意:生產(chǎn)者、消費(fèi)者和消息代理(中間人)不是必須在一個(gè)機(jī)器上,確實(shí)如此,大多數(shù)應(yīng)用程序都這樣。

"Hello World"

(using the php-amqplib Client)

In this part of the tutorial we'll write two programs in PHP; a producer that sends a single message, and a consumer that receives messages and prints them out. We'll gloss over some of the detail in the?php-amqplib?API, concentrating on this very simple thing just to get started. It's a "Hello World" of messaging.

在這一扒,我們會(huì)寫倆PHP程序,一個(gè)發(fā)送單一消息的生產(chǎn)者,和一個(gè)接收消息并打印的消費(fèi)者。我們略過一些php-amqplib API的細(xì)節(jié),集中精力到這些簡單的事兒上來開個(gè)頭兒 —— 一個(gè)消息的Hello World程序。

In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.

在下面的圖表中,P是我們的生產(chǎn)者,C是我們的消費(fèi)者。中間內(nèi)筐就是個(gè)隊(duì)列-一個(gè)由RabbitMQ維持的消息緩沖區(qū)。

(P) -> [|||] -> (C)

The php-amqplib client library

RabbitMQ speaks?AMQP, which is an open, general-purpose protocol for messaging. There are a number of clients for AMQP in?many different languages. We'll use the php-amqplib in this tutorial.

RabbitMQ 遵循AMQP——一個(gè)開放的、多用途的消息協(xié)議。還有若干不同語言的的AMQP客戶端。我們就用php-amqplib啦~

Add a composer.json file to your project: 在你的項(xiàng)目中添加一個(gè)composer.json文件,內(nèi)容如下:

{    "require": {        "videlalvaro/php-amqplib": "v2.1.0"    }}

Provided you have?composer?installed, you can run the following: 假使你已經(jīng)安裝了composer, 有可以執(zhí)行下面的命令:

$ composer.phar install

Now we have the php-amqplib installed, we can write some code.

現(xiàn)在php-amqplib安完了,寫點(diǎn)code唄。

Sending(發(fā)送)

(P) -> [|||]

We'll call our message sender?send.php?and our message receiver?receive.php. The sender will connect to RabbitMQ, send a single message, then exit.

創(chuàng)建一個(gè)文件send.php來發(fā)消息,receive.php來收消息。發(fā)送方(send.php)連接RabbitMQ發(fā)送一條消息,然后退出程序。

In?send.php, we need to include the library and?use?the necessary classes:

send.php里,需要包含必要的庫及引入必要的類。

require_once __DIR__ . '/vendor/autoload.php';use PhpAmqpLib\Connection\AMQPConnection;use PhpAmqpLib\Message\AMQPMessage;

then we can create a connection to the server:

然后創(chuàng)建連接服務(wù)器的connection.

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();

The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a broker on the local machine - hence the?localhost. If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.

這個(gè)連接幫我們做socket鏈接,處理協(xié)議版本談判和鑒定。這樣呢我們就鏈接到了本地的消息代理(broker),因?yàn)槭莑ocalhost嘛。如果想鏈接不同機(jī)器上的broker,簡單的指定下名字或者IP地址就好啦,就是替換一下上面的localhost。

Next we create a channel, which is where most of the API for getting things done resides.

下面我們創(chuàng)建個(gè)信道——多數(shù)API干的活都在這里完成。

To send, we must declare a queue for us to send to; then we can publish a message to the queue:

為了能發(fā)送消息,必須聲明一個(gè)隊(duì)列。然后就可以發(fā)送消息到了。

$channel->queue_declare('hello', false, false, false, false);$msg = new AMQPMessage('Hello World!');$channel->basic_publish($msg, '', 'hello');echo " [x] Sent 'Hello World!'\n";

Declaring a queue is idempotent - it will only be created if it doesn't exist already. The message content is a byte array, so you can encode whatever you like there.

聲明隊(duì)列是冪等的——只有在它不存在的情況下才會(huì)被創(chuàng)建。消息內(nèi)容是字節(jié)數(shù)組,所以呢,你可以編碼一下隨便啥,放那替換下就能發(fā)。

Lastly, we close the channel and the connection;

末了,關(guān)閉信道和服務(wù)器連接。

$channel->close();$connection->close();

Here's the whole send.php class. (這是整個(gè)send.php類文件)

Sending doesn't work!(發(fā)布出去消息咋辦)

If this is your first time using RabbitMQ and you don't see the "Sent" message then you may be left scratching your head wondering what could be wrong. Maybe the broker was started without enough free disk space (by default it needs at least 1Gb free) and is therefore refusing to accept messages. Check the broker logfile to confirm and reduce the limit if necessary. The?configuration file documentation?will show you how to setdisk_free_limit.

如果你頭一次用RabbitMQ并且沒看到"已發(fā)送(sent)"字樣的提示信息,然后剩下的就是撓頭了——為毛呢?哪錯(cuò)了?有可能是broker啟動(dòng)時(shí)木有足夠可用的硬盤空間(默認(rèn)需要至少1G),所以拒絕接受消息。檢查broker日志文件確定一下,有必要的話,調(diào)低下限值。這個(gè)配置文件會(huì)告訴你咋設(shè)置可用硬盤限值。

Receiving(接收消息)

That's it for our sender. Our receiver is pushed messages from RabbitMQ, so unlike the sender which publishes a single message, we'll keep it running to listen for messages and print them out.

發(fā)送者就是這么回事啦。接收者呢,顧名思義就是接收從RabbitMQ推送過來的消息,所以不像發(fā)送者哪有就發(fā)一條消息就完了。

我們保持接收者一直運(yùn)行來接收和打印消息。

[|||] -> (C)

The code (in?receive.php) has almost the same?include?and?uses as?send:

receive.php中include和use的代碼幾乎和send.php一樣。

require_once __DIR__ . '/vendor/autoload.php';use PhpAmqpLib\Connection\AMQPConnection;

Setting up is the same as the sender; we open a connection and a channel, and declare the queue from which we're going to consume. Note this matches up with the queue that?send publishes to.

設(shè)置也和上面一樣,打開一個(gè)連接和信道,聲明一個(gè)我們要消費(fèi)(接收消息)的隊(duì)列。注意這里要和上面發(fā)送者的的聲明要對應(yīng)。

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();$channel->queue_declare('hello', false, false, false, false);echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

Note that we declare the queue here, as well. Because we might start the receiver before the sender, we want to make sure the queue exists before we try to consume messages from it.

主要這里我們也同樣聲明了隊(duì)列。因?yàn)橛锌赡芪覀冊诎l(fā)送者發(fā)送消息之前就開始接收了,所以在我們從中接收消息之前得確定存它在呀。

We're about to tell the server to deliver us the messages from the queue. We will define a?PHP callable?that will receive the messages sent by the server. Keep in mind that messages are sent asynchronously from the server to the clients.

我們得告訴服務(wù)器從隊(duì)列中給我們遞送消息。定義個(gè)回調(diào)來接收服務(wù)器發(fā)送的消息。記住啊,消息從服務(wù)器發(fā)送到客戶端是異步的啊。

$callback = function($msg) {  echo " [x] Received ", $msg->body, "\n";};$channel->basic_consume('hello', '', false, true, false, false, $callback);while(count($channel->callbacks)) {    $channel->wait();}

Our code will block while our?$channel?has callbacks. Whenever we receive a message our$callback?function will be passed the received message.

由于$channel有回調(diào),所以我們的代碼會(huì)阻塞住(收聽消息嘛,不是 bug哦),啥時(shí)候我們收到了消息,回調(diào)函數(shù)就會(huì)處理它啦。

Here's the whole receive.php class(完整的receive.php類文件)

Putting it all together(合體?。。。?/h3>

Now we can run both scripts. In a terminal, run the sender:

寫都寫完了,跑一下唄,先跑sender

$ php send.php

then, run the receiver:然后跑receiver

$ php receive.php

The receiver will print the message it gets from the sender via RabbitMQ. The receiver will keep running, waiting for messages (Use Ctrl-C to stop it), so try running the sender from another terminal.

內(nèi)個(gè),接收者會(huì)把從RabbitMQ接收的消息打印出來,當(dāng)然這消息是發(fā)送者發(fā)給RabbitMQ的。接收者會(huì)在這死磕,一直跟這等消息(用Ctrl-C可以給干掉它),試試在其他的終端跑一下sender.

If you want to check on the queue, try using?rabbitmqctl list_queues.

要想看看這個(gè)隊(duì)列,用一下rabbitmqctl list_queues這個(gè)命令。(內(nèi)個(gè)。。在sbin/下呢哦,找不到的話)

Hello World! 你好呀,世界~~

Time to move on to?part 2?and build a simple?work queue. (敬請期待,哦吼吼吼~~)

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

See all articles