pom.xml
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
application.properties
# kafka相关配置
spring.kafka.bootstrap-servers=192.168.137.100:9092
spring.kafka.producer.retries=0
spring.kafka.producer.acks=1
spring.kafka.producer.batch-size=16384
spring.kafka.producer.properties.linger.ms=0
spring.kafka.producer.buffer-memory = 33554432
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=com.hurricane.kafka.utils.EncodeingKafka
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto.commit.interval.ms=1000
spring.kafka.consumer.auto-offset-reset=earliest #latest
spring.kafka.consumer.properties.session.timeout.ms=120000
spring.kafka.consumer.properties.request.timeout.ms=180000
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=com.hurricane.kafka.utils.DecodeingKafka
spring.kafka.listener.missing-topics-fatal=false
消费者:
package com.hurricane.kafka.consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
public class KafkaConsumer {
@KafkaListener(topics = {"testtopic"},groupId = "defaultConsumerGroup")
public void onMessage1(ConsumerRecord<?, ?> record){
System.out.println("简单消费:"+record.topic()+"-"+record.partition()+"-"+record.value());
}
}
生产者:
package com.hurricane.kafka.producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class KafkaProducer {
@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;
// 发送消息
@GetMapping("/kafka/callback/{message}")
public void sendMessage3(@PathVariable("message") String callbackMessage) {
System.out.println(callbackMessage);
kafkaTemplate.send("testtopic", callbackMessage).addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
public void onFailure(Throwable ex) {
System.out.println("发送消息失败:"+ex.getMessage());
}
public void onSuccess(SendResult<String, Object> result) {
System.out.println("发送消息成功:" + result.getRecordMetadata().topic() + "-" + result.getRecordMetadata().partition() + "-" + result.getRecordMetadata().offset());
}
});
}
}
为了能够将json类型作为消息成功发送,创建消息编解码类:
编码类:
package com.hurricane.kafka.utils;
import java.util.Map;
import org.apache.kafka.common.serialization.Serializer;
public class EncodeingKafka implements Serializer<Object> {
@Override
public void configure(Map configs, boolean isKey) {}
@Override
public byte[] serialize(String topic, Object data) {
return BeanUtils.bean2Byte(data);
}
@Override
public void close() {
System.out.println("EncodeingKafka is close");
}
}
解码类:
package com.hurricane.kafka.utils;
import java.util.Map;
import org.apache.kafka.common.serialization.Deserializer;
public class DecodeingKafka implements Deserializer<Object> {
@Override
public void configure(Map<String, ?> configs, boolean isKey) {}
@Override
public Object deserialize(String topic, byte[] data) {
return BeanUtils.byte2Obj(data);
}
@Override
public void close() {}
}
工具类:
package com.hurricane.kafka.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class BeanUtils {
private BeanUtils() {}
/**
* 对象序列化为byte数组
*
* @param obj
* @return
*/
public static byte[] bean2Byte(Object obj) {
byte[] bb = null;
try (ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArray)){
outputStream.writeObject(obj);
outputStream.flush();
bb = byteArray.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return bb;
}
/**
* 字节数组转为Object对象
*
* @param bytes
* @return
*/
public static Object byte2Obj(byte[] bytes) {
Object readObject = null;
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream inputStream = new ObjectInputStream(in)){
readObject = inputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return readObject;
}
}