book-python/main.py

40 lines
1.1 KiB
Python
Raw Normal View History

2024-12-20 16:24:19 +08:00
from recommendation_service.kafka.book_event_consumer import BookEventConsumer
import signal
import sys
import logging
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def signal_handler(sig, frame):
logger.info('接收到终止信号,正在关闭消费者...')
consumer.close()
sys.exit(0)
if __name__ == "__main__":
logger.info("启动图书推荐服务...")
# 注册信号处理器
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# 创建并启动消费者
logger.info("初始化Kafka消费者...")
consumer = BookEventConsumer(
bootstrap_servers=['localhost:9092']
)
try:
logger.info("开始监听图书事件...")
consumer.start_consuming()
except KeyboardInterrupt:
logger.info('接收到中断信号,正在关闭...')
consumer.close()
except Exception as e:
logger.error(f"服务运行出错: {str(e)}")
consumer.close()
sys.exit(1)