Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of integrating kafka 2.2.0 with Springboot 2.x

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail the "Springboot 2.x integration of kafka 2.2.0 method", the content is detailed, the steps are clear, the details are handled properly, I hope this "Springboot 2.x integration kafka 2.2.0 method" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Introduction

Kafka has been updated very quickly in recent years, and you can see that kafka is used more and more frequently in the enterprise. It is relatively simple to integrate kafka in springboot, but you should pay attention to the version used and the basic configuration in kafka. This place requires confidence to prevent it from entering the pit.

Basic environment

Springboot version 2.1.4

Kafka version 2.2.0

Jdk 1.8

Code writing 1. Basic reference pom 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.example demo 0.0.1-SNAPSHOT kafkademo Demo project for Spring Boot 1.8 Org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime Org.springframework.boot spring-boot-starter-test test org.springframework.kafka spring-kafka 2.2.0.RELEASE Com.google.code.gson gson 2.7 org.springframework.boot Spring-boot-maven-plugin 2 、 Basic configuration spring.kafka.bootstrap-servers=2.1.1.1:9092spring.kafka.consumer.group-id=test-consumer-groupspring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializerspring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializerspring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializerspring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer # logging.level.root=debug3, entity class package com.example.demo.model Import java.util.Date; public class Messages {private Long id; private String msg; private Date sendTime; public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getMsg () {return msg;} public void setMsg (String msg) {this.msg = msg;} public Date getSendTime () {return sendTime } public void setSendTime (Date sendTime) {this.sendTime = sendTime;}} 4, producer side package com.example.demo.service; import com.example.demo.model.Messages;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.kafka.core.KafkaTemplate;import org.springframework.kafka.support.SendResult;import org.springframework.stereotype.Service;import org.springframework.util.concurrent.ListenableFuture Import java.util.Date;import java.util.UUID; @ Servicepublic class KafkaSender {@ Autowired private KafkaTemplate kafkaTemplate; private Gson gson = new GsonBuilder () .create (); public void send () {Messages message = new Messages (); message.setId (System.currentTimeMillis ()); message.setMsg ("123"); message.setSendTime (new Date ()); ListenableFuture test0 = kafkaTemplate.send (" newtopic ", gson.toJson (message)) Consumer package com.example.demo.service; import org.apache.kafka.clients.consumer.ConsumerRecord;import org.springframework.kafka.annotation.KafkaListener;import org.springframework.stereotype.Service; import java.util.Optional; @ Servicepublic class KafkaReceiver {@ KafkaListener (topics = {"newtopic"}) public void listen (ConsumerRecord record) {Optional kafkaMessage = Optional.ofNullable (record.value ()) If (kafkaMessage.isPresent ()) {Object message = kafkaMessage.get (); System.out.println ("record =" + record); System.out.println ("message =" + message);} 6, Test

Simulate the message producer in the startup method and send the message to kafka

Package com.example.demo; import com.example.demo.service.KafkaSender;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext; @ SpringBootApplicationpublic class KafkademoApplication {public static void main (String [] args) {ConfigurableApplicationContext context = SpringApplication.run (KafkademoApplication.class, args); KafkaSender sender = context.getBean (KafkaSender.class); for (int I = 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report