V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
piaochen0
V2EX  ›  Python

采用 Pull 来主动定时获取 RocketMQ 的消息,每次都会收到已经处理过的信息?

  •  
  •   piaochen0 · 2021-10-19 17:17:18 +08:00 · 2221 次点击
    这是一个创建于 891 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目需要 Pull 的方式,定时主动获取 RocketMQ 中的消息,而不是采用订阅 Push 的方式。
    查了下,目前 python 连接 rocketmq 主要有两个库:rocketmq-client-python 和 rociketmq
    rocketmq-client-python 是用来替代 rocketmq 的
    发现最新的 rocketmq-client-python 库只有 PushConsumer,PullConsumer 已经去除了。
    于是就找了老的 rocketmq 的库,里面还有 PullConsumer

    不过按官方案例调试了下,发现每次运行如下代码,会收到大量已经获取过的消息。
    代码如下:
    from rocketmq.client import PullConsumer
    consumer = PullConsumer('CID_test')
    consumer.set_namesrv_addr('127.0.0.1:9876')
    consumer.start()

    for msg in consumer.pull('TEST-TOPIC'):
    print(msg.id, msg.reconsume_times, msg.queue_offset, msg.body)
    consumer.shutdown()

    网上搜了下,也看了源代码,应该 pull 的方式,客户端需要手动处理 offset,对应有一个 offset_table 变量
    我能否这样处理:
    1.每次收到一个消息后,我都把 offset_table 存储到数据库里。
    2.下次重启程序的时候,我要从数据库里把 offset 信息取出来,赋给 offset_table 变量

    目前我简单做了测试,没有发现问题。不知道我的理解处理有没有问题。 这块网上信息好少,特别是 python 处理的。麻烦有经验的小伙伴赐教。

    2 条回复    2021-10-19 19:47:18 +08:00
    NaVient
        1
    NaVient  
       2021-10-19 19:38:20 +08:00
    commit 了吗?
    janxin
        2
    janxin  
       2021-10-19 19:47:18 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   958 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:45 · PVG 05:45 · LAX 14:45 · JFK 17:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.