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

目次
Introduction
"Hello World"
(using the php-amqplib Client)
The php-amqplib client library
Sending(發(fā)送)
Sending doesn't work!(發(fā)布出去消息咋辦)
Receiving(接收消息)

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 是一個消息代理。說白了,它從“生產(chǎn)者”接收消息,并將這些消息發(fā)送給“消費(fèi)者”。在這個過程中,它可以根據(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ā)送。一個發(fā)送消息的程序就是一個“生產(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)用,但它只能存儲于隊(duì)列當(dāng)中。隊(duì)列不受任何限制,隨便你存多少消息,看你心情-隊(duì)列本質(zhì)上是一個無窮大的緩沖區(qū)。 多個“消費(fèi)者”可以發(fā)送消息給一個隊(duì)列-多個“消費(fèi)者”也可以從一個隊(duì)列接收消息。我們這樣來表示一個隊(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)者“通常是一個等待接受消息的程序。我們用“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)者和消息代理(中間人)不是必須在一個機(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.

在這一扒,我們會寫倆PHP程序,一個發(fā)送單一消息的生產(chǎn)者,和一個接收消息并打印的消費(fèi)者。我們略過一些php-amqplib API的細(xì)節(jié),集中精力到這些簡單的事兒上來開個頭兒 —— 一個消息的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)筐就是個隊(duì)列-一個由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——一個開放的、多用途的消息協(xié)議。還有若干不同語言的的AMQP客戶端。我們就用php-amqplib啦~

Add a composer.json file to your project: 在你的項(xiàng)目中添加一個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)建一個文件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.

這個連接幫我們做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)建個信道——多數(shù)API干的活都在這里完成。

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

為了能發(fā)送消息,必須聲明一個隊(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ì)列是冪等的——只有在它不存在的情況下才會被創(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. (這是整個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)"字樣的提示信息,然后剩下的就是撓頭了——為毛呢?哪錯了?有可能是broker啟動時(shí)木有足夠可用的硬盤空間(默認(rèn)需要至少1G),所以拒絕接受消息。檢查broker日志文件確定一下,有必要的話,調(diào)低下限值。這個配置文件會告訴你咋設(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è)置也和上面一樣,打開一個連接和信道,聲明一個我們要消費(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ì)列中給我們遞送消息。定義個回調(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),所以我們的代碼會阻塞住(收聽消息嘛,不是 bug哦),啥時(shí)候我們收到了消息,回調(diào)函數(shù)就會處理它啦。

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)個,接收者會把從RabbitMQ接收的消息打印出來,當(dāng)然這消息是發(fā)送者發(fā)給RabbitMQ的。接收者會在這死磕,一直跟這等消息(用Ctrl-C可以給干掉它),試試在其他的終端跑一下sender.

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

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

Hello World! 你好呀,世界~~

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

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫像を無料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

寫真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中國語版

SublimeText3 中國語版

中國語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Windows 11 でウィンドウの境界線の設(shè)定を調(diào)整する方法: 色とサイズを変更する Windows 11 でウィンドウの境界線の設(shè)定を調(diào)整する方法: 色とサイズを変更する Sep 22, 2023 am 11:37 AM

Windows 11 では、新鮮でエレガントなデザインが前面に押し出されており、最新のインターフェイスにより、ウィンドウの境界線などの細(xì)部をカスタマイズして変更することができます。このガイドでは、Windows オペレーティング システムで自分のスタイルを反映した環(huán)境を作成するのに役立つ手順について説明します。ウィンドウの境界線の設(shè)定を変更するにはどうすればよいですか? + を押して設(shè)定アプリを開きます。 Windows [個人用設(shè)定] に移動し、[色の設(shè)定] をクリックします。ウィンドウの境界線の色の変更設(shè)定ウィンドウ 11" width="643" height="500" > [タイトル バーとウィンドウの境界線にアクセント カラーを表示する] オプションを見つけて、その橫にあるスイッチを切り替えます。 [スタート] メニューとタスク バーにアクセント カラーを表示するにはスタート メニューとタスク バーにテーマの色を表示するには、[スタート メニューとタスク バーにテーマを表示] をオンにします。

