Automatic Service Discovery

From @authentik8 on Mon Mar 05 2018 17:01:00 GMT+0000 (UTC)

Is there a method/property in the Nameko MethodProxy / ServiceProxy API that allows for the retrieval of the available RPC methods on a given service?

Copied from original issue: https://github.com/nameko/nameko/issues/518

From @mattbennett on Tue Mar 06 2018 07:01:11 GMT+0000 (UTC)

Not out of the box, but it’s trivial to write a service that can introspect itself:

from collections import defaultdict

from nameko.extensions import DependencyProvider
from nameko.rpc import rpc


class EntrypointList(DependencyProvider):

    def get_dependency(self, worker_ctx):
        # all Extensions have access to self.container
        return list(self.container.entrypoints)


class Service:
    name = "discovery"

    entrypoint_list = EntrypointList()

    @rpc
    def introspect(self):
        entrypoints = defaultdict(list)
        for ep in self.entrypoint_list:
            entrypoints[ep.method_name].append(type(ep).__name__)
        return entrypoints
$ nameko shell
Nameko Python 3.5.3 (default, Jun 30 2017, 18:28:54)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] shell on darwin
Broker: pyamqp://guest:guest@localhost
>>> n.rpc.discovery.introspect()
{'introspect': ['Rpc']}
>>>