SpringBoot配置Kafka的生產者與消費者

SpringBoot配置Kafka的生產者與消費者

1. 配置pom.xml 文件

<dependency>

<groupId>org.springframework.kafka</groupId>

<artifactId>spring-kafka</artifactId>

</dependency>

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<String, Object> producerConfigs() {

? ? ? ? Map<String, Object> props = new HashMap<>();

? ? ? ? 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<String, String> producerFactory() {

? ? ? ? return new DefaultKafkaProducerFactory<>(producerConfigs());

? ? }

? ? @Bean

? ? public KafkaTemplate<String, String> 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<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {

? ? ? ? ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();

? ? ? ? factory.setConsumerFactory(consumerFactory());

? ? ? ? factory.setConcurrency(concurrency);

? ? ? ? factory.getContainerProperties().setPollTimeout(1500);

? ? ? ? return factory;

? ? }

? ? private ConsumerFactory<String, String> consumerFactory() {

? ? ? ? return new DefaultKafkaConsumerFactory<>(

? ? ? ? ? ? ? ? consumerConfigs(),

? ? ? ? ? ? ? ? new StringDeserializer(),

? ? ? ? ? ? ? ? new StringDeserializer()

? ? ? ? );

? ? }

? ? private Map<String, Object> consumerConfigs() {

? ? ? ? Map<String, Object> propsMap = new HashMap<>();

? ? ? ? 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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容