pom文件:
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>kafka_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>kafka_demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.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> <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>2.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第一种: 常见的生产者:
package com.example.kafka_demo; import org.apache.kafka.clients.producer.*; import java.util.Properties; public class Demo { public static void main(String[] args) { Properties props = new Properties(); props.put("bootstrap.servers", "192.168.8.20:9092"); props.put("acks", "0"); props.put("retries", 0); // props.put("batch.size", 16384); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer producer = new KafkaProducer(props); //正常发送消息的producer // ProducerRecord<String, String> record = new ProducerRecord<>("first", "333"); //带key的producer // ProducerRecord<String, String> record = new ProducerRecord<>("first","key", "3312333"); //指定分区的producer ProducerRecord<String, String> record = new ProducerRecord<>("first",0,"key", "lover"); producer.send(record); producer.close(); } }
第二种: 生产者带回调函数
package com.example.kafka_demo; import org.apache.kafka.clients.producer.*; import java.util.Properties; public class Demo { public static void main(String[] args) { Properties props = new Properties(); props.put("bootstrap.servers", "192.168.8.20:9092"); props.put("acks", "1"); props.put("retries", 0); // props.put("batch.size", 16384); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer producer = new KafkaProducer(props); ProducerRecord<String, String> record = new ProducerRecord<>("first", 0, "123", "haasdf"); producer.send(record, (metadata, e) -> { if (e != null) { e.printStackTrace(); } System.out.println("success:"+ metadata); }); producer.close(); } }