adexos/m2-kafka-connector 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adexos/m2-kafka-connector

Composer 安装命令:

composer require adexos/m2-kafka-connector

包简介

Kafka module with Avro support

README 文档

README

This module is a bridge of koco/messenger-kafka for Magento 2.

It is built to use Magento 2 native queue system with some adjustments to the settings.

It also handles reading with Avro Schemes.

Disclaimer

Only the reading part is done from now. You can't write to a queue.

Installation

composer require adexos/m2-kafka-connector

Usage

Declare your configuration

To do so, you can simply include in your app/code/Namespace/Module/etc/adminhtml/system.xml the kafka configuration form :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="kafka" translate="label" type="text" sortOrder="300" showInDefault="1">
            <group id="warehouse" translate="" type="text" sortOrder="20" showInDefault="1">
                <label>Warehouse</label>
                <include path="Adexos_KafkaConnector::includes/kafka_conf_included.xml"/>
            </group>
        </section>
    </system>
</config>

Please note the group id you set, it will be used to connect the queue runner to the Kafka broker

You can find the configuration here : Stores -> Configuration -> Services -> Kafka

Add the queue system

Communication

app/code/Namespace/Module/etc/communication.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
    <topic name="warehouse.update.stock" request="Namespace\Module\Model\StockMessage"/>
</config>

The request field will be used as the model of the message you are receiving.

This class must be full typed with PhpDoc because of Magento 2 requirements

Please note that the topic name DO NOT HAVE to be the same as the Kafka queue you are looking for. Since most of Kafka queues are in the same broker but have different names depending on the environment, we cannot define them in .xml as we do for other connection types like db queues

Instead, the real Kafka topic name must be defined in the system you have set earlier.**

Queue consumer

app/code/Namespace/Module/etc/queue_consumer.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
    <consumer name="warehouse"
              queue="warehouse.stock.update"
              connection="kafka.warehouse"
              handler="Namespace\Module\Handler\StockMessageHandler::handle"
              onlySpawnWhenMessageAvailable="0"
    />
</config>
  • queue : as per Magento doc, it must be identical to the topic name defined in the communication.xml file
  • connection : please note that the Kafka connection must starts with kafka., for example : kafka.warehouse. This is done to detect all kafka connection types and to retrieve them in the configuration defined.

If connection is kafka.warehouse, the group id defined in the system.xml file but be warehouse. This allow us to map through the core_config_data table automatically

  • handler : The handler that will take your message and process it. The parameter type must be as same type as the one defined in communication.xml inside the request field
  • onlySpawnWhenMessageAvailable : this flag must be set to zero. Since natively Magento only spawns a consumer when there is a message available, the Kafka consumer will be spawned and despawned endlessly. A Kafka consumer only commits its offset when a message is read. However, if no message is read and no offset is committed yet, the next time the consumer will spawn, it'll ready from the very end of the queue. Adding this flag ensure that a message will be read at least once and the offset will be commited. In fact, you can remove it after the offset is commit (manually or automatically). It is not advised to do so.

High performance queue

You might need to have a better performance for your queue. To do so, you can add leverage the power of headers from Kafka message if your messages has some. If you can filter only needed messages by reading headers, you can just skip the Avro deserialization and avoid a lot of processing time.

Here is how you can do it :

app/code/Namespace/Module/etc/di.xml

<type name="Adexos\KafkaConnector\Serializer\AvroSchemaRegistrySerializer">
    <arguments>
        <argument name="headerFilters" xsi:type="array">
            <item name="your.magento.topic.name" xsi:type="object">
                Namespace\Module\Serializer\HeaderFilter\YourCustomAvroHeaderFilter
            </item>
        </argument>
    </arguments>
</type>

your.magento.topic.name : must be replaced by the topic name you defined in communication.xml file. Namespace\Module\Serializer\HeaderFilter\YourCustomAvroHeaderFilter : must be replaced by your custom header filter

Your custom header filter must implement the Adexos\KafkaConnector\Serializer\HeaderFilter\AvroHeaderFilterInterface and the serializer will automatically pass the headers to your filter if it exists for your topic.

adexos/m2-kafka-connector 适用场景与选型建议

adexos/m2-kafka-connector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.06k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 adexos/m2-kafka-connector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adexos/m2-kafka-connector 我们能提供哪些服务?
定制开发 / 二次开发

基于 adexos/m2-kafka-connector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-18