feat: python发布消息到nats-server
This commit is contained in:
parent
743d2ef1d0
commit
fe13c5840b
|
@ -1,22 +1,20 @@
|
|||
from flask import Flask, jsonify
|
||||
from pynats2 import NATSClient
|
||||
from datetime import datetime,timedelta
|
||||
import json
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/publish', methods=['POST'])
|
||||
def publish_to_nats():
|
||||
data_dict = {
|
||||
"key": "value",
|
||||
"number": 123,
|
||||
"list": [1, 2, 3]
|
||||
standardLog = {
|
||||
"timestamp": (datetime.utcnow()+timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
"level": 'INFO', # DEBUG、INFO、WARNING、ERROR、CRITICAL等级别
|
||||
"name": 'alg.slice',# 日志生产的模块、服务
|
||||
"message": "算法版本:$1,分割结果:$2",# 日志记录模板
|
||||
"customFields": {
|
||||
"$1": "1.0.0",
|
||||
"$2": True
|
||||
},
|
||||
}
|
||||
data_str = json.dumps(data_dict)
|
||||
# 将字符串转换为bytes
|
||||
data_bytes = data_str.encode('utf-8')
|
||||
with NATSClient() as client:
|
||||
client.publish("alg.test", payload=data_bytes)
|
||||
return jsonify({'message': 'Message sent successfully!'})
|
||||
client.publish("alg.test", payload=json.dumps(standardLog).encode('utf-8'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
publish_to_nats()
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
flask
|
||||
pynats2
|
Loading…
Reference in New Issue
Block a user