Neutron RPC API Layer

Client Side

Here is an example of an rpc client definition:

import oslo_messaging

from neutron.common import rpc as n_rpc


class ClientAPI(object):
    """Client side RPC interface definition.

    API version history:
        1.0 - Initial version
        1.1 - Added my_remote_method_2
    """

    def __init__(self, topic):
        target = oslo_messaging.Target(topic=topic, version='1.0')
        self.client = n_rpc.get_client(target)

    def my_remote_method(self, context, arg1, arg2):
        cctxt = self.client.prepare()
        return cctxt.call(context, 'my_remote_method', arg1=arg1, arg2=arg2)

    def my_remote_method_2(self, context, arg1):
        cctxt = self.client.prepare(version='1.1')
        return cctxt.call(context, 'my_remote_method_2', arg1=arg1)
my_remote_method 在v1中,
my_remote_method_2 v1.1中,当调用这个api是,其指定了server端至少实现了v1.1

Server Side

The server side of an rpc interface looks like this:

import oslo_messaging


class ServerAPI(object):

    target = oslo_messaging.Target(version='1.1')

    def my_remote_method(self, context, arg1, arg2):
        return 'foo'

    def my_remote_method_2(self, context, arg1):
        return 'bar'

server端说明了支持v1.1

Versioning

rpc interfaces change必须向后兼容。server必须在同一大版本下支持旧的client。

Example Change

比如说my_remote_method_2里增加一个参数,该参数必须有个default值向后兼容,升级小版本

server side code would look like this:

import oslo_messaging


class ServerAPI(object):

    target = oslo_messaging.Target(version='1.2')

    def my_remote_method(self, context, arg1, arg2):
        return 'foo'

    def my_remote_method_2(self, context, arg1, arg2=None):
        if not arg2:
            # Deal with the fact that arg2 was not specified if needed.
        return 'bar'

The client must also specify that version ‘1.2’ is required for this method call to be successful.

import oslo_messaging

from neutron.common import rpc as n_rpc


class ClientAPI(object):
    """Client side RPC interface definition.

    API version history:
        1.0 - Initial version
        1.1 - Added my_remote_method_2
        1.2 - Added arg2 to my_remote_method_2
    """

    def __init__(self, topic):
        target = oslo_messaging.Target(topic=topic, version='1.0')
        self.client = n_rpc.get_client(target)

    def my_remote_method(self, context, arg1, arg2):
        cctxt = self.client.prepare()
        return cctxt.call(context, 'my_remote_method', arg1=arg1, arg2=arg2)

    def my_remote_method_2(self, context, arg1, arg2):
        cctxt = self.client.prepare(version='1.2')
        return cctxt.call(context, 'my_remote_method_2',
                          arg1=arg1, arg2=arg2)

 

Example: DHCP

The DHCP agent includes a client API, neutron.agent.dhcp.agent.DhcpPluginAPI.

The server side is defined in neutron.api.rpc.handlers.dhcp_rpc.DhcpRpcCallback.

Similarly, there is an RPC interface defined that allows the Neutron plugin to remotely invoke methods in the DHCP agent.

The client side is defined in neutron.api.rpc.agentnotifiers.dhcp_rpc_agent_api.DhcpAgentNotifyApi.

The server side of this interface that runs in the DHCP agent is neutron.agent.dhcp.agent.DhcpAgent.

 

http://docs.openstack.org/developer/neutron/devref/rpc_api.html

 

posted on 2016-05-13 11:27  allcloud  阅读(595)  评论(0编辑  收藏  举报

导航