In the previous article “Installing Kafka on Linux”, we covered how to install Kafka on Linux, how to start/stop Kafka, and how to create posts and generate messages and consume messages. This article introduces SpringBoot integration Kafka.

V Creating a project

If kafka is added to an existing project, skip to 1.3

1.1 create springboot:

1.2 Web and Kafka:

1.3 Add Kafka to existing projects and add dependencies to POM.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
Copy the code

1.4 Overall Architecture Catalogue:

V Configuration Items

2.1 set up application. Yml

Spring: kafka: bootstrap-Servers: 127.0.0.1:9092 # org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer consumer: group-id: Default_consumer_group # Group ID enable-auto-commit: true auto-commit-interval: 1000 key-deserializer: org.apache.kafka.common.serialization.StringDeserializer value-deserializer: org.apache.kafka.common.serialization.StringDeserializer server: port: 8500Copy the code

The project generates applicaton.properties by default, just rename the modified file to yML.

2.2 Adding a Producer ProducerController

package com.toutou.Controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author toutou * @date by 2019/08 */ @RestController public class ProducerController { @Autowired private KafkaTemplate<String,Object> kafkaTemplate; @RequestMapping("message/send") public String send(String msg){ kafkaTemplate.send("demo", msg); Return "success"; }}Copy the code

2.3 Add ConsumerDemo

package com.toutou.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @date by 2019/08 */ @component public class ConsumerDemo {/** * Define the consumer to receive a Topic = "demo" message that corresponds to the topic in the Controller * @param Record variable representing the message itself, which can be passed to ConsumerRecord<? ,? > type of record variable to print various messages received */ @kafkalistener (topics = "demo") public void listen (ConsumerRecord<? ,? > record){ System.out.printf("topic is %s, offset is %d, value is %s \n", record.topic(), record.offset(), record.value()); }}Copy the code

V Start-up test

3.1 Test producers

3.2 Consumer Effect

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \