简体   繁体   English

如何从 Python 脚本调用微服务

[英]How to invoke the microservice from a Python script

This code defines the services:此代码定义了服务:

from nameko.rpc import rpc


class GreetingService:
    name = "greeting_service"

    @rpc
    def hello(self, name):
        return "Hello, {}!".format(name)
    
class MultiplyService:
    name = "multiply_service"
    
    @rpc
    def multiple(self,a,b):
        return str(int(a) * int(b))

I have the services running:我有服务运行: 在此处输入图像描述

I can open a nameko shell and access the microservice:我可以打开一个 nameko shell 并访问微服务: 在此处输入图像描述 在此处输入图像描述

I want to access the microservice from another Python script.我想从另一个 Python 脚本访问微服务。 What am I missing?我错过了什么? 在此处输入图像描述

The n global is only available inside nameko shell . n全局仅在nameko shell内可用。

To invoke an RPC from a script, you need to use the one of the "standalone" clients in nameko.standalone.rpc .要从脚本调用 RPC,您需要使用nameko.standalone.rpc中的“独立”客户端之一。

Example in the docs here https://nameko.readthedocs.io/en/stable/built_in_extensions.html#rpc此处文档中的示例https://nameko.readthedocs.io/en/stable/built_in_extensions.html#rpc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM