0

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.

M.E.
  • 4,955
  • 4
  • 49
  • 128
  • if you are already set up for nameko rpc calls, another option is to use those for all service-to-service communication. that way you don't need to reverse-proxy anything to anywhere except your "central microservice" – second Dec 16 '17 at 11:58
  • The answer to the linked question actually just says that the RPC proxy must be invoked inside a _service method_ (as opposed to on the class). The service method doesn’t also need to be an @rpc entrypoint – Matt Dec 16 '17 at 23:43
  • I finally add the registration process via RpcProxy invoking it inside an rpc method. That rpc method is called "register" and it is launched once via nameko shell in the script that launches all microservices. I found that the easiest way to achieve this. – M.E. Dec 18 '17 at 09:51

2 Answers2

0

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

Nameko - invoking RPC method from another service

M.E.
  • 4,955
  • 4
  • 49
  • 128
0

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