基于oslo_messaging的RPC通信
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通信的更多相关文章
- RPC通信框架——RCF介绍
现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...
- RPC通信框架——RCF介绍(替换COM)
阅读目录 RPC通信框架 为什么选择RCF 简单的性能测试 参考资料 总结 现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实 ...
- RPC通信原理(未完,先睡觉)
一 背景 OpenStack 各组件之间是通过 REST 接口进行相互通信,比如Nova.Cinder.Neutron.Glance直间的通信都是通过keystone获取目标的endpoint,即ap ...
- 基于框架的RPC通信技术原理解析
RPC的由来 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时, ...
- 【Java】分布式RPC通信框架Apache Thrift 使用总结
简介 Apache Thrift是Facebook开源的跨语言的RPC通信框架,目前已经捐献给Apache基金会管理,由于其跨语言特性和出色的性能,在很多互联网公司得到应用,有能力的公司甚至会基于th ...
- 基于Netty的RPC简易实现
代码地址如下:http://www.demodashi.com/demo/13448.html 可以给你提供思路 也可以让你学到Netty相关的知识 当然,这只是一种实现方式 需求 看下图,其实这个项 ...
- openstack RPC通信
openstack RPC通信 OpenStack 的主要组件有 Nova.Cinder.Neutron.Glance 等,分别负责云平台的计算.存储.网络资源管理.openstack 各组件之间是通 ...
- RPC通信功能实现
Table of Contents RPC通信功能实现 配置參数 调用方法 RPC通信功能实现 HBase的RPC通信功能主要基于Protobuf和NIO这两个组件来实现.在通信管道上选择的是prot ...
- RPC通信框架——RCF介绍
现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...
随机推荐
- JAVA 操作mysql 存储 调用
HashMap map = new HashMap(); map.put("a", 9); map.put("b", ""); List&l ...
- windows7下搭建robot framework环境指导
第一步 安装Python并设置环境变量 1.安装python: python下载地址https://www.python.org/,建议用2.7.x版本 2.设置环境变量: 方法如下所示 第二步 安装 ...
- 15.纯 CSS 创作条形图,不用任何图表库
原文代码:https://segmentfault.com/a/1190000014768534#articleHeader1 HTML代码: <html> <head> &l ...
- Oracle生成关闭外键的SQL语句
select 'alter table ' || t.table_name || ' disable constraint ' || t.constraint_name || ';' from DBA ...
- 【3-30】document获取、事件、标记样式
一.获取标记对象 1.id选择:document.getelementbyid("id名")---根据id找单个元素 2.class选择:document.getelementsb ...
- python 生成器 的send
>>> def f(): c=yield 5 print c d=yield c+5 print d >>> b=f() >>> b.send(N ...
- canal 入门(基于docker)
第一步:安装MySQL:(可以参考:https://my.oschina.net/amhuman/blog/1941540) 命令: sudo docker run -it -d --restart ...
- 安装 docker管理 工具 页面 portainer
sudo docker run -d -p 7998:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data ...
- Oracle掌管权限和角色
转自:https://blog.csdn.net/without_bont/article/details/79862112 掌管权限和角色 这一部分我们主要看oracle中如何管理权限和角色,权限和 ...
- ubuntu编译安装nginx
下载nginx源码, ./configure --prefix=/usr/local/nginx/1.8 --with-http_ssl_module --with-http_dav_module - ...