Custom Search

Sunday, February 1, 2015

OpenStack oslo messaging and config examples

* How to print configuration using oslo.config

from pprint import pprint
from oslo.config import cfg

res = [{k:v} for k, v in cfg.CONF.iteritems()]
pprint(res)


==============

* How to register/unregister/override an option in configuration using oslo.config


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)

##Print
res = [{k:v} for k, v in cfg.CONF.iteritems()]
pprint(res)






==============

* RPC Server
--------------------


#vim /usr/local/lib/python2.7/dist-packages/oslo.messaging-1.4.1.dist-info/entry_points.txt

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', '127.0.0.1')
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)

##Set/Override Configurations required to Create Target (Exchange, Topic and Server to listen on)
>>> cfg.CONF.set_override('control_exchange', 'openstack')

##Create Target (Exchange, Topic and Server to listen on)
>>> target = om.Target(topic='testme', server='127.0.0.1')

>>> target.namespace
>>> target.topic
'testme'
>>> target.version
>>> target.fanout
>>> target.server
'127.0.0.1'
>>> target.exchange

##Create End Points
class TestEndpoint(object):
    def test(self, ctx, arg):
        return arg

endpoints = [TestEndpoint(),]

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

>>> server.dispatcher

>>> server.transport

>>> server.executor
'blocking'
>>> server.start
>
>>>

##Check rabbitmq queue
#sudo rabbitmqctl list_queues | grep testme
testme    0
testme.127.0.0.1    0
testme_fanout_ba1e644ad10e40fc881793445b5e42bf    0

##Debug RPC server
#vim /usr/local/lib/python2.7/dist-packages/oslo/messaging/server.py
Add debug statement "import pdb; pdb.set_trace()" in method "start".
   

* 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', '127.0.0.1')
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 named "test"

ctxt={}
arg="blabla"
client.call(ctxt, 'test', arg=arg)

==============

RPC Server Code
-----------------------


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', '127.0.0.1')
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='10.0.2.15')

##Create End Points
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

endpoints = [TestEndpoint(),]


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

##Start RPC Server
server.start()

RPC Client Code
-----------------------


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', '127.0.0.1')
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', server='10.0.2.15')

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

==============

a)
Client Request to Specific Server

##Create Target
target = om.Target(topic='testme', server='127.0.0.1')
##Create RPC Client
client = om.RPCClient(transport, target)
for x in range(10):
    client.call(ctxt, 'test_method1', arg=x)

b)
Client Request to one of the servers in a round-robin fashion.

##Create Target
target = om.Target(topic='testme')
##Create RPC Client
client = om.RPCClient(transport, target)
for x in range(10):
    client.call(ctxt, 'test_method1', arg=x)

b)
Client Request to all the servers. (fanout)

##Create Target
target = om.Target(topic='testme')
##Create RPC Client
client = om.RPCClient(transport, target, fanout=True)
for x in range(10):
    client.call(ctxt, 'test_method1', arg=x)

No comments:

Post a Comment