• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot集成Kafka的步骤

springboot 搞代码 4年前 (2022-01-09) 36次浏览 已收录 0个评论

SpringBoot集成Kafka

本篇主要讲解SpringBoot 如何集成Kafka ,并且简单的 编写了一个Demo 来测试 发送和消费功能

前言

选择的版本如下:

springboot : 2.3.4.RELEASE

spring-kafka : 2.5.6.RELEASE

kafka : 2.5.1

zookeeper : 3.4.14

本Demo 使用的是 SpringBoot 比较高的版本 SpringBoot 2.3.4.RELEASE 它会引入 spring-kafka 2.5.6 RELEASE ,对应了版本关系中的
Spring Boot 2.3 users should use 2.5.x (Boot dependency management will use the correct version).

spring和 kafka 的版本 关系

https://spring.io/projects/sp…

1.搭建Kafka 和 Zookeeper 环境

搭建kafka 和 zookeeper 环境 并且启动 它们

2.创建Demo 项目引入spring-kafka

2.1 pom 文件

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
</dependency>

2.2 配置application.yml

spring:
 kafka:
  bootstrap-servers: 192.168.25.6:9092 #bootstrap-servers:连接kafka的地址,多个地址用逗号分隔
  consumer:
   group-id: myGroup
   enable-auto-commit: true
   auto-commit-interval: 100ms
   properties:
    session.timeout.ms: 15000
   key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
   value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
   auto-offset-reset: earliest
  producer:
   retries: 0 #若设置大于0的值,客户端会将发送失败的记录重新发送
   batch-size: 16384 #当将多个记录被发送到同一个分区时, Producer 将尝试将记录组合到更少的请求中。这有助于提升客户端和服务器端的性能。这个配置控制一个批次的默认大小(以字节为单位)。16384是缺省的配置
   buffer-memory: 33554432 #Producer 用来缓冲等待被发送到服务器的记录的总字节数,33554432是缺省配置
   key-se<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i>rializer: org.apache.kafka.common.serialization.StringSerializer #关键字的序列化类
   value-serializer: org.apache.kafka.common.serialization.StringSerializer #值的序列化类

2.3 定义消息体Message

/**
 * @author johnny
 * @create 2020-09-23 上午9:21
 **/
@Data
public class Message {


  private Long id;

  private String msg;

  private Date sendTime;
}

2.4 定义KafkaSender

主要利用 KafkaTemplate 来发送消息 ,将消息封装成Message 并且进行 转化成Json串 发送到Kafka中

@Component
@Slf4j
public class KafkaSender {

  private final KafkaTemplate<String, String> kafkaTemplate;

  //构造器方式注入 kafkaTemplate
  public KafkaSender(KafkaTemplate<String, String> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
  }

  private Gson gson = new GsonBuilder().create();

  public void send(String msg) {
    Message message = new Message();

    message.setId(System.currentTimeMillis());
    message.setMsg(msg);
    message.setSendTime(new Date());
    log.info("【++++++++++++++++++ message :{}】", gson.toJson(message));
    //对 topic = hello2 的发送消息
    kafkaTemplate.send("hello2",gson.toJson(message));
  }

}

2.5 定义KafkaConsumer


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot集成Kafka的步骤

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址