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"));
            }
        });
    }

}
相关推荐
派大鑫wink5 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
xUxIAOrUIII6 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home6 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
JIngJaneIL6 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
全靠bug跑7 小时前
Spring Cloud OpenFeign 实战三部曲:快速集成 · 连接池优化 · 客户端抽取
java·spring boot·openfeign
北城以北88888 小时前
Spring定时任务与Spring MVC拦截器
spring boot·spring·mvc
缘不易8 小时前
Springboot 整合JustAuth实现gitee授权登录
spring boot·后端·gitee
Mr.朱鹏9 小时前
SQL深度分页问题案例实战
java·数据库·spring boot·sql·spring·spring cloud·kafka
白宇横流学长9 小时前
基于SpringBoot实现的冬奥会科普平台设计与实现【源码+文档】
java·spring boot·后端
Hello.Reader11 小时前
Flink SQL CREATE 语句从建表到 CTAS/RTAS,一次讲清
sql·flink·linq