1.导入依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
2.编写配置文件
server:
port: 8080
#kafka配置
spring:
kafka:
bootstrap-servers: 123.60.111.191:9091
producer: #生产者配置
retries: 3 #设置大于0的值,则客户端会将发送失败的记录重新发送
batch-size: 16384
buffer-memory: 33554432
acks: 1
#指定key和value的序列化
key-serializer: org.apache.kafka.common.serialezation.StringSerializer
value-serializer: org.apache.kafka.common.serialezation.StringSerializer
consumer:
group-id: group-lzl
enable-auto-commit: false
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialezation.StringSerializer
value-deserializer: org.apache.kafka.common.serialezation.StringSerializer
listener:
#手动调用Acknowledgment.acknowledge()后立即提交,一般使用这种
ack-mode: manual_immediate
3.编写生产者
package com.lzl.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/msg")
public class MyKafkaController {
private final static String TOPIC_NAME = "my-one-topic";
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
@RequestMapping("/send")
public String sendMessage(){
kafkaTemplate.send(TOPIC_NAME,0,"key","this is a message");
return "success";
}
}
4.编写消费者
package com.lzl.consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;
/**
* 消费者
*/
@Component
public class MyConsumer {
@KafkaListener(topics = "my-one-topic",groupId = "group-lzl")
public void listenGroup(ConsumerRecord<String,String> record, Acknowledgment ack){ //手动提交ack才生效
String value = record.value();
System.out.println("value="+value + "|record="+record);
//手动提交offset
ack.acknowledge();
}
}
消费者中配置主题 分区 和偏移量
//消费者中配置主题 分区 和偏移量
@KafkaListener(groupId = "group-lzl",topicPartitions =
{ @TopicPartition( topic = "topic1",partitions = {"0","1"}),
@TopicPartition(topic = "topic2",partitions = "0",
partitionOffsets = @PartitionOffset(partition = "1",initialOffset = "100"))})
public void listenGroup1(ConsumerRecord<String,String> record, Acknowledgment ack){ //手动提交ack才生效
String value = record.value();
System.out.println("value="+value + "|record="+record);
//手动提交offset
ack.acknowledge();
}