概览Overview

一个独立的 MoonBit 命令行演示项目:用开源库 daqing/moonkafka 连接 Apache Kafka,发送消息并读取消息。

A standalone MoonBit command-line demo that uses the open-source daqing/moonkafka library to connect to Apache Kafka, produce messages and consume them.

  • MoonBit
  • native target
  • Apache Kafka 4.x KRaft
  • daqing/moonkafka 0.3.4

这个项目做什么What it does

仓库提供一个可执行程序 cmd/main,带有 produce 与 consume 两个子命令。前者把一条消息写入指定主题,并打印 broker 返回的偏移量;后者持续从主题拉取记录,把每条记录的 offset、timestamp、key 与 value 逐条打印出来。

The repository ships one executable, cmd/main, with two subcommands. produce writes a single message to a topic and prints the offset the broker assigned. consume keeps polling a topic and prints each record's offset, timestamp, key and value.

职责边界Where the boundary is

所有 Kafka 行为——broker 连接、元数据处理、Kafka 协议、消息发送与拉取——都由 moonkafka 的 Producer 与 Consumer 完成。本项目只保留 命令行参数解析、UTF-8 文本转换和结果展示,没有实现任何自定义 Kafka 客户端逻辑。

Every Kafka behaviour — broker connection, metadata handling, the Kafka protocol, producing and fetching — comes from moonkafka's Producer and Consumer. What is left here is argument parsing, UTF-8 conversion and result rendering: no custom Kafka client logic is implemented.

API 基线API baseline

项目通过 MoonBit 包管理器引用 daqing/moonkafka@0.3.4。上游称该版本为 纯文档发布——没有任何库代码改动——因此 API 基线仍是 v0.3.3 标签提交 6e0f45f18ae9e5d9b16e30ddf9ac0533a7cdd602。0.3.4 只发布在 mooncakes.io 上,没有自己的 tag。

The project pulls in daqing/moonkafka@0.3.4 through the MoonBit package manager. Upstream calls that release documentation-only — no library code changed — so the API baseline is still the v0.3.3 tag, commit 6e0f45f18ae9e5d9b16e30ddf9ac0533a7cdd602. 0.3.4 lives on mooncakes.io only; it carries no tag of its own.

功能Features

项目结构Project layout

moonkafka-demo/
├── cmd/main/                 CLI: produce / consume
│   ├── main.mbt              entry point, subcommand dispatch
│   ├── cli.mbt               argument parsing and help text
│   ├── producer_demo.mbt     produce subcommand
│   ├── consumer_demo.mbt     consume subcommand
│   └── cli_wbtest.mbt        unit tests for argument parsing
├── tests/itest/              end-to-end integration test
│   ├── main.mbt              phases and teardown
│   ├── harness.mbt           Step / Runner, command execution, assertions
│   ├── kafka.mbt             runtime detection, Kafka lifecycle, topics
│   ├── options.mbt           integration-test options
│   ├── cli.mbt               builds CLI commands, parses their output
│   └── harness_wbtest.mbt    unit tests for the harness helpers
├── docs/                     this site
├── docker-compose.kafka.yml  single-node Kafka 4.3 KRaft
├── Makefile                  day-to-day commands and itest targets
└── moon.mod                  module definition and dependencies
路径职责
cmd/main/main.mbt入口:读取参数并分派到 produce / consume / 帮助
cmd/main/cli.mbt参数解析:端口校验、起始位置、--max-messages、帮助文本
cmd/main/producer_demo.mbtproduce:连接、发送并打印偏移量
cmd/main/consumer_demo.mbtconsume:连接、轮询并逐条打印记录
cmd/main/cli_wbtest.mbt参数解析的单元测试
tests/itest/main.mbt集成测试主流程:两个阶段与收尾
tests/itest/harness.mbtStep 与 Runner:执行外部命令、打印日志、记录断言
tests/itest/kafka.mbt容器运行时探测、Kafka 启停、建主题与就绪等待
tests/itest/options.mbt集成测试的命令行选项
tests/itest/cli.mbt拼装要执行的 CLI 命令,并解析其输出
tests/itest/harness_wbtest.mbtharness 辅助函数的单元测试
docker-compose.kafka.yml单节点 Kafka 4.3 KRaft broker
Makefile日常命令与 itest 目标
moon.mod模块定义与依赖
PathResponsibility
cmd/main/main.mbtEntry point: dispatch to produce / consume / help
cmd/main/cli.mbtArgument parsing: port validation, start position, --max-messages, help text
cmd/main/producer_demo.mbtproduce: connect, send, print the offset
cmd/main/consumer_demo.mbtconsume: connect, poll, print each record
cmd/main/cli_wbtest.mbtUnit tests for argument parsing
tests/itest/main.mbtIntegration-test flow: both phases and teardown
tests/itest/harness.mbtStep and Runner: run external commands, log, record assertions
tests/itest/kafka.mbtContainer-runtime detection, Kafka lifecycle, topic creation and readiness
tests/itest/options.mbtIntegration-test command-line options
tests/itest/cli.mbtBuilds the CLI commands to run and parses their output
tests/itest/harness_wbtest.mbtUnit tests for the harness helpers
docker-compose.kafka.ymlSingle-node Kafka 4.3 KRaft broker
MakefileDay-to-day commands and itest targets
moon.modModule definition and dependencies
只在 native 目标上构建 moonkafka 的 socket 实现只支持 MoonBit native 目标,两个包的 moon.pkg 都声明了 supported_targets = "+native"。 下文所有命令都显式带上 --target native。
Native target only moonkafka's socket layer supports the MoonBit native target only, and both packages declare supported_targets = "+native". Every command below passes --target native explicitly.

下一步Next steps