Top Banner
Oslo messaging Oslo messaging RPC API
26

OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Jul 17, 2015

Download

Software

Saju Madhavan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Oslo messaging

Oslo messaging RPC API

Page 2: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Main Components● Transport

● Executors

● Target

● RPC Server

● RPC Client

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

Page 3: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Transport

from oslo.config import cfgimport 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

Page 4: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

OpenStack Training Videos

Play Training Videoshttps://www.youtube.com/user/sajuptpm/videos

Page 5: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

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.

Page 6: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

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 cfgimport oslo.messaging as om

a)Target Setup for creating a server:* topic and server is required; exchange is optionaltarget = 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 optionaltarget = om.Target(topic='testme')

Page 7: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC ServerAn 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 cfgimport oslo.messaging as om

##Create transport and targettransport = om.get_transport(cfg.CONF)target = om.Target(topic='testme', server='192.168.56.102')

##Create EndPointsclass TestEndpoint(object): def test_method1(self, ctx, arg): return argendpoints = [TestEndpoint(),]

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

Page 8: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC ClientThe 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 Targettransport = om.get_transport(cfg.CONF)target = om.Target(topic='testme')

##Create RPC Clientclient = 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)

Page 9: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Demohttps://www.youtube.com/watch?v=Bf4gkeoBzvA

Page 10: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Demo Setup

● RabbitMQ Server (192.168.56.101)

● RPC Server–1 (192.168.56.102)

● RPC Server–2 (192.168.56.103)

● RPC Client (192.168.56.104)

Page 11: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

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

Page 12: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC Server–1 (192.168.56.102)

from pprint import pprintfrom oslo.config import cfgimport oslo.messaging as om

##Invoke "get_transport". This call will set default Configurations required to Create Messaging Transporttransport = om.get_transport(cfg.CONF)

##Set/Override Configurations required to Create Messaging Transportcfg.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 Configurationsres = [{k:v} for k, v in cfg.CONF.iteritems()]pprint(res)

Page 13: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC Server–1 (192.168.56.102) Conti....##Create Messaging Transporttransport = 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 EndPointclass 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 Listendpoints = [TestEndpoint(),]

Page 14: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC Server–1 (192.168.56.102) Conti....

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

##Start RPC Serverserver.start()

Page 15: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

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

Page 16: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC Client (192.168.56.104)from pprint import pprintfrom oslo.config import cfgimport oslo.messaging as om

##Invoke "get_transport". This call will set default Configurations required to Create Messaging Transporttransport = om.get_transport(cfg.CONF)

##Set Configurations required to Create Messaging Transportcfg.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 Configurationsres = [{k:v} for k, v in cfg.CONF.iteritems()]pprint(res)

Page 17: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC Client (192.168.56.104) Conti...

##Create Messaging Transporttransport = om.get_transport(cfg.CONF)

##Create Targettarget = om.Target(topic='testme')

##Create RPC Clientclient = 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)

Page 18: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

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)

Page 19: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

RPC Call and Cast

##Create Targettarget = om.Target(topic='testme')

##Create RPC Clientclient = om.RPCClient(transport, target)

##RPC Callctxt = {}for x in range(10): client.call(ctxt, 'test_method1', arg=x)

##RPC Castctxt ={}for x in range(10): client.cast(ctxt, 'test_method1', arg=x)

Page 20: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

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 Clientclient = om.RPCClient(transport, target)

##RPC callctxt = {}for x in range(10): client.call(ctxt, 'test_method1', arg=x)

##RPC castctxt = {}for x in range(10): client.cast(ctxt, 'test_method1', arg=x)

Page 21: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Client send request to one of the servers in a round-robin fashion

##Create Target without any specific servertarget = om.Target(topic='testme')

##Create RPC Clientclient = om.RPCClient(transport, target)

##RPC Callctxt = {}for x in range(10): client.call(ctxt, 'test_method1', arg=x)

##RPC Castctxt = {}for x in range(10): client.cast(ctxt, 'test_method1', arg=x)

Page 22: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Client send request to all the servers. (fanout)

##Create Target and set fanout = Truetarget = om.Target(topic='testme', fanout=True)

##Create RPC Clientclient = 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)

Page 23: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Oslo Config

An OpenStack library for parsing configuration options from the command line and configuration

files.

http://docs.openstack.org/developer/oslo.config/

Page 24: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

How to print all configurations

from pprint import pprintfrom oslo.config import cfg

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

Page 25: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

How to register/unregister/override an option in configuration

from pprint import pprintfrom oslo.config import cfg

##Registeropts = [cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""),]cfg.CONF.register_opts(opts)

ORcfg.CONF.register_opt(cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""))

##Unregisteropts = [cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""),]cfg.CONF.unregister_opts(opts)

ORcfg.CONF.unregister_opt(cfg.StrOpt('api_server_ip', default='127.0.0.1', help=""))

##Overridecfg.CONF.set_override('transport_url', None)

Page 26: OpenStack Oslo Messaging RPC API Tutorial Demo Call, Cast and Fanout

Thanks

● Email: [email protected]

● Training Videos: https://www.youtube.com/user/sajuptpm/videos

● WebSite: http://fosshelp.blogspot.in

● IRC: saju_m

● Skype: sajuptpm