Simple Kafka java program creates producers and consumers
1. Preset conditions
Kafka basic installation and use
2. Create a springboot project
2.1 Introduce related dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyd</groupId>
<artifactId>test-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<kafka.version>2.0.0</kafka.version>
<scala.version>2.11</scala.version>
<lombik.version>1.18.8</lombik.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.version}</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_${scala.version}</artifactId>
<version>${kafka.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 Create a producer
package com.example.kafkatest.producer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
public class ProducerTest {
private static final String brokerList = "localhost:9092";
private static final String topic = "hello";
public static void main(String[] args) {
//设置配置项
Properties properties = new Properties();
//设置key和value的序列化
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
//设置重试次数
properties.put(ProducerConfig.RETRIES_CONFIG, 10);
//设置集群地址
properties.put("bootstrap.servers", brokerList);
//创建生产者
KafkaProducer<String, String> kafkaProducer = new KafkaProducer(properties);
//设置topic及消息
ProducerRecord<String,String> record = new ProducerRecord<>(topic, "kafka-demo", "hhh");
//消息发送
kafkaProducer.send(record);
//关闭生产者
kafkaProducer.close();
}
}
2.3 Startup verification
The prerequisite is that the related kafka and zookeeper services on the machine have been started. For details, refer to the preset conditions in section 1, and kafka has created related topics
Start the main method of this class
View the consumer window opened on the command window
2.4 Create consumers
package com.example.kafkatest.consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
public class ConsumerTest {
private static final String brokerList = "localhost:9092";
private static final String topic = "hello";
private static final String groupId = "group.demo";
public static void main(String[] args) {
//设置配置项
Properties properties = new Properties();
//设置key和value的序列化
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
//设置集群地址
properties.put("bootstrap.servers", brokerList);
//设置groupId
properties.put("group.id", groupId);
KafkaConsumer consumer = new KafkaConsumer(properties);
//订阅主题
consumer.subscribe(Collections.singletonList(topic));
//设置监听消费
while (true){
ConsumerRecords<String, String> consumerRecords = consumer.poll(Duration.ofSeconds(1));
for (ConsumerRecord<String, String> record:consumerRecords){
System.out.println(record.value());
}
}
}
}
2.5 Startup verification
Start the consumer and output the following statement in a loop.
Start the production and send a message to view the consumer output