Nameko - Invoke remote procedure just once when service is loaded

555 views Asked by At

As per last question about invoking remote RPCs, an additional example to the one included in the documentation would be this one:

$ cat nameko.sh
#!/bin/bash
/usr/local/bin/nameko run service1:Microservice1 &
nameko_id=$!
echo 'Microservice 1 PID: ' $nameko_id
/usr/local/bin/nameko run service2:Microservice2 &
nameko_id=$!
echo 'Microservice 2 PID: ' $nameko_id
wait 2> /dev/null


$ cat service1.py
# -*- coding: utf-8 -*-
from nameko.rpc import rpc, RpcProxy
class Microservice1(object):
    name = "microservice1"
    @rpc
    def hello(self):
        print 'Microservice1 hello method invoked'
        return True

$ cat service2.py
# -*- coding: utf-8 -*-
from nameko.rpc import rpc, RpcProxy
class Microservice2(object):
    name = "microservice2"
    microservice1 = RpcProxy('microservice1')
    @rpc
    def remote_hello(self):
        print 'Microservice2 invokes hello method from Microservice1'
        self.microservice1.hello()
        return True

I am trying to build an architecture where microservices register themselves against a central microservice on startup (basically this central microservice is in charge of displaying a REST API, where each microservice deal with their part of the REST API -this is done to avoid the usage of a reverse proxy and deal with the port numbers-).

In Nameko, how could you launch a remote procedure just when the microservice is registered? As mentioned in the above post's answers, remote RPC invoke can not be done outside a @rpc method.

2

There are 2 answers

0
M.E. On

Actually stand alone proxy as answered here is the way to achive this:

Nameko - invoking RPC method from another service

0
Edwin He On

use the once entrypoint

from nameko.testing.service import once

class Microservice2(object):
name = "microservice2"
microservice1 = RpcProxy('microservice1')
@rpc
def remote_hello(self):
    print 'Microservice2 invokes hello method from Microservice1'
    self.microservice1.hello()
    return True