SpringBoot配置Kafka的生产者与消费者
1. 配置pom.xml 文件
2. 配置application.properties 文件
#zookeeper连接地址
kafka.consumer.zookeeper.connect=192.168.16.23:8082
#消费者集群地址ip+port,可以是多个,逗号分隔
kafka.consumer.servers=192.168.16.22:8083
#是否自动提交
kafka.consumer.enable.auto.commit=true
#连接超时时间
kafka.consumer.session.timeout=6000
#刷新间隔时间,负值失败时候刷新,0每次发送后刷新
kafka.consumer.auto.commit.interval=100
#latest-各分区有提交的offset从提交地方消费,没有实时生成实时消费,不会从头消费
#earliest-各分区有提交的offset从提交的地方开始消费,没有提交的从头开始消费
#none-各分区都有提交的offset从提交的地方开始消费,有一个分区没有提交的抛出异常
kafka.consumer.auto.offset.reset=latest
#消费topic
kafka.consumer.topic=topic1
#消费组id
kafka.consumer.group.id=group1
#消费线程数
kafka.consumer.concurrency=10
#生产者集群地址ip+port,可以是多个,逗号分隔
kafka.producer.servers=15.128.16.232:8083
#重试次数,默认为0不重试
kafka.producer.retries=0
#批处理字节数,默认16384(16K)
kafka.producer.batch.size=4096
#逗留时间,延时作用,默认为0立即发送
kafka.producer.linger=1
#内存大小,默认33554432(32M),当生成速度大于接收速度,用于缓存消息
kafka.producer.buffer.memory=40960
#消息topic
kafka.topic.default=topic1
3. 生产者配置
package com.xuexi.kafka.config;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
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.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.serializer.JsonDeserializer;
import org.springframework.kafka.support.serializer.JsonSerializer;
@Configuration
@EnableKafka
public class KafkaProducerConfig {
@Value("${kafka.producer.servers}")
private String servers;
@Value("${kafka.producer.retries}")
private int retries;
@Value("${kafka.producer.batch.size}")
private int batchSize;
@Value("${kafka.producer.linger}")
private int linger;
@Value("${kafka.producer.buffer.memory}")
private int bufferMemory;
public Map
Map
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
props.put(ProducerConfig.RETRIES_CONFIG, retries);
props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
props.put(ProducerConfig.LINGER_MS_CONFIG, linger);
props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
}
public ProducerFactory
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
@Bean
public KafkaTemplate
return new KafkaTemplate<>(producerFactory());
}
}
4. 消费者配置
package com.xuexi.kafka.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.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.support.serializer.JsonDeserializer;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableKafka
public class KafkaConsumerConfig {
@Value("${kafka.consumer.servers}")
private String servers;
@Value("${kafka.consumer.enable.auto.commit}")
private boolean enableAutoCommit;
@Value("${kafka.consumer.session.timeout}")
private String sessionTimeout;
@Value("${kafka.consumer.auto.commit.interval}")
private String autoCommitInterval;
@Value("${kafka.consumer.group.id}")
private String groupId;
@Value("${kafka.consumer.auto.offset.reset}")
private String autoOffsetReset;
@Value("${kafka.consumer.concurrency}")
private int concurrency;
@Bean
public KafkaListenerContainerFactory
ConcurrentKafkaListenerContainerFactory
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(concurrency);
factory.getContainerProperties().setPollTimeout(1500);
return factory;
}
private ConsumerFactory
return new DefaultKafkaConsumerFactory<>(
consumerConfigs(),
new StringDeserializer(),
new StringDeserializer()
);
}
private Map
Map
propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
return propsMap;
}
}
5. 生产者
package com.xuexi.kafka.producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
public class SimpleProducer {
@Autowired
@Qualifier("kafkaTemplate")
private KafkaTemplate kafkaTemplate;
public void send(String topic, String message) {
kafkaTemplate.send(topic, message);
}
}
6. 消费者
package com.xuexi.kafka.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SimpleConsumer {
@KafkaListener(topics = "${kafka.topic.default}", containerFactory = "kafkaListenerContainerFactory")
public void receive(String message) {
if(message.contains("key")){
//dosomething
log.info(message);
}
}
}
7. 生产者测试
package com.xuexi.kafka.controller;
import com.xuexi.common.JSONResult;
import com.xuexi.kafka.producer.SimpleProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/kafka")
public class ProduceController {
@Autowired
private SimpleProducer simpleProducer;
@Value("${kafka.topic.default}")
private String topic;
@RequestMapping(value = "/sendKafka", method = RequestMethod.GET, produces = {"application/json"})
public JSONResult sendKafka() {
SimpleProducer.send(topic, "测试");
return JSONResult.ok();
}
}
8. 样例下载链接 https://download.csdn.net/download/u010782875/13609247
————————————————
版权声明:本文为CSDN博主「擦肩回眸2011」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u010782875/article/details/110947316