热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

译PHPrabbitMQTutorial5

php教程|php手册-PHP,rabbitMQ,Tutorial-5,小雨,转晴php教程-php手册PHP源码ERP,ubuntu启动卡住不动,爬虫模块是什么,i()php,网

php教程|php手册译-PHP rabbitMQ Tutorial-5
-PHP,rabbitMQ,Tutorial-5,小雨,转晴
php教程-php手册
PHP源码ERP,ubuntu启动卡住不动,爬虫模块是什么,i()php,网页制作seolzw
Topics (usingphp-amqplib) In theprevious tutorialwe improved our logging system. Instead of using afanoutexchange only capable of dummy broadcasting, we used adirect one, and gained a possibility of selectively receiving the logs. 上一节,
微课堂v2源码,如何右键打开vscode,ubuntu使用vps,tomcat中设置编码,golangredis爬虫,php 语法 c,seo新手培训教程排名,美色视频系统网站,水利工程dedecms模板lzw
springmvc的项目源码下载,ubuntu邮件gmail,tomcat配置为路由模式,charles爬虫登录,php库存管理软件,西藏短视频seo优化怎么做lzw


Topics

(using php-amqplib)

In the previous tutorial we improved our logging system. Instead of using a fanout exchange only capable of dummy broadcasting, we used a direct one, and gained a possibility of selectively receiving the logs.

上一节,我们改进了日志系统。取代了fanout交换器仅仅会傻乎乎的广播,我们使用了定向交换器,使得选择性接收消息成为可能。

Although using the direct exchange improved our system, it still has limitations – it can’t do routing based on multiple criteria.

尽管使用定向交换器改善了我们的系统,但它仍有局限性——不能基于多重条件进行路由。

In our logging system we might want to subscribe to not only logs based on severity, but also based on the source which emitted the log. You might know this concept from the syslog unix tool, which routes logs based on both severity (info/warn/crit…) and facility (auth/cron/kern…).

在日志系统中,我们可能想不仅订阅基于严重等级的内容,而且订阅基于消息发布来源的内容。你可以从syslog unix工具中得知这个概念——基于严重性和设备路由日志。

That would give us a lot of flexibility – we may want to listen to just critical errors coming from ‘cron’ but also all logs from ‘kern’.

那会给我们带来很大的灵活性——我们可能想只是收听来自cron的致命错误和来自kern的所有消息。

To implement that in our logging system we need to learn about a more complex topic exchange.

为了在我们的日志系统上实现这种灵活性,我们需要学习一下更为复杂一些的topic交换器。


Topic exchange

Messages sent to a topic exchange can’t have an arbitrary routing_key – it must be a list of words, delimited by dots. The words can be anything, but usually they specify some features connected to the message. A few valid routing key examples: “stock.usd.nyse”, “nyse.vmw”, “quick.orange.rabbit”. There can be as many words in the routing key as you like, up to the limit of 255 bytes.

发送到topic交换器的消息不能给一个任意的routing_key——它必须是一个由逗号分隔的单词列表。

The binding key must also be in the same form. The logic behind the topic exchange is similar to a direct one – a message sent with a particular routing key will be delivered to all the queues that are bound with a matching binding key. However there are two important special cases for binding keys:

binding key也必须是同样的格式。topic交换器背后的逻辑和定向交换器类似——带有特定routing key的消息会被派送到所有捆绑了与routing key相匹配的binding key的队列。

* (star) can substitute for exactly one word.

*(星号)可以代表一个单词

# (hash) can substitute for zero or more words.

#(井号)可以代表零个或多个单词

It’s easiest to explain this in an example:

In this example, we’re going to send messages which all describe animals. The messages will be sent with a routing key that consists of three words (two dots). The first word in the routing key will describe speed, second a colour and third a species: “..”.

这个例子中,我们将发送所有描述动物的消息。发送时,这些消息带有由三个单词(两个点号分隔)组成的routing key.其中起一个单词用来表述速度,第二个用来表示颜色,最后一个用来表示种类:”..”

We created three bindings: Q1 is bound with binding key “*.orange.*” and Q2 with “*.*.rabbit” and “lazy.#”.

创建三个捆绑:用 “*.orange.*” 来绑定Q1,用 “*.*.rabbit” 和”lazy.#”来绑定Q2.

These bindings can be summarised as:

这几个捆绑可以概括为:

Q1 is interested in all the orange animals.

Q1喜欢所有橙色的动物

Q2 wants to hear everything about rabbits, and everything about lazy animals.

Q2想知道所有关于兔子和懒惰动物的事情

A message with a routing key set to “quick.orange.rabbit” will be delivered to both queues. Message “lazy.orange.elephant” also will go to both of them. On the other hand “quick.orange.fox” will only go to the first queue, and “lazy.brown.fox” only to the second. “lazy.pink.rabbit” will be delivered to the second queue only once, even though it matches two bindings. “quick.brown.fox” doesn’t match any binding so it will be discarded.

这两个队列都会接收到routing key设置成”quick.orange.rabbit”的消息。设置成”lazy.orange.elephant”的也一样。

但”quick.orange.fox”仅仅会进入第一个队列,而”lazy.brown.fox”则仅会进入第二个队列。”lazy.pink.rabbit”仅仅会进入到第二个队列一次,虽然它可以匹配到两个困难规则。由于”quick.brown.fox”和谁都匹配不上,所以会被丢弃。

What happens if we break our contract and send a message with one or four words, like “orange” or “quick.orange.male.rabbit”? Well, these messages won’t match any bindings and will be lost.

要是打破了我们的约束会怎样?比如,发送带有一个或是四个单词的,像”orange”或”quick.orange.male.rabbit”的消息。

好吧!消息会丢失,因为它不匹配任何捆绑规则。

On the other hand “lazy.orange.male.rabbit”, even though it has four words, will match the last binding and will be delivered to the second queue.

但是 “lazy.orange.male.rabbit”这种routing key,尽管它有四个单词,但是还是会进入到第二个队列(为喵呢?你来说说)


Putting it all together(合体!!!烦死了是吧?哈,还有一次!)

We’re going to use a topic exchange in our logging system. We’ll start off with a working assumption that the routing keys of logs will have two words: “.”.

在我们的日志系统中使用topic交换器。我们假设日志的routing keyes有”.”组成。

The code is almost the same as in the previous tutorial.

代码和之前的几乎一妈生的。

The code for emit_log_topic.php:

emit_log_topic.php代码:

channel();$channel->exchange_declare('topic_logs', 'topic', false, false, false);$routing_key = $argv[1];if(empty($routing_key)) $routing_key = "anonymous.info";$data = implode(' ', array_slice($argv, 2));if(empty($data)) $data = "Hello World!";$msg = new AMQPMessage($data);$channel->basic_publish($msg, 'topic_logs', $routing_key);echo " [x] Sent ",$routing_key,':',$data," \n";$channel->close();$connection->close();?>

The code for receive_logs_topic.php:

channel();$channel->exchange_declare('topic_logs', 'topic', false, false, false);list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);$binding_keys = array_slice($argv, 1);if( empty($binding_keys )) { file_put_contents('php://stderr', "Usage: $argv[0] [binding_key]\n"); exit(1);}foreach($binding_keys as $binding_key) { $channel->queue_bind($queue_name, 'topic_logs', $binding_key);}echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";$callback = function($msg){ echo ' [x] ',$msg->delivery_info['routing_key'], ':', $msg->body, "\n";};$channel->basic_consume($queue_name, '', false, true, false, false, $callback);while(count($channel->callbacks)) { $channel->wait();}$channel->close();$connection->close();?>

To receive all the logs:

接收所有日志:

$ php receive_logs_topic.php "#"

To receive all logs from the facility “kern”:

接收所有来自kern的日志:

$ phpreceive_logs_topic.php "kern.*"

Or if you want to hear only about “critical” logs:

或是你想仅仅接收”致命“日志

$ php receive_logs_topic.php "*.critical"

You can create multiple bindings:

你也可以多重绑定

$ php receive_logs_topic.php "kern.*" "*.critical"

And to emit a log with a routing key “kern.critical” type:

发布一个带有”kern.critical”routing key的日志就输入:

$ php emit_log_topic.php "kern.critical" "A critical kernel error"

Have fun playing with these programs. Note that the code doesn’t make any assumption about the routing or binding keys, you may want to play with more than two routing key parameters.

玩的开心哈!注意上面的代码没有做路由或捆绑的例子,有可能想体验两个以上的routing key参数。

Some teasers:

Will “*” binding catch a message sent with an empty routing key?

星号会匹配带有空routing key的消息吗?

Will “#.*” catch a message with a string “..” as a key? Will it catch a message with a single word key?

“#.*”会匹配”..”的消息吗? 它是匹配到一个单一的单词吗?

How different is “a.*.#” from “a.#”?

“a.*.#”和”a.#”有啥不同?

(Full source code for emit_log_topic.php and receive_logs_topic.php)

emit_log_topic.php和receive_logs_topic.php源码。

Next, find out how to do a round trip message as a remote procedure call in tutorial 6

下次,我们讲如何像远程过程调用一样完成信息往返。


推荐阅读
  • PHP输出缓冲控制Output Control系列函数详解【PHP】
    后端开发|php教程PHP,输出缓冲,Output,Control后端开发-php教程概述全景网页源码,vscode如何打开c,ubuntu强制解锁,sts启动tomcat慢,sq ... [详细]
  • PHP函数实现分页含文本分页和数字分页【PHP】
    后端开发|php教程PHP,分页后端开发-php教程最近,在项目中要用到分页。分页功能是经常使用的一个功能,所以,对其以函数形式进行了封装。影视网源码带充值系统,vscode配置根 ... [详细]
  • PHPMailer邮件类邮件发送功能的使用教学及注意事项
    本文介绍了使用国外开源码PHPMailer邮件类实现邮件发送功能的简单教学,同时提供了一些注意事项。文章涵盖了字符集设置、发送HTML格式邮件、群发邮件以及避免类的重定义等方面的内容。此外,还提供了一些与PHP相关的资源和服务,如传奇手游游戏源码下载、vscode字体调整、数据恢复、Ubuntu实验环境搭建、北京爬虫市场、进阶PHP和SEO人员需注意的内容。 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 分享css中提升优先级属性!important的用法总结
    web前端|css教程css!importantweb前端-css教程本文分享css中提升优先级属性!important的用法总结微信门店展示源码,vscode如何管理站点,ubu ... [详细]
  • mui框架offcanvas侧滑超出部分隐藏无法滚动如何解决
    web前端|js教程off-canvas,部分,超出web前端-js教程mui框架中off-canvas侧滑的一个缺点就是无法出现滚动条,因为它主要用途是设置类似于qq界面的那种格 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • Oracle 和 mysql的9点区别【MySQL】
    数据库|mysql教程oracle,Oracle,money,mysql,coun数据库-mysql教程1.组函数用法规则mysql中组函数在select语句中可以随意使用,但在o ... [详细]
  • ORACLE空间管理实验5:块管理之ASSM下高水位的影响
    数据库|mysql教程ORACLE,空间,管理,实验,ASSM,下高,水位,影响,数据库-mysql教程易语言黑客软件源码,vscode左侧搜索,ubuntu怎么看上一页,ecs搭 ... [详细]
  • 用PHP连接MySQL代码的参数说明【PHP】
    后端开发|php教程PHP,连接,MySQL,参数后端开发-php教程代码是这样的:大图标网站源码,怎么在vscode中调试css,ubuntu退出命令行,系统默认开tomcat, ... [详细]
  • 电脑f5键是什么作用
    常见问题f5常见问题韩亚整形医院源码,vscode写前端代码,ubuntu低配,tomcat下载路径乱码,爬虫_gscu,php精粹pdf,广州快速seo优化排名,aspwap网站 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 背景应用安全领域,各类攻击长久以来都危害着互联网上的应用,在web应用安全风险中,各类注入、跨站等攻击仍然占据着较前的位置。WAF(Web应用防火墙)正是为防御和阻断这类攻击而存在 ... [详细]
  • 本文介绍了Java集合库的使用方法,包括如何方便地重复使用集合以及下溯造型的应用。通过使用集合库,可以方便地取用各种集合,并将其插入到自己的程序中。为了使集合能够重复使用,Java提供了一种通用类型,即Object类型。通过添加指向集合的对象句柄,可以实现对集合的重复使用。然而,由于集合只能容纳Object类型,当向集合中添加对象句柄时,会丢失其身份或标识信息。为了恢复其本来面貌,可以使用下溯造型。本文还介绍了Java 1.2集合库的特点和优势。 ... [详细]
  • 本文介绍了在Web应用系统中,数据库性能是导致系统性能瓶颈最主要的原因之一,尤其是在大规模系统中,数据库集群已经成为必备的配置之一。文章详细介绍了主从数据库架构的好处和实验环境的搭建方法,包括主数据库的配置文件修改和设置需要同步的数据库等内容。MySQL的主从复制功能在国内外大型网站架构体系中被广泛采用,本文总结了作者在实际的Web项目中的实践经验。 ... [详细]
author-avatar
琦玉老师
迷茫的,没有热情的死肥宅
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有