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') msg = ns.get('msg')
if msg.error(): # if msg.error():
# 写入日志文件记录错误信息 # # 写入日志文件记录错误信息
with open('baidu_kafka_error.txt', 'a', encoding='utf-8') as f: # 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") # f.write(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - 消费者错误: {msg.error()}\n")
else: # else:
try: 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] msg_data = msg_data_ori['messages'][0]
messageid = msg_data.get('id') messageid = msg_data.get('id')
taskid = msg_data.get('taskId') taskid = msg_data.get('taskId')

View File

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