2. Main Components
● Transport
● Executors
● Target
● RPC Server
● RPC Client
Doc http://docs.openstack.org/developer/oslo.messaging/
3. Transport
from oslo.config import cfg
import oslo.messaging as om
cfg.CONF.set_override('rabbit_host', '192.168.56.101')
cfg.CONF.set_override('rabbit_port', 5672)
cfg.CONF.set_override('rabbit_userid', 'guest')
cfg.CONF.set_override('rabbit_password', 'cloud')
cfg.CONF.set_override('rabbit_login_method', 'AMQPLAIN')
cfg.CONF.set_override('rabbit_virtual_host', '/')
cfg.CONF.set_override('rpc_backend', 'rabbit')
transport = om.get_transport(cfg.CONF)
* This method will construct a Transport object from transport configuration
collected from the user’s configuration
5. Executors
Executors are providing the way an incoming message will be
dispatched so that the message can be used for meaningful
work. Different types of executors are supported, each with its
own set of restrictions and capabilities.
6. Target
* A Target encapsulates all the information to identify where a message should
be sent or what messages a server is listening for.
from oslo.config import cfg
import oslo.messaging as om
a)
Target Setup for creating a server:
* topic and server is required; exchange is optional
target = om.Target(topic='testme', server='192.168.56.102')
b)
Endpoint’s Target
* namespace and version is optional
c)
Target Setup for the client to sending a message:
* topic is required, all other attributes optional
target = om.Target(topic='testme')
7. RPC Server
An RPC server exposes a number of endpoints, each of which contain a set of
methods which may be invoked remotely by clients over a given transport.
To create an RPC server, you supply a transport, target and a list of endpoints.
from oslo.config import cfg
import oslo.messaging as om
##Create transport and target
transport = om.get_transport(cfg.CONF)
target = om.Target(topic='testme', server='192.168.56.102')
##Create EndPoints
class TestEndpoint(object):
def test_method1(self, ctx, arg):
return arg
endpoints = [TestEndpoint(),]
##Create RPC Server
server = om.get_rpc_server(transport, target, endpoints,
executor='blocking')
8. RPC Client
The RPCClient class is responsible for sending method invocations to remote
servers via a messaging transport.
A method invocation consists of a request context dictionary, a method name
and a dictionary of arguments. A cast() invocation just sends the request and
returns immediately. A call() invocation waits for the server to send a return
value.
##Create Messaging Transport and Target
transport = om.get_transport(cfg.CONF)
target = om.Target(topic='testme')
##Create RPC Client
client = om.RPCClient(transport, target)
##Invoke remote method and wait for a reply. (call)
arg = "Saju"
ctxt = {}
client.call(ctxt, 'test_method1', arg=arg)
##Invoke remote method and return immediately. (cast)
client.cast(ctxt, 'test_method1', arg=arg)
11. RabbitMQ Server (192.168.56.101)
*How ro restart rabbitmq-server
#sudo /etc/init.d/rabbitmq-server restart
* How to list all queues
#sudo rabbitmqctl list_queues
* How to list all queues and grep
#sudo rabbitmqctl list_queues | grep testme
12. RPC Server–1 (192.168.56.102)
from pprint import pprint
from oslo.config import cfg
import oslo.messaging as om
##Invoke "get_transport". This call will set default Configurations required to
Create Messaging Transport
transport = om.get_transport(cfg.CONF)
##Set/Override Configurations required to Create Messaging Transport
cfg.CONF.set_override('rabbit_host', '192.168.56.101')
cfg.CONF.set_override('rabbit_port', 5672)
cfg.CONF.set_override('rabbit_userid', 'guest')
cfg.CONF.set_override('rabbit_password', 'cloud')
cfg.CONF.set_override('rabbit_login_method', 'AMQPLAIN')
cfg.CONF.set_override('rabbit_virtual_host', '/')
cfg.CONF.set_override('rpc_backend', 'rabbit')
##Check the Configurations
res = [{k:v} for k, v in cfg.CONF.iteritems()]
pprint(res)
13. RPC Server–1 (192.168.56.102) Conti....
##Create Messaging Transport
transport = om.get_transport(cfg.CONF)
##Create Target (Exchange, Topic and Server to listen on)
target = om.Target(topic='testme', server='192.168.56.102')
##Create EndPoint
class TestEndpoint(object):
def test_method1(self, ctx, arg):
res = "Result from test_method1 " + str(arg)
print res
return res
def test_method2(self, ctx, arg):
res = "Result from test_method2 " + str(arg)
print res
return res
##Create EndPoint List
endpoints = [TestEndpoint(),]
14. RPC Server–1 (192.168.56.102) Conti....
##Create RPC Server
server = om.get_rpc_server(transport, target, endpoints, executor='blocking')
##Start RPC Server
server.start()
15. RPC Server–2 (192.168.56.103)
* Use the same code which used in RPC Server-1 and change server of the
Target to 192.168.56.103
##Create Target (Exchange, Topic and Server to listen on)
target = om.Target(topic='testme', server='192.168.56.103')
16. RPC Client (192.168.56.104)
from pprint import pprint
from oslo.config import cfg
import oslo.messaging as om
##Invoke "get_transport". This call will set default Configurations required to
Create Messaging Transport
transport = om.get_transport(cfg.CONF)
##Set Configurations required to Create Messaging Transport
cfg.CONF.set_override('rabbit_host', '192.168.56.101')
cfg.CONF.set_override('rabbit_port', 5672)
cfg.CONF.set_override('rabbit_userid', 'guest')
cfg.CONF.set_override('rabbit_password', 'cloud')
cfg.CONF.set_override('rabbit_login_method', 'AMQPLAIN')
cfg.CONF.set_override('rabbit_virtual_host', '/')
cfg.CONF.set_override('rpc_backend', 'rabbit')
##Check Configurations
res = [{k:v} for k, v in cfg.CONF.iteritems()]
pprint(res)
17. RPC Client (192.168.56.104) Conti...
##Create Messaging Transport
transport = om.get_transport(cfg.CONF)
##Create Target
target = om.Target(topic='testme')
##Create RPC Client
client = om.RPCClient(transport, target)
##Invoke remote method and wait for a reply. (call)
arg = "Saju"
ctxt = {}
client.call(ctxt, 'test_method1', arg=arg)
##Invoke remote method and return immediately. (cast)
client.cast(ctxt, 'test_method1', arg=arg)
18. RPC Client – Call, Cast and Fanout
● RPC Call
● RPC Cast
● Client send request to Specific Server
● Client send request to one of the servers in a
round-robin fashion
● Client send request to all the servers. (fanout)
19. RPC Call and Cast
##Create Target
target = om.Target(topic='testme')
##Create RPC Client
client = om.RPCClient(transport, target)
##RPC Call
ctxt = {}
for x in range(10):
client.call(ctxt, 'test_method1', arg=x)
##RPC Cast
ctxt ={}
for x in range(10):
client.cast(ctxt, 'test_method1', arg=x)
20. Client send request to Specific Server
##Create Target and specify the server where you want to send the request
target = om.Target(topic='testme', server='192.168.56.102')
##Create RPC Client
client = om.RPCClient(transport, target)
##RPC call
ctxt = {}
for x in range(10):
client.call(ctxt, 'test_method1', arg=x)
##RPC cast
ctxt = {}
for x in range(10):
client.cast(ctxt, 'test_method1', arg=x)
21. Client send request to one of the servers in a
round-robin fashion
##Create Target without any specific server
target = om.Target(topic='testme')
##Create RPC Client
client = om.RPCClient(transport, target)
##RPC Call
ctxt = {}
for x in range(10):
client.call(ctxt, 'test_method1', arg=x)
##RPC Cast
ctxt = {}
for x in range(10):
client.cast(ctxt, 'test_method1', arg=x)
22. Client send request to all the servers.
(fanout)
##Create Target and set fanout = True
target = om.Target(topic='testme', fanout=True)
##Create RPC Client
client = om.RPCClient(transport, target)
##RPC Call (Will not Support)
ctxt = {}
for x in range(10):
client.call(ctxt, 'test_method1', arg=x)
##RPC Cast. Fanout works with only RPC Cast.
ctxt = {}
for x in range(10):
client.cast(ctxt, 'test_method1', arg=x)
23. Oslo Config
An OpenStack library for parsing configuration
options from the command line and configuration
files.
http://docs.openstack.org/developer/oslo.config/
24. How to print all configurations
from pprint import pprint
from oslo.config import cfg
res = [{k:v} for k, v in cfg.CONF.iteritems()]
pprint(res)
25. How to register/unregister/override an option
in configuration
from pprint import pprint
from oslo.config import cfg
##Register
opts = [cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""),]
cfg.CONF.register_opts(opts)
OR
cfg.CONF.register_opt(cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""))
##Unregister
opts = [cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""),]
cfg.CONF.unregister_opts(opts)
OR
cfg.CONF.unregister_opt(cfg.StrOpt('api_server_ip', default='127.0.0.1',
help=""))
##Override
cfg.CONF.set_override('transport_url', None)