Custom Search

Sunday, February 1, 2015

OpenStack Oslo messaging RPC API Example Call, Cast and Fanout

1) RPC Server
===========




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 Configurations
res = [{k:v} for k, v in cfg.CONF.iteritems()]
pprint(res)


##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(),]

##Create RPC Server
server = om.get_rpc_server(transport, target, endpoints, executor='blocking')

##Start RPC Server
server.start()

2) RPC Client
===========




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)


##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.
arg = "Saju"
ctxt = {}
client.call(ctxt, 'test_method1', arg=arg)


##Invoke remote method and return immediately.
arg = "Saju"
ctxt = {}
client.cast(ctxt, 'test_method1', arg=arg)


3)
RPC Client Options
===============

a) 
RPC Client 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)


b)
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)


c)
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)ctxt = {}


d)
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)



Ref: http://docs.openstack.org/developer/oslo.messaging/


3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Sorry for accidental removal of comment..

      This is a Great video.. Thank you for explaining RabbitMQ in so simple manner..

      Delete
  2. BlueHost is ultimately one of the best website hosting provider with plans for all of your hosting needs.

    ReplyDelete