简体   繁体   English

如何使用 python 从 microservice-1 调用 microservice-2?

[英]How to call microservice-2 from microservice-1 using python?

How to call my microservice-2 from microservice-1.如何从 microservice-1 调用我的 microservice-2。 So our result looks like this:-所以我们的结果是这样的:-

Result:- {“message”: “vivek”} --> {“message”: “keviv”, “random”: 3.89}结果:- {“消息”:“vivek”} --> {“消息”:“keviv”,“随机”:3.89}

command to access microservice-1:-访问 microservice-1 的命令:-

curl http://127.0.0.1:5000/reverse_random/vivek

microservice-1微服务-1

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/reverse_reandom/<string:string>', methods=['GET'])
def reverse(string):
    string = string[::-1]
    return jsonify({'message': string })


if __name__ == '__main__':
   app.run(debug = True)

microservice-2微服务-2

import random
from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/', methods=['GET'])
def myRandom():
    r1 = random.uniform(0, 10)
    return jsonify({'message': r1 })

if __name__ == '__main__':
    app.run(debug=True)

you'll need to issue a GET request to service 2 in order to get the random number, I suggest to use requests for this, like您需要向service 2发出GET请求才能获取随机数,我建议对此使用请求,例如

r = requests.get('url-for-service-2:port/')
data = r.json()
random_num = data['message']

keep in mind to check the data object for message key, or using .get() or equivalent请记住检查data object 的message密钥,或使用.get()或等效

Run microservice-2 on a different port.在不同的端口上运行 microservice-2。 Send request using Python standard or 3rd party library from microservice-1 to microservice-2 upon request to microservice-1.根据对微服务 1 的请求,使用 Python 标准或第三方库从微服务 1 向微服务 2 发送请求。

Below is the example of using Python3 standard library only:以下是仅使用 Python3 标准库的示例:

m1.py: m1.py:

from flask import Flask, jsonify
import urllib
import json 

app = Flask(__name__)

@app.route('/reverse_random/<string:string>', methods=['GET'])
def reverse(string):
    content = urllib.request.urlopen('http://127.0.0.1:5001').read().decode('utf-8')
    print('response from m2: ', content)
    string = string[::-1]
    return jsonify({'message': string, 'random' : json.loads(content)['message']})


if __name__ == '__main__':
   app.run(debug = True)

m2.py : m2.py

import random
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/', methods=['GET'])
def myRandom():
    r1 = random.uniform(0, 10)

    return jsonify({'message': r1 })

if __name__ == '__main__':
    app.run(debug=True, port=5001) # running m2 on a different port than default 5000

Run the m1: python3 m1.py运行 m1: python3 m1.py

Run the m2 in a different shell: python3 m2.py在不同的 shell 中运行 m2: python3 m2.py

Send request to m1: curl http://127.0.0.1:5000/reverse_random/vivek向m1发送请求: curl http://127.0.0.1:5000/reverse_random/vivek

The result is:结果是:

{ 
  "message": "keviv",  
  "random": 4.138115905045612 
} 

Observe the log of m1 and of m2 to make sure m2 was invoked.观察 m1 和 m2 的日志,确保调用了 m2。

To connect between services you can use background tasks such as celery and ramq or use nsq and nats要在服务之间连接,您可以使用后台任务,例如celeryramq或使用nsqnats

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

相关问题 如何从 Python 脚本调用微服务 - How to invoke the microservice from a Python script 使用微服务的Python Falcon微服务? - Python Falcon microservice that uses microservice? 如何将文件从 Bamboo 任务发送到我们的 python 微服务 - How to send a file from Bamboo task to a our python Microservice Python 微服务如何在 Authorization 标头中使用 JWT 验证请求? - How can a Python Microservice verify requests using a JWT in an Authorization header? gcloud应用引擎灵活-从微服务python中删除标头 - gcloud app engine flexible - removing header from microservice python 如何从 Python 中的 Kubernetes 集群中获取已部署微服务的 Helm 图表版本 - How to fetch the Helm chart version of an already deployed microservice from Kubernetes Cluster in Python 如何保护从 Django 到 Flask 微服务的通信? - How can i secure communications from Django to a Flask microservice? 使用node.js实现金融微服务| python | [R - implement financial microservice using node.js | python | R 客户端、NestJS 后端和 Python 微服务的架构和交互 - Architecture and interaction of the client, NestJS backend, and Python microservice 使用来自另一个应用程序(微服务)的参数渲染模板 - Render template with params from another app (microservice)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM