[英]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
要在服务之间连接,您可以使用后台任务,例如
celery
和ramq
或使用nsq
和nats
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.