一、依赖引入
<!--kafka-->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>${kafka.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.client.version}</version>
</dependency>
注意,springboot与kafka之前有对应关系,官网:Spring for Apache Kafka
二、配置application.yml
spring:
kafka:
bootstrap-servers: ip:port
#key、value默认是以序列化的方式发送的,局限性大,所以修改机制为字符串方式
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
# 生产者每个批次最多放多少条记录
batch-size: 16384
# 生产者一端总的可用发送缓冲区大小,单位为字节,此处设置为32MB
buffer-memory: 33554432
#消费者需要反序列化字符串
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
group-id: helloGroup
# 如果在kakfa中找不到当前消费者的偏移量,则直接将偏移量重置为最早的
auto-offset-reset: earliest
# 消费者的偏移量是自动提交还是手动提交,此处自动提交
enable-auto-commit: true
# 消费者的偏移量多长时间设置提交一次
auto-commit-interval: 1000
三、生产者
public class CzxxServiceImpl implements CzxxService {
@Autowired(required = false)
KafkaTemplate<String,String> kafkaTemplate;
@Override
public String producer(String key, Integer value) {
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("redis_update", 0,key, value.toString());
//设定回调结果,异步等待broker端的返回结果
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onFailure(Throwable throwable) {
log.info("生产者消息发送失败,失败原因:{}",throwable.getMessage());
}
@Override
public void onSuccess(SendResult<String, String> stringStringSendResult) {
RecordMetadata metadata = stringStringSendResult.getRecordMetadata();
log.info("生产者消息发送成功:topic为{},partition为{}, offset为{}", metadata.topic(), metadata.partition(), metadata.offset());
}
});
return "success";
}
@GetMapping("/send2")
public String send2(){
/**
* 发送消息为对象时
* 使用fastjson把对象转换为json字符串
*/
kafkaTemplate.send("hello_pojo","002", JSON.toJSONString(new User("zhangsan",18)));
return "send successfully!";
}
}
四、消费者
public class KafkaConsumer {
@Autowired
JedisUtils jedisUtils;
@KafkaListener(topics = "redis_update")
public void receive(ConsumerRecord<String,String> record){
Jedis jedis = jedisUtils.getJedis();
String update = jedis.set(record.key(), record.value());
jedisUtils.close(jedis);
log.info("消费者消费成功,消费的内容为:key:{},value:{},offset:{},result:{}", record.key(), record.value(), record.offset(), update);
}
}