oslo_messaging源于Openstack的一个经典的模块,用以实现服务间的RPC通信。Client端将数据放入rabbitmq中,server端从消息队列中获取传送数据。

oslo.messaging库就是把rabbitmq的python库做了封装,考虑到了编程友好、性能、可靠性、异常的捕获等诸多因素。让各个项目的开发者聚焦于业务代码的编写,而不用考虑消息如何发送和接收。

一张比较经典的图见下:

Target:作为消息发送者,需要在target中指定消息要发送到的topic,exchange, binding-key, consumer等信息。

Transport(传输层)主要实现RPC底层的通信(比如socket)以及事件循环,多线程等其他功能.可以通过URL来获得不同transport的句柄.URL的格式为:

transport://user:password@host:port[,hostN:portN]/virtual_host

目前支持的Transport有rabbit,qpid与zmq,分别对应不同的后端消息总线.用户可以使用oslo.messaging.get_transport函数来获得transport对象实例的句柄.

Notifier:消息的发送端,可以在不同的优先级别上发送通知,这些优先级包括sample,critical,error,warn,info,debug,audit等

Notification Listener和Server类似,一个Notification Listener对象可以暴露多个endpoint,每个endpoint包含一组方法.但是与Server对象中的endpoint不同的是,这里的endpoint中的方法对应通知消息的不同优先级。在发送消息时,指定方法info,warn等,在notifer listener监听消息队列,使用dispatcher对象根据消息的publish_id, event_type将消息路由到不同的endpoint方法上。

举个例子,在notifier listener端程序见下:

 from  oslo_config import cfg
import oslo_messaging class NotificationEndpoint(object):
# filter_rule = oslo_messaging.NotificationFilter(
# publish_id='^compute.*')
def warn(self, ctxt, publish_id, event_type, payload, metadata):
print "caesar==> %s" % payload class ErrorEndpoint(object):
# filter_rule = oslo_messaging.NotificationFilter(
# event_type='^instance\..*\.start',
# context={'ctxt_key':'regexp'}) def error(self, ctxt, publish_id, event_type, payload, metadata):
print "caesar==> %s" % payload transport = oslo_messaging.get_notification_transport(cfg.CONF)
endpoints = [
NotificationEndpoint(),
ErrorEndpoint()
]
targets = [
oslo_messaging.Target(topic='notification'),
oslo_messaging.Target(topic='notification_bis')
] server = oslo_messaging.get_notification_listener(transport, targets,
endpoints)
server.start()
server.wait()

程序中,两个endpoint中分别有error和warn方法,当开启服务时,会创建四个topic消息 队列,见下:

在客户端,通过notifier中topic和方法,比如topic=notification 方法为error,即可以向notification.error队列中传入数据。

 from oslo_config import cfg
import oslo_messaging as messaging transport = messaging.get_transport(cfg.CONF)
notifier = messaging.Notifier(transport, driver='messaging', topics=['notification'])
project_id = 'b23a5e41d1af4c20974bf58b4dff8e5a'
user_id = 'ceb61464a3d341ebabdf97d1d4b97099'
notifier.error(ctxt={},
event_type='my_type',
payload={
'tenant_id': project_id,
'user_id': user_id,
'instance_id': '',
'instance_type_id': 1,
'instance_type': 'm1.flavor',
'state': 'active' })

执行notifier程序,查询消息队列为空,即已经被notification listnener消费,消息无阻塞。:

在notification listnener 路由到ErrorEndpoint的error方法,打印结果见下:

基于oslo_messaging的RPC通信的更多相关文章

  1. RPC通信框架——RCF介绍

    现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...

  2. RPC通信框架——RCF介绍(替换COM)

    阅读目录 RPC通信框架 为什么选择RCF 简单的性能测试 参考资料 总结 现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实 ...

  3. RPC通信原理(未完,先睡觉)

    一 背景 OpenStack 各组件之间是通过 REST 接口进行相互通信,比如Nova.Cinder.Neutron.Glance直间的通信都是通过keystone获取目标的endpoint,即ap ...

  4. 基于框架的RPC通信技术原理解析

    RPC的由来 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时, ...

  5. 【Java】分布式RPC通信框架Apache Thrift 使用总结

    简介 Apache Thrift是Facebook开源的跨语言的RPC通信框架,目前已经捐献给Apache基金会管理,由于其跨语言特性和出色的性能,在很多互联网公司得到应用,有能力的公司甚至会基于th ...

  6. 基于Netty的RPC简易实现

    代码地址如下:http://www.demodashi.com/demo/13448.html 可以给你提供思路 也可以让你学到Netty相关的知识 当然,这只是一种实现方式 需求 看下图,其实这个项 ...

  7. openstack RPC通信

    openstack RPC通信 OpenStack 的主要组件有 Nova.Cinder.Neutron.Glance 等,分别负责云平台的计算.存储.网络资源管理.openstack 各组件之间是通 ...

  8. RPC通信功能实现

    Table of Contents RPC通信功能实现 配置參数 调用方法 RPC通信功能实现 HBase的RPC通信功能主要基于Protobuf和NIO这两个组件来实现.在通信管道上选择的是prot ...

  9. RPC通信框架——RCF介绍

    现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...

随机推荐

  1. python-性能测试

    目录: 1.timeit 1.1 在命令后调用timeit 1.2 在代码中使用 1.3 创建计时器实例,通过autorange获得循环次数 1.4 Wall时间和CPU时间 2.profile和cP ...

  2. JSP 调用java 常量 枚举

    JAVA:public enum ReimStatus { UNCONFIRMED ("118001"), //未确认 DISPATCH_VERIFY("118002&q ...

  3. div的全屏与退出全屏

    div的全屏与退出全屏 作用:将div全屏与退出全屏,一般播放器使用较多. html按钮: <button onclick="showFull();"> 全屏 < ...

  4. 交叉编译zookeeper的C库

    原创文章,转载请正确注明本文原始URL及作者. 今天要编译一个zookeeper的C接口,要编译一个TK1版本. 事情经过这这样的:原来用的是zookeeper-3.4.6版本,但是源码中有个函数是汇 ...

  5. MySQL大数据量分页查询方法及其优化

    MySQL大数据量分页查询方法及其优化   ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适 ...

  6. 对比深度学习十大框架:TensorFlow 并非最好?

    http://www.oschina.net/news/80593/deep-learning-frameworks-a-review-before-finishing-2016 TensorFlow ...

  7. How to Pronounce the Days of the Week

    How to Pronounce the Days of the Week Share Tweet Share Vocabulary study:  how to pronounce the days ...

  8. window 服务

    c#写windows服务   序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用W ...

  9. LeetCode OJ 60. Permutation Sequence

    题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...

  10. 将 DNSCrypt 部署到 Openwrt 路由器上+ DNSmasq 解析国内域名用本地 DNS[ZT+实践]

    原文地址: 1.https://typcn.com/legacy/blog/posts/openwrt-dnscypt.html 2.http://www.openwrt.pro/post-376.h ...