本文主要是介绍基于openstack oslo_messaging的python rpc远程调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码很简单首先启动服务端
from oslo_config import cfg
import oslo_messaging
import timeclass ServerControlEndpoint(object):def __init__(self, server):self.server = serverdef stop(self, ctx):if self.server:self.server.stop()class TestEndpoint(object):def test(self, ctx, a,b):return a+b
#cfg.CONF.set_override("rabbit_host","192.168.31.230")
transport_url = 'rabbit://admin:password@192.168.31.230:5672/'
transport = oslo_messaging.get_transport(cfg.CONF,transport_url)
target = oslo_messaging.Target(topic='test', server='server1')
endpoints = [ServerControlEndpoint(None),TestEndpoint(),
]
server = oslo_messaging.get_rpc_server(transport, target, endpoints,executor='blocking')
try:server.start()while True:time.sleep(1)
except KeyboardInterrupt:print("Stopping server")server.stop()
server.wait()
这里需要先安装rabbitmq
客户端代码如下:
from oslo_config import cfg
import oslo_messagingtransport_url = 'rabbit://admin:password@192.168.31.230:5672/'
transport = oslo_messaging.get_transport(cfg.CONF,transport_url)
target = oslo_messaging.Target(topic='test', server='server1')
client = oslo_messaging.RPCClient(transport, target)
r = client.call({}, 'test',a=2,b=3)
print rr = client.call({}, 'stop')
print 'success'
那么就可以远程调用服务器端的test和stop方法了
在openstack中远程调用的也是通过这种方式实现的,只不过配置是放在CONF中
这篇关于基于openstack oslo_messaging的python rpc远程调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!