springoot 整合kafka 入门
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
spring:
kafka:
bootstrap-servers: localhost:9092
简单说明
@KafkaListener
//可以监听多个主题
@KafkaListener(topice = "topic1,topic2....")
//通过 @Header 来获取当前消息 来自的分支
@KafkaListener(topice = "topic", groupId = "groupid")
void listen( @Payload String message,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partiton){
message....;
}
效果
// 指定 接收的分区消息
@KafkaListener(groupId = "test-consumer",
topicPartitions = @TopicPartition(topic = "test",
partitionOffsets = {
@PartitionOffset( partition = "0",initialOffset = "0")
}))
/* @KafkaListener(groupId = "test-consumer",
topicPartitions = @TopicPartition(topic = "test", partitions = { "0", "1" }))*/ //不指定 initialOffset的写法
public void listen(@Payload String message,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition) {
log.info("接收消息: {},partition:{}", message, partition);
}
配置类
目前内容中使用的都是 String 简单类型的消息设置
# linux 创建 topic
./kafka-topics.sh --create --zookeeper 119.91.60.82:2181 --replication-factor 1 --partitions 1 --topic test
Created topic test.
消费者
package com.lfweixiao.demo.config;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import java.util.HashMap;
/**
* @author LFweixiao
* @create 2022-05-05 8:52 下午
* kafka消费者 consumer
*/
/*
配置类上使用了@EnableKafka 才能在spring 托管的Bean上检测 @KafkaListener 注解
需要 ioc 容器中有 ConsumerFactory
KafkaListenerContainerFactory
*/
@EnableKafka
@Configuration
public class KafkaConsumerConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Value("${spring.kafka.consumer.group-id}")
private String consumerGroupId;
@Value("${spring.kafka.consumer.auto-offset-reset}")
private String autoOffsetReset;
@Bean
public ConsumerFactory<String,String> consumerFactory() {
HashMap<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,// 服务地址
bootstrapServers);
props.put(ConsumerConfig.GROUP_ID_CONFIG, //消费组
consumerGroupId);
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, //消息策略
autoOffsetReset);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, //key 串行化转换
StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,//value 串行化转换
StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(props);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String,String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
生产者
package com.lfweixiao.demo.config;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import java.util.HashMap;
/**
* @author LFweixiao
* @create 2022-05-05 7:44 下午
* 配置一个生产者 producer
*/
@Configuration
public class KafkaProducerConfig {
//读取服务地址
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public ProducerFactory<String,String> producerFactory() {
HashMap<String, Object> configProps = new HashMap<>();
configProps.put(
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,//服务地址
bootstrapServers);
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,//序列化 key
StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, //序列化 value
StringSerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
// 主要使用这个对象来发送消息
@Bean
public KafkaTemplate<String,String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
监听器
package com.lfweixiao.demo.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
/**
* @author LFweixiao
* @create 2022-05-06 1:16 下午
* 监听消息
*/
@Component
public class KafkaMessageListener {
public static final Logger log = LoggerFactory.getLogger(KafkaMessageListener.class);
//监听 主题 test 分组为 test-consumer 的消息
@KafkaListener(topics = "test", groupId = "test-consumer")
public void listen(String message) {
log.info("消息接收{}",message);
}
}
测试类
package com.lfweixiao.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
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;
/**
* @author LFweixiao
* @create 2022-05-05 7:57 下午
* 客户端来送信息
*/
@RestController
public class SendMessageController {
public static final Logger log = LoggerFactory.getLogger(SendMessageController.class);
//发送消息的模版,key -value 都是string 类型
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
@GetMapping("send/{message}")
public void send(@PathVariable("message")String message){
//kafkaTemplate.send("test01",message);//send 是异步方法
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("test", message);
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onFailure(Throwable ex) {
log.error("消息:{}发送失败,原因:{}",message,ex.getMessage());
}
@Override
public void onSuccess(SendResult<String, String> result) {
log.info("消息:{}发送成功,offset=[{}]",message,result.getRecordMetadata().offset());
}
});
}
}
控制台
消息是对象的话
- 是体力继承 Serializable
- 转换成json。或者在设置里面设置 json格式传输