05_kafka-整合springboot

文章目录


  • kafka 整合 springboot

  • pom.xml

xml 复制代码
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
</parent>
<dependencies>

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

       <dependency>
           <groupId>org.springframework.kafka</groupId>
           <artifactId>spring-kafka</artifactId>
       </dependency>
       <!--测试-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>

</dependencies>
  • application.yml
yaml 复制代码
spring:
  kafka:
    bootstrap-servers: kafka_1:9092,kafka_2:9092,kafka_3:9092
    consumer:
      auto-commit-interval: 100
      auto-offset-reset: earliest
      enable-auto-commit: true
      group-id: group1
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      properties:
        isolation:
          level: read_committed
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    producer:
      acks: all
      batch-size: 16384
      buffer-memory: 33554432
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      properties:
        enable:
          idempotence: true
      retries: 5
      transaction-id-prefix: transaction-id-
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
  • KafkaBootApp
java 复制代码
package cn.qww.boot;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.KafkaListeners;
import org.springframework.messaging.handler.annotation.SendTo;

import java.io.IOException;


@SpringBootApplication

@EnableKafka
public class KafkaBootApp {
    public static void main(String[] args) throws IOException {
        SpringApplication.run(KafkaBootApp.class, args);
    }


    @KafkaListeners(value = {@KafkaListener(topics = {"topic04"})})
    @SendTo(value = {"topic02"})
    public String listener(ConsumerRecord<?, ?> cr) {
        System.out.println("receive:" + cr);
        return cr.value() + " qww";
    }


}
  • kafkaTemplate 使用,及 事务
java 复制代码
package cn.qww.boot;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaOperations;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest(classes = {KafkaBootApp.class})
@RunWith(SpringRunner.class)
public class KafkaTemplateTests {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 两种方式,注掉 application.yml 中   transaction-id-prefix: transaction-id-
     * 或者添加 @Transactional 注释
     */
    @Test
    // @Transactional
    public void sendTest() {
        kafkaTemplate.send(new ProducerRecord<>("topic01", "kafkaTemplate_key", "kafkaTemplate_value"));
    }

    /**
     * 注掉 application.yml 中 需要开启:  transaction-id-prefix: transaction-id- 配置;
     * 就是说和上边的方法相互冲突
     */
    @Test
    public void testKafkaTemplateTx() {
        kafkaTemplate.executeInTransaction(new KafkaOperations.OperationsCallback<String, String, Object>() {
            @Override
            public Object doInOperations(KafkaOperations<String, String> kafkaOperations) {
                return kafkaOperations.send(new ProducerRecord<>("topic01", "OperationsCallback_key", "OperationsCallback_OperationsCallback"));
            }
        });
    }

}
相关推荐
indexsunny8 小时前
互联网大厂Java求职面试实战:基于电商场景的技术问答及解析
java·spring boot·redis·kafka·security·microservices·面试指导
马克Markorg8 小时前
SpringBoot + LangChain4j 打造企业级 RAG 智能知识库,多工具集成方案
spring boot·向量数据库·rag·qdrant·langchain4j·增强知识检索库
百锦再9 小时前
Java中的日期时间API详解:从Date、Calendar到现代时间体系
java·开发语言·spring boot·struts·spring cloud·junit·kafka
树码小子10 小时前
图书管理系统(2)图书列表接口
spring boot·mybatis·图书管理系统
Coder_Boy_11 小时前
Java高级_资深_架构岗 核心知识点——高并发模块(底层+实践+最佳实践)
java·开发语言·人工智能·spring boot·分布式·微服务·架构
健康平安的活着11 小时前
AI之Toolcalling的使用案例(langchain4j+springboot)
人工智能·spring boot·后端
百锦再12 小时前
Java IO详解:File、FileInputStream与FileOutputStream
java·开发语言·jvm·spring boot·spring cloud·kafka·maven
百锦再13 小时前
Java InputStream和OutputStream实现类完全指南
java·开发语言·spring boot·python·struts·spring cloud·kafka
予枫的编程笔记13 小时前
【Kafka基础篇】RabbitMQ、RocketMQ、Kafka怎么选?3种主流MQ核心差异实测解析
kafka·rabbitmq·rocketmq·分布式流处理·发布订阅模型·消息队列(mq)·点对点模型
树码小子13 小时前
图书管理系统(3)修改图书接口
spring boot·mybatis·图书管理系统