解決策: 組織では PIN を変更する必要があります。 解決策: 組織では PIN を変更する必要があります。 Oct 04, 2023 pm 05:45 PM

ログイン畫面に「組織から PIN の変更を求められています」というメッセージが表示されます。これは、個人のデバイスを制御できる組織ベースのアカウント設(shè)定を使用しているコンピューターで PIN の有効期限の制限に達(dá)した場合に発生します。ただし、個人アカウントを使用して Windows をセットアップした場合、エラー メッセージは表示されないのが理想的です。常にそうとは限りませんが。エラーが発生したほとんどのユーザーは、個人アカウントを使用して報(bào)告します。私の組織が Windows 11 で PIN を変更するように要求するのはなぜですか?アカウントが組織に関連付けられている可能性があるため、主なアプローチはこれを確認(rèn)することです。ドメイン管理者に問い合わせると解決できます。さらに、ローカル ポリシー設(shè)定が間違っていたり、レジストリ キーが間違っていたりすると、エラーが発生する可能性があります。今すぐ

Windows 11でタイトルバーの色を変更するにはどうすればよいですか? Windows 11でタイトルバーの色を変更するにはどうすればよいですか? Sep 14, 2023 pm 03:33 PM

デフォルトでは、Windows 11 のタイトル バーの色は、選択したダーク/ライト テーマによって異なります。ただし、任意の色に変更できます。このガイドでは、デスクトップ エクスペリエンスを変更し、視覚的に魅力的なものにするためにカスタマイズする 3 つの方法について、段階的な手順を説明します。アクティブなウィンドウと非アクティブなウィンドウのタイトル バーの色を変更することはできますか?はい、設(shè)定アプリを使用してアクティブなウィンドウのタイトル バーの色を変更したり、レジストリ エディターを使用して非アクティブなウィンドウのタイトル バーの色を変更したりできます。これらの手順を?qū)W習(xí)するには、次のセクションに進(jìn)んでください。 Windows 11でタイトルバーの色を変更するにはどうすればよいですか? 1. 設(shè)定アプリを使用して + を押して設(shè)定ウィンドウを開きます。 Windows「個人用設(shè)定」に進(jìn)み、

Windows 11 でタスクバーのサムネイル プレビューを有効または無効にする方法 Windows 11 でタスクバーのサムネイル プレビューを有効または無効にする方法 Sep 15, 2023 pm 03:57 PM

タスクバーのサムネイルは楽しい場合もありますが、気が散ったり煩わしい場合もあります。この領(lǐng)域にマウスを移動する頻度を考えると、重要なウィンドウを誤って閉じてしまったことが何度かある可能性があります。もう 1 つの欠點(diǎn)は、より多くのシステム リソースを使用することです。そのため、リソース効率を高める方法を探している場合は、それを無効にする方法を説明します。ただし、ハードウェアの仕様が対応可能で、プレビューが気に入った場合は、有効にすることができます。 Windows 11でタスクバーのサムネイルプレビューを有効にする方法は? 1. 設(shè)定アプリを使用してキーをタップし、[設(shè)定] をクリックします。 Windows では、「システム」をクリックし、「バージョン情報(bào)」を選択します。 「システムの詳細(xì)設(shè)定」をクリックします。 [詳細(xì)設(shè)定] タブに移動し、[パフォーマンス] の下の [設(shè)定] を選択します。 「視覚効果」を選択します

Windows 11で明るさを調(diào)整する10の方法 Windows 11で明るさを調(diào)整する10の方法 Dec 18, 2023 pm 02:21 PM

