feat: python发布消息到nats-server

This commit is contained in:
mozzie 2023-09-14 14:05:21 +08:00
parent fe13c5840b
commit 61a874ca6f

View File

@ -2,9 +2,14 @@ from pynats2 import NATSClient
from datetime import datetime,timedelta
import json
# nats-server配置信息
NATS_PATTERN = "alg.test"
NATS_SERVER = "nats://localhost:4222"
def publish_to_nats():
timestamp = (datetime.utcnow() + timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
standardLog = {
"timestamp": (datetime.utcnow()+timedelta(hours=8)).strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"timestamp": timestamp,
"level": 'INFO', # DEBUG、INFO、WARNING、ERROR、CRITICAL等级别
"name": 'alg.slice',# 日志生产的模块、服务
"message": "算法版本:$1,分割结果:$2",# 日志记录模板
@ -13,8 +18,8 @@ def publish_to_nats():
"$2": True
},
}
with NATSClient() as client:
client.publish("alg.test", payload=json.dumps(standardLog).encode('utf-8'))
with NATSClient(NATS_SERVER) as client:
client.publish(NATS_PATTERN, payload=json.dumps(standardLog).encode('utf-8'))
if __name__ == '__main__':
publish_to_nats()