From fe13c5840b0f56222e172afa1c83c8dc884c5bc0 Mon Sep 17 00:00:00 2001 From: mozzie Date: Thu, 14 Sep 2023 13:59:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20python=E5=8F=91=E5=B8=83=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=88=B0nats-server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/services/alg/app.py | 26 ++++++++++++-------------- apps/services/alg/requirements.txt | 1 - 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/apps/services/alg/app.py b/apps/services/alg/app.py index ddc6db9..8f4792d 100644 --- a/apps/services/alg/app.py +++ b/apps/services/alg/app.py @@ -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() diff --git a/apps/services/alg/requirements.txt b/apps/services/alg/requirements.txt index 1f0559d..512aaf3 100644 --- a/apps/services/alg/requirements.txt +++ b/apps/services/alg/requirements.txt @@ -1,2 +1 @@ -flask pynats2 \ No newline at end of file