SpringBoot整合kafka

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

SpringBoot整合kafka_第1张图片

pom.xml

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

    <artifactId>kafkaartifactId>

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

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.kafkagroupId>
            <artifactId>spring-kafkaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        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!

你可能感兴趣的:(kafka,SpringBoot,kafka,spring,boot,java)