畫面の明るさは、最新のコンピューティング デバイスを使用する上で不可欠な部分であり、特に長時(shí)間畫面を見る場合には重要です。目の疲れを軽減し、可読性を向上させ、コンテンツを簡単かつ効率的に表示するのに役立ちます。ただし、設(shè)定によっては、特に新しい UI が変更された Windows 11 では、明るさの管理が難しい場合があります。明るさの調(diào)整に問題がある場合は、Windows 11 で明るさを管理するすべての方法を次に示します。 Windows 11で明るさを変更する方法【10の方法を解説】 シングルモニターユーザーは、次の方法でWindows 11の明るさを調(diào)整できます。これには、ラップトップだけでなく、単一のモニターを使用するデスクトップ システムも含まれます。はじめましょう。方法 1: アクション センターを使用する アクション センターにアクセスできる

Windows 11 でのディスプレイ スケーリング ガイド Windows 11 でのディスプレイ スケーリング ガイド Sep 19, 2023 pm 06:45 PM

Windows 11 のディスプレイ スケーリングに関しては、好みが人それぞれ異なります。大きなアイコンを好む人もいれば、小さなアイコンを好む人もいます。ただし、適切なスケーリングが重要であることには誰もが同意します。フォントのスケーリングが不十分であったり、畫像が過度にスケーリングされたりすると、作業(yè)中の生産性が大幅に低下する可能性があるため、システムの機(jī)能を最大限に活用するためにカスタマイズする方法を知る必要があります。カスタム ズームの利點(diǎn): これは、畫面上のテキストを読むのが難しい人にとって便利な機(jī)能です。一度に畫面上でより多くの情報(bào)を確認(rèn)できるようになります。特定のモニターおよびアプリケーションにのみ適用するカスタム拡張プロファイルを作成できます。ローエンド ハードウェアのパフォーマンスの向上に役立ちます。畫面上の內(nèi)容をより詳細(xì)に制御できるようになります。 Windows 11の使用方法

Windows Serverでアクティベーションエラーコード0xc004f069を修正する方法 Windows Serverでアクティベーションエラーコード0xc004f069を修正する方法 Jul 22, 2023 am 09:49 AM

Windows のライセンス認(rèn)証プロセスが突然切り替わり、このエラー コード 0xc004f069 を含むエラー メッセージが表示されることがあります。ライセンス認(rèn)証プロセスはオンラインですが、Windows Server を?qū)g行している一部の古いシステムではこの問題が発生する可能性があります。これらの初期チェックを?qū)g行し、システムのアクティブ化に役に立たない場合は、問題を解決するための主要な解決策に進(jìn)んでください?;乇懿?– エラー メッセージとアクティベーション ウィンドウを閉じます。次に、コンピュータを再起動します。 Windows ライセンス認(rèn)証プロセスを最初から再試行します。解決策 1 – ターミナルからアクティブ化する cmd ターミナルから Windows Server Edition システムをアクティブ化します。ステージ – 1 Windows Server のバージョンを確認(rèn)する 使用している W の種類を確認(rèn)する必要があります

React と RabbitMQ を使用して信頼性の高いメッセージング アプリを構(gòu)築する方法 React と RabbitMQ を使用して信頼性の高いメッセージング アプリを構(gòu)築する方法 Sep 28, 2023 pm 08:24 PM

React と RabbitMQ を使用して信頼性の高いメッセージング アプリケーションを構(gòu)築する方法 はじめに: 最新のアプリケーションは、リアルタイム更新やデータ同期などの機(jī)能を?qū)g現(xiàn)するために、信頼性の高いメッセージングをサポートする必要があります。 React はユーザー インターフェイスを構(gòu)築するための人気のある JavaScript ライブラリであり、RabbitMQ は信頼性の高いメッセージング ミドルウェアです。この記事では、React と RabbitMQ を組み合わせて信頼性の高いメッセージング アプリケーションを構(gòu)築する方法を紹介し、具體的なコード例を示します。 RabbitMQ の概要:

See all articles