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