This commit is contained in:
ping 2025-11-21 16:15:13 +08:00
parent 6045e8ec76
commit 9cbda277fb
2 changed files with 120 additions and 114 deletions

View File

@ -41,14 +41,15 @@ async def baidu_sms_kafka_consumer(ns={}):
msg = ns.get('msg')
if msg.error():
# 写入日志文件记录错误信息
with open('baidu_kafka_error.txt', 'a', encoding='utf-8') as f:
f.write(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - 消费者错误: {msg.error()}\n")
else:
# if msg.error():
# # 写入日志文件记录错误信息
# with open('baidu_kafka_error.txt', 'a', encoding='utf-8') as f:
# f.write(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - 消费者错误: {msg.error()}\n")
# else:
try:
# 解析消息内容为字典(避免变量名冲突)
msg_data_ori = json.loads(msg.value().decode('utf-8'))
# msg_data_ori = json.loads(msg.value().decode('utf-8'))
msg_data_ori = json.loads(msg)
msg_data = msg_data_ori['messages'][0]
messageid = msg_data.get('id')
taskid = msg_data.get('taskId')

View File

@ -100,7 +100,7 @@ async def baidu_sms_kafka_consumer(ns={}):
'security.protocol': 'SASL_SSL',
'ssl.endpoint.identification.algorithm': 'none',
# 证书文件路径
'ssl.ca.location': 'baidu_kafka_ca.pem',
'ssl.ca.location': 'D:/Code/kboss/kgadget/src/baidu_kafka_ca.pem',
# SASL 机制
'sasl.mechanism': 'SCRAM-SHA-512',
# SASL 用户名
@ -124,21 +124,23 @@ async def baidu_sms_kafka_consumer(ns={}):
pass
else:
try:
# print('Received message: {}'.format(msg.value().decode('utf-8')))
method = "POST"
url = 'https://www.opencomputing.cn/baiduc/baidu_sms_kafka_consumer.dspy'
header = {
"Host": "www.opencomputing.cn",
"Content-Type": "application/json"
# "Content-Type": "application/json"
}
data = {
'msg': msg
'msg': msg.value().decode('utf-8')
}
async with aiohttp_client.request(
method=method,
url=url,
headers=header,
json=data) as res:
data_ = await res.json()
data=data) as res:
data_ = await res.text()
# print("Response data:", data_)
except json.JSONDecodeError:
return {
@ -146,6 +148,9 @@ async def baidu_sms_kafka_consumer(ns={}):
'msg': '错误消息内容非有效JSON'
}
except Exception as e:
import traceback
print(str(e)+ traceback.format_exc())
traceback.print_exc()
return {
'status': False,
'msg': f"处理异常: {str(e)}"