{"id":21018739,"url":"https://github.com/tal-tech/kafka-proxy","last_synced_at":"2025-05-15T06:31:46.582Z","repository":{"id":109364079,"uuid":"307105955","full_name":"tal-tech/kafka-proxy","owner":"tal-tech","description":null,"archived":false,"fork":false,"pushed_at":"2020-10-26T02:26:59.000Z","size":47,"stargazers_count":9,"open_issues_count":0,"forks_count":4,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-03T05:12:06.648Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tal-tech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-25T13:33:04.000Z","updated_at":"2023-07-31T13:31:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"b67f9d63-9b05-4f66-aba8-6e1c2f3fed1d","html_url":"https://github.com/tal-tech/kafka-proxy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2Fkafka-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2Fkafka-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2Fkafka-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2Fkafka-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tal-tech","download_url":"https://codeload.github.com/tal-tech/kafka-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254288262,"owners_count":22045868,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T10:27:28.956Z","updated_at":"2025-05-15T06:31:46.576Z","avatar_url":"https://github.com/tal-tech.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\n\n# 概述：\n\nmqproxy是基于 java客户端实现的一款kafka代理工具，它实现了生产、消费、延时队列等功能。目前我们只开源了生产者代理和延时队列代理，消费者代理我们会陆续进行开源。如果您正在正在被kafka不支持延时队列而困扰，mqproxy可能会为你提供一种新的思路。\n\n# 一、设计思路：\n\n## 1、生产者代理\n\n​\t设计思路：我们将生产的消息通过[mapdb](http://www.mapdb.org/)进行临时存储来保证消息传输的可靠性，同时利用[mapdb](http://www.mapdb.org/)的高吞吐量来保证代理的生产效率。\n\n## 2、延时队列代理\n\n​\t设计思路：设计上，我们借鉴了多层时间轮的概念，将时间轮的等级和刻度设计为kafka的topic，通过不同延时级别的topic来传递消息，这样的设计避免了因代理意外宕机导致的数据难以恢复的问题和任意时延需求下kafka位移提交的问题。\n\n举例：我们预先在kafka上创建4组topic（用户可以在代码中自定义组数），延时级别分别为\n\n1s，2s，3s ... 9s, \n\n10s,20s,30s ... 90s ,\n\n100s ,200s ,300s ...900s,\n\n1000s,2000s,3000s ...9000s。\n\n### \t  例1：当生产者代理收到一个延时级别为119s的延时请求：\n\n​\t\t1、**生产者代理**会把消息投入到延时级别为**100s的topic**中。\n\n​\t\t2、**延时队列代理**检测到100s 延时级别的请求，则将消息从队列拉取到**内存中**进行**100s倒计时**。\n\n​\t\t3、当100s倒计时结束后，发现还有19s的剩余时间，则会把消息继续向下投递到延时级别为**10s的topic**中，同时向延时级别为100s的topic**提交位移**。\n\n​\t\t4、**延时队列代理**检测到10s 延时级别的请求，则将消息从队列拉取到**内存中**进行**10s倒计时**。\n\n​\t\t5、当10s倒计时结束后，发现还有9s的剩余时间，则会把消息继续向下投递到延时级别为**9s的topic中**，同时向延时级别为10s的topic**提交位移**。\n\n​\t\t6、**延时队列代理**检测到9s 延时级别的请求，则将消息从队列拉取到内存中进行9s倒计时。\n\n​\t\t7、当9s倒计时结束后，发现延时时间已经被耗尽，则把消息投入到**原topic**，同时向延时级别为9s的topic**提交位移**，逐级传递的流程结束。\n\n###      例2：当生产者代理收到一个延时级别为20001s的延时请求：\n\n​\t\t1、消息会在延时级别为**9000s**的延时队列中重复投递2次，直至剩余2001s，\n\n​\t\t2、重复“例1”中的逐级传递流程，直到投入到原topic为止\n\n\n\n# 二、安装运行：\n\n## 1、下载并安装jdk1.8\n\n以cenos安装openjdk1.8为例\n\n```\nyum install -y java-1.8.0-openjdk\n```\n\n```\njava -version\n```\n\n## 2、安装maven环境（略），编译打包：\n\n```\nmvn clean package -Dmaven.test.skip=true\n```\n\n## 3、运行jar包\n\n生产者代理jar：kafka-producer-service-1.0.0，这里给出了一个启动命令，读者可以根据自身情况进行参数设定\n\n```\nnohup java -jar kafka-producer-service-1.0.0 --kafka.producer.server=\"127.0.0.1:9092\" \u003e/dev/null 2\u003e\u00261 \u0026\n```\n\n如果有延时消息请求，延时队列代理：kafka-delay-anytime-service-0.0.1-SNAPSHOT\n\n```\nnohup java -jar kafka-delay-anytime-service-1.0.0 --kafka.bootstrap-servers=\"127.0.0.1:9092\" \u003e/dev/null 2\u003e\u00261 \u0026\n```\n\n\n\n# 三、使用\n\n1、发送普通消息\n\n```\ncurl  --request POST '127.0.0.1:8080/v1/kafka/send' --header 'Content-Type: application/json' --data '{\"headers\": {\"content\": \"goods\",\"instant\": \"yes\"},\"topic\": \"topic-open\",\"payload\": \"hello proxy\"}'\n```\n\n2、发送延时时间为19秒的延时消息\n\n```\ncurl  --request POST '127.0.0.1:8080/v1/kafka/send' --header 'Content-Type: application/json' --data '{\"headers\": {\"content\": \"goods\",\"instant\": \"yes\",\"x-delay\": \"11\"},\"topic\": \"topic-open\",\"payload\": \"hello delay\"}'\n```\n\n**ps：生产者代理可以独立安装部署，延时队列代理需要借助生产者代理进行消息发送。**\n\n\n\n# Contact us\n\nissue：https://github.com/tal-tech/kafka-proxy/issues\n\n加群请加微信：\n\n![](http://static0.xesimg.com/tal-tech-pic/fend/assets/contactus.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftal-tech%2Fkafka-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftal-tech%2Fkafka-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftal-tech%2Fkafka-proxy/lists"}