在网络上传输消息,所有的对象必须经过序列化,接收消息要反序列话。
自定义对象的关键其实就是如果序列化和反序列话,我们使用FastJson来序列化自定义对象。
需要俩个依赖
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
</dependencies>
自定义对象
public class Student
{
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
对该对象进行序列化
import org.apache.kafka.common.serialization.Serializer;
import com.alibaba.fastjson.JSON;
public class StudentSerializer implements Serializer<Student>
{
@Override
public byte[] serialize(String topic, Student data) {
return JSON.toJSONBytes(data);
}
}
自定义反序列化
import org.apache.kafka.common.serialization.Deserializer;
import com.alibaba.fastjson.JSON;
public class StudentDeserializer implements Deserializer<Student>
{
@Override
public Student deserialize(String topic, byte[] data) {
return JSON.parseObject(data, Student.class);
}
}
消息接受者
import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
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;
public class MyConsumer
{
@SuppressWarnings("resource")
public static void main(String[] args) {
//指定kafkaconsumer的配置参数
Properties properties = new Properties();
properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.112.111:9092,192.168.112.111:9093");
//设置key和value序列化
properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "day0603.StudentDeserializer");
//设置分组
properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "group1");
KafkaConsumer<String, Student> consumer=new KafkaConsumer<>(properties);
//订阅主题
consumer.subscribe(Arrays.asList("mytopic1"));
//接收消息
while (true)
{
ConsumerRecords<String,Student> records = consumer.poll(Duration.ofSeconds(2));
for (ConsumerRecord<String, Student> consumerRecord : records)
{
System.out.println("key ="+consumerRecord.key());
System.out.println("value ="+consumerRecord.value().toString());
}
}
}
}
生产者
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
public class MyProducer
{
public static void main(String[] args) {
//是指producer的可配参数
Properties conf = new Properties();
conf.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.112.111:9092,192.168.112.111:9093");
//设置key和value序列化方式
conf.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
conf.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "day0603.StudentSerializer");
//创建producer
Producer<String, Student> producer=new KafkaProducer<>(conf);
for (int i = 0; i < 10; i++)
{
Student student = new Student();
student.setId(i);
student.setName("lily_"+i);
student.setAge(20+i);
ProducerRecord<String, Student> record = new ProducerRecord<>("mytopic1", "key_"+i, student);
producer.send(record);
}
producer.close();
}
}