SpringBoot整合kafka

在SpringBoot官方对kafka的依赖没有使用starter,而是spring-kafka,如下:

image-20220607232634660

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>kafka</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- kafka -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <!-- spring boot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot 单元测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

application.yml内容

spring:
  kafka:
    bootstrap-servers: 192.168.126.156:9092
    consumer:
      group-id: kafka-demo
      # key/value的反序列化
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    producer:
      # key/value的序列化
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

启动类

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

/**
 * Kafka 整合
 * @author terry
 * @version 1.0
 * @date 2022/6/7 23:36
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

测试类

import com.terry.App;
import lombok.extern.java.Log;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;

@SpringBootTest(classes = App.class)
@Log
public class TestKafka {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;


    @KafkaListener(topics = "terry-topic")
    public void receive(ConsumerRecord record) {
        String message = (String) record.value();
        log.info("收到消息: " + message);
    }

    @Test
    public void test() throws InterruptedException {
        // 模拟一秒钟发送一次消息
        while (true) {
            kafkaTemplate.send("terry-topic", "hello kafka!");
            Thread.sleep(1000);
        }
    }
}

打印如下:

2022-06-08 00:27:08.267  INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka                 : 收到消息: hello kafka!
2022-06-08 00:27:09.277  INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka                 : 收到消息: hello kafka!
2022-06-08 00:27:10.289  INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka                 : 收到消息: hello kafka!
2022-06-08 00:27:11.288  INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka                 : 收到消息: hello kafka!
2022-06-08 00:27:12.302  INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka                 : 收到消息: hello kafka!
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

terrybg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值