Springboot Kafka Module Skill 标志

Springboot Kafka Module Skill

社区
jiushiwon
springboot-kafka-module-skill

Spring Kafka 模块快速集成技能。面向已拥有 Spring Boot 项目骨架的开发者,提供 Kafka 生产者、消费者、消息订阅、事件驱动等能力的快速集成。触发词:"Spring Kafka"、"Spring Boot Kafka"、"Kafka 集成"、"kafka producer"、"kafka consumer"、"kafka 消息"、"kafka 事件"、"kafka 队列"。

概览

发布者jiushiwon
代码仓库wg-skills
技能名称springboot-kafka-module-skill
星标数
123
复刻数
38
附带文件
仅含指令
许可协议Apache-2.0
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 jiushiwon 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Springboot Kafka Module Skill AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/jiushiwon/wg-skills/tree/main/vibeCoding/backend/java/springboot-module/springboot-kafka-module-skill
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/jiushiwon/wg-skills.git /tmp/wg-skills
mkdir -p .claude/skills
cp -r /tmp/wg-skills/vibeCoding/backend/java/springboot-module/springboot-kafka-module-skill .claude/skills/springboot-kafka-module-skill
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Springboot Kafka Module Skill,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Springboot Kafka Module Skill。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Springboot Kafka Module Skill 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Spring Kafka Module Skill

面向已有 Spring Boot 项目的开发者,快速集成 Kafka 能力。

能力清单

能力说明
生产者同步/异步发送消息、消息分区、消息key
消费者消费监听、消息重试、消费者组
消息序列化JSON/Avro/ProtoBuf
事务消息Kafka 事务
拦截器消息发送/消费拦截
** Streams**Kafka Streams 流处理

触发场景

用户说"帮我加 Kafka"或"集成 Kafka"时触发。

依赖配置

xml
<!-- pom.xml 添加 -->
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

默认方法封装

1. 生产者

java
@Service
public class KafkaProducerService {
    
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    
    /**
     * 发送消息(同步)
     */
    public SendResult<String, String> send(String topic, String key, String message) {
        ListenableFuture<SendResult<String, String>> future = 
            kafkaTemplate.send(topic, key, message);
        try {
            return future.get(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException("发送消息失败", e);
        }
    }
    
    /**
     * 发送消息(异步)
     */
    public void sendAsync(String topic, String key, String message, SendCallback callback) {
        kafkaTemplate.send(topic, key, message).addCallback(callback);
    }
    
    /**
     * 发送 JSON 消息
     */
    public void sendJson(String topic, String key, Object object) {
        String json = JSON.toJSONString(object);
        kafkaTemplate.send(topic, key, json);
    }
    
    /**
     * 发送带回调的消息
     */
    public void sendWithCallback(String topic, String key, String message) {
        kafkaTemplate.send(topic, key, message).addCallback(
            success -> {
                // 成功
                String topic = success.getRecordMetadata().topic();
                int partition = success.getRecordMetadata().partition();
                long offset = success.getRecordMetadata().offset();
                log.info("消息发送成功: topic={}, partition={}, offset={}", topic, partition, offset);
            },
            failure -> {
                // 失败
                log.error("消息发送失败: {}", failure.getMessage());
            }
        );
    }
}

2. 消费者

java
@Service
public class KafkaConsumerService {
    
    /**
     * 简单消费
     */
    @KafkaListener(topics = "my-topic", groupId = "my-group")
    public void consume(ConsumerRecord<String, String> record) {
        log.info("收到消息: key={}, value={}, partition={}, offset={}", 
            record.key(), record.value(), record.partition(), record.offset());
        
        // 业务处理
        String message = record.value();
        // ...
    }
    
    /**
     * 消费 JSON 消息
     */
    @KafkaListener(topics = "user-events", groupId = "user-group", containerFactory = "jsonKafkaListenerContainerFactory")
    public void consumeJson(UserEvent event) {
        log.info("收到用户事件: {}", event);
        // 业务处理
    }
    
    /**
     * 批量消费
     */
    @KafkaListener(topics = "batch-topic", groupId = "batch-group", containerFactory = "batchKafkaListenerContainerFactory")
    public void consumeBatch(List<ConsumerRecord<String, String>> records) {
        log.info("批量收到 {} 条消息", records.size());
        for (ConsumerRecord<String, String> record : records) {
            // 处理每条消息
        }
    }
    
    /**
     * 带错误处理的消费
     */
    @KafkaListener(topics = "error-topic", groupId = "error-group", errorHandler = "kafkaErrorHandler")
    public void consumeWithError(ConsumerRecord<String, String> record) {
        // 业务处理
    }
}

3. 事务消息

java
@Service
public class KafkaTransactionService {
    
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    
    /**
     * 事务操作:在事务内发送多条消息,要么全部成功,要么全部回滚
     */
    @Transactional
    public void sendInTransaction(String topic1, String msg1, String topic2, String msg2) {
        kafkaTemplate.executeInTransaction(operations -> {
            operations.send(topic1, "key1", msg1);
            operations.send(topic2, "key2", msg2);
            return true;
        });
    }
}

4. 配置类

java
@Configuration
public class KafkaConfig {
    
    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;
    
    /**
     * 生产者配置
     */
    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        // acks 配置
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        // 重试次数
        props.put(ProducerConfig.RETRIES_CONFIG, 3);
        // 批量大小
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        // 缓冲内存
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
        return new DefaultKafkaProducerFactory<>(props);
    }
    
    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
    
    /**
     * 消费者配置
     */
    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        // 自动提交
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        // 最早/最新偏移量
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        return new DefaultKafkaConsumerFactory<>(props);
    }
    
    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = 
            new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        // 并发消费
        factory.setConcurrency(3);
        // 批量消费
        factory.setBatchListener(true);
        return factory;
    }
    
    /**
     * JSON 序列化/反序列化
     */
    @Bean
    public JsonSerializer<Object> jsonSerializer() {
        return new JsonSerializer<>();
    }
    
    @Bean
    public JsonDeserializer<Object> jsonDeserializer() {
        JsonDeserializer<Object> deserializer = new JsonDeserializer<>();
        deserializer.addTrustedPackages("*");
        return deserializer;
    }
}

5. 错误处理

java
@Component
public class KafkaErrorHandler implements ErrorHandler {
    
    @Override
    public void handle(Exception e, ConsumerRecord<String, String> record) {
        log.error("消费消息失败: topic={}, partition={}, offset={}, key={}, value={}, error={}",
            record.topic(), record.partition(), record.offset(), 
            record.key(), record.value(), e.getMessage());
        
        // 可选:发送到死信队列
        // 可以记录到数据库或发送到告警
    }
    
    @Override
    public void handle(List<ConsumerRecord<String, String>> records, Exception e) {
        log.error("批量消费消息失败,数量: {}", records.size(), e);
    }
}

配置模板

application.yml

yaml
spring:
  kafka:
    bootstrap-servers: ${KAFKA_HOST:localhost}:${KAFKA_PORT:9092}
    producer:
      # 生产者配置
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
      acks: all
      retries: 3
      batch-size: 16384
      buffer-memory: 33554432
      properties:
        linger.ms: 10
        compression.type: snappy
    consumer:
      # 消费者配置
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      group-id: my-group
      auto-offset-reset: earliest
      enable-auto-commit: false
      properties:
        spring.json.trusted.packages: "*"
    listener:
      # 监听器配置
      ack-mode: manual_immediate
      concurrency: 3

不做

  • 不负责安装 Kafka(用户自行安装或使用 Docker)
  • 不处理 Kafka 集群配置(单节点为主)
  • 不提供消息持久化策略
  • 不处理 Kafka Connect 数据同步

常见问题

Springboot Kafka Module Skill AI 技能有什么作用?

Spring Kafka 模块快速集成技能。面向已拥有 Spring Boot 项目骨架的开发者,提供 Kafka 生产者、消费者、消息订阅、事件驱动等能力的快速集成。触发词:"Spring Kafka"、"Spring Boot Kafka"、"Kafka 集成"、"kafka producer"、"kafka consumer"、"kafka 消息"、"kafka 事件"、"kafka 队列"。

为什么在 TypingMind 中使用 Springboot Kafka Module Skill?

因为您只需安装一次,就能搭配任何模型使用。Springboot Kafka Module Skill 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Springboot Kafka Module Skill?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/jiushiwon/wg-skills/tree/main/vibeCoding/backend/java/springboot-module/springboot-kafka-module-skill。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Springboot Kafka Module Skill?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Springboot Kafka Module Skill 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Springboot Kafka Module Skill 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Springboot Kafka Module Skill AI 技能免费吗?

是的。jiushiwon 已在 GitHub 上根据 Apache-2.0 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