Spring Boot应用程序集成Apache Kafka

以下是一个Spring Boot应用程序集成Apache Kafka的示例,展示了如何发送和消费消息。我们将使用Spring Kafka模块来实现这个示例。

创建Spring Boot项目

首先,确保你已经创建了一个Spring Boot项目,并在项目的 pom.xml 文件中添加了以下依赖:

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.12.RELEASE</version>
        </dependency>

        <!-- Spring Boot Starter Kafka -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.3.12.RELEASE</version>
        </dependency>
    </dependencies>

配置Kafka

接下来,在项目的 `application.yml 文件中添加Kafka的配置:

spring:
  kafka:
    bootstrap-servers: 127.0.0.1:9092
    consumer:
      enable-auto-commit: true
      auto-offset-reset: latest
      max-poll-records: 1 # 每次只消费一条消息
      #group-id: pbt-scheduler
      auto-commit-interval: 1000
      heartbeat-interval: 3000
      session:
        timeout:
          ms: 20000
      # 指定consumer两次poll的最大时间间隔(默认5分钟) ,超过这个时间,则消费者将重新拉取该消息进行处理
      properties:
        max:
          poll:
            interval:
              ms: 300000
    producer:
      acks: all
      retries: 0
      batch-size: 4096
      buffer-memory: 40960
      linger:
        ms: 1

编写生产者代码

创建一个Kafka生产者类,用于发送消息:

package com.example.kafkademo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class KafkaProducer {

    private static final String TOPIC = "my_topic";

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String message) {
        kafkaTemplate.send(TOPIC, message);
    }
}

编写消费者代码

创建一个Kafka消费者类,用于消费消息:

package com.example.kafkademo;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaConsumer {

    @KafkaListener(topics = "my_topic", groupId = "my-group")
    public void consume(String message) {
        System.out.println("Received message: " + message);
    }
}

创建控制器

创建一个控制器来触发消息的发送:

package com.example.kafkademo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KafkaController {

    @Autowired
    private KafkaProducer kafkaProducer;

    @GetMapping("/send")
    public String sendMessage(@RequestParam("message") String message) {
        kafkaProducer.sendMessage(message);
        return "Message sent to Kafka Topic!";
    }
}

Spring Boot 主类

创建Spring Boot应用程序的主类:

package com.example.kafkademo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KafkaDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(KafkaDemoApplication.class, args);
    }
}

运行应用程序

启动Spring Boot应用程序,确保Kafka服务器正在运行。然后,使用浏览器或工具(如Postman)访问以下URL以发送消息:

http://localhost:8080/send?message=HelloKafka

验证消费

你应该能够在控制台上看到消费者接收到的消息输出:

Received message: HelloKafka

解释

  1. 依赖管理

    • 使用Spring Boot Starter Web和Spring Boot Starter Kafka来简化Kafka集成。
  2. 配置

    • application.yml 中指定Kafka服务器地址和消费者组ID。
  3. 生产者服务

    • 使用 KafkaTemplate 来发送消息到指定的主题。
  4. 消费者服务

    • 使用 @KafkaListener 注解来监听指定主题的消息并处理。
  5. 控制器

    • 创建一个简单的REST控制器来触发消息发送操作。

通过这个示例,你可以轻松地使用Spring Boot集成Kafka来实现消息的发送和消费。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值