一、简介
在这里介绍kafka与spring的整合,这里采用kafkaTemplate方式。
二、生产者开发步骤
1、添加maven依赖(略去spring依赖,请自行添加)
2、添加生产者spring配置org.apache.kafka kafka_2.9.2 0.8.2.1 org.apache.kafka kafka-clients 1.0.0
主要包含配置kafka生产者连接参数,产生kafka生产者工厂bean以及产生kafkaTemplate的bean。
实例如下:
xml version="1.0" encoding="UTF-8"?>3、生产者产生kafka消息xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> id="kafkaProducerProperites" class="java.util.HashMap"> key="bootstrap.servers" value="192.168.0.107:9092" /> key="group.id" value="0"/> key="retries" value="3"/> key="batch.size" value="16384"/> key="linger.ms" value="1"/> key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer" /> key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer" /> id="kafkaProducerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory"> ref="kafkaProducerProperites"/> id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate"> ref="kafkaProducerFactory" /> name="autoFlush" value="true"/> name="defaultTopic" value="defaultTopic"/>
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.kafka.core.KafkaTemplate; public class SpringProducerMain { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-producer.xml"); KafkaTemplate kafkaTemplate = ctx.getBean("kafkaTemplate", KafkaTemplate.class); for (int i = 1; i < 5; i++) { String msg = "msg-" + i; //向topicOne发送消息 kafkaTemplate.send("topicOne", msg); System.out.println("send msg : " + msg); } } }
输出:
send msg : msg-1
send msg : msg-2
send msg : msg-3
send msg : msg-4
1、添加maven依赖(略去spring依赖,请自行添加)
2、添加kafka消费监听器org.apache.kafka kafka_2.9.2 0.8.2.1 org.apache.kafka kafka-clients 1.0.0
import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.listener.MessageListener; import org.springframework.stereotype.Component; @Component("kafkaConsumerListener") public class KafkaConsumerListener implements MessageListener3、添加消费者spring配置, String> { @Override public void onMessage(ConsumerRecord , String> record) { String topic = record.topic(); String key = record.key(); String val = record.value(); long offset = record.offset(); int partition = record.partition(); System.out.printf("receive msg -- topic:%s key:%s val:%s offset:%s partition:%s \r\n",topic,key,val,offset,partition); } }
xml version="1.0" encoding="UTF-8"?>4、启动消费者监听器xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.dragon.study" /> id="kafkaConsumerProperites" class="java.util.HashMap"> key="bootstrap.servers" value="192.168.0.107:9092" /> key="group.id" value="0"/> key="enable.auto.commit" value="true"/> key="auto.commit.interval.ms" value="1000"/> key="session.timeout.ms" value="30000"/> key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer" /> key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer" /> id="kafkaConsumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory"> ref="kafkaConsumerProperites"/> id="consumerContainerProperties" class="org.springframework.kafka.listener.config.ContainerProperties"> value="topicOne"/> name="messageListener" ref="kafkaConsumerListener" /> id="conusmerContainer" class="org.springframework.kafka.listener.KafkaMessageListenerContainer"> ref="kafkaConsumerFactory"/> ref="consumerContainerProperties"/>
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.concurrent.TimeUnit; public class SpringConsumerMain { public static void main(String[] args) throws InterruptedException { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-consumer.xml"); TimeUnit.HOURS.sleep(1); } }输出:
receive msg -- topic:topicOne key:null val:msg-1 offset:638 partition:0
receive msg -- topic:topicOne key:null val:msg-2 offset:639 partition:0
receive msg -- topic:topicOne key:null val:msg-3 offset:640 partition:0
receive msg -- topic:topicOne key:null val:msg-4 offset:641 partition:0