消息队列kafka(二)--与spring整合(kafkaTemplate方式)
来源:互联网 发布:汉宁窗 c语言 编辑:程序博客网 时间:2024/06/09 13:21
一、简介
在这里介绍kafka与spring的整合,这里采用kafkaTemplate方式。
二、生产者开发步骤
1、添加maven依赖(略去spring依赖,请自行添加)
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.9.2</artifactId> <version>0.8.2.1</version></dependency><dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>1.0.0</version></dependency>2、添加生产者spring配置
主要包含配置kafka生产者连接参数,产生kafka生产者工厂bean以及产生kafkaTemplate的bean。
实例如下:
<?xml version="1.0" encoding="UTF-8"?><beans 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"> <bean id="kafkaProducerProperites" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="192.168.0.107:9092" /> <entry key="group.id" value="0"/> <entry key="retries" value="3"/> <entry key="batch.size" value="16384"/> <entry key="linger.ms" value="1"/> <entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer" /> <entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer" /> </map> </constructor-arg> </bean> <bean id="kafkaProducerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory"> <constructor-arg ref="kafkaProducerProperites"/> </bean> <bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate"> <constructor-arg ref="kafkaProducerFactory" /> <constructor-arg name="autoFlush" value="true"/> <property name="defaultTopic" value="defaultTopic"/> </bean></beans>3、生产者产生kafka消息
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依赖,请自行添加)
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.9.2</artifactId> <version>0.8.2.1</version></dependency><dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>1.0.0</version></dependency>2、添加kafka消费监听器
import org.apache.kafka.clients.consumer.ConsumerRecord;import org.springframework.kafka.listener.MessageListener;import org.springframework.stereotype.Component;@Component("kafkaConsumerListener")public class KafkaConsumerListener implements MessageListener<String, String> { @Override public void onMessage(ConsumerRecord<String, 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); }}3、添加消费者spring配置
<?xml version="1.0" encoding="UTF-8"?><beans 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" /> <bean id="kafkaConsumerProperites" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="192.168.0.107:9092" /> <entry key="group.id" value="0"/> <entry key="enable.auto.commit" value="true"/> <entry key="auto.commit.interval.ms" value="1000"/> <entry key="session.timeout.ms" value="30000"/> <entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer" /> <entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer" /> </map> </constructor-arg> </bean> <bean id="kafkaConsumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory"> <constructor-arg ref="kafkaConsumerProperites"/> </bean> <bean id="consumerContainerProperties" class="org.springframework.kafka.listener.config.ContainerProperties"> <constructor-arg value="topicOne"/> <property name="messageListener" ref="kafkaConsumerListener" /> </bean> <bean id="conusmerContainer" class="org.springframework.kafka.listener.KafkaMessageListenerContainer"> <constructor-arg ref="kafkaConsumerFactory"/> <constructor-arg ref="consumerContainerProperties"/> </bean></beans>4、启动消费者监听器
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
阅读全文
0 0
- 消息队列kafka(二)--与spring整合(kafkaTemplate方式)
- mybatis与spring整合(方式二)
- 消息队列 RabbitMQ 与 Spring 整合使用
- 消息队列 RabbitMQ 与 Spring 整合使用
- 消息队列 RabbitMQ 与 Spring 整合使用
- 消息队列 RabbitMQ 与 Spring 整合使用
- 【消息队列】二、Spring整合JMS(消息中间件)实例
- springMVC+MQ 消息队列整合(二)
- spring整合消息队列rabbitmq
- spring整合消息队列rabbitmq
- spring整合消息队列rabbitmq
- spring整合消息队列rabbitmq
- spring整合消息队列rabbitmq
- 消息队列ActiveMQ+Spring整合
- spring整合消息队列rabbitmq
- spring整合消息队列rabbitmq
- spring整合消息队列rabbitmq
- spring与struts整合方式二
- Java连接Access数据库
- linux下gdb如何重定向输入
- 基于Struts2和hibernate的WebSocket聊天室的实现教程四:实现登录接口
- 14. ArrayList、LinkedList、Vector的区别。
- checkpoint机制,show engine innodb status
- 消息队列kafka(二)--与spring整合(kafkaTemplate方式)
- HDU-5575-Discover Water Tank
- Oracle 创建用户
- Android相关
- soj1103:IP判断_情况巨多的水题
- tensorflow基础--Regression
- 16 STL集合和映射容器set/multiset_map/multimap(学自Boolean)
- scrapy 安装
- Unity FPS帧率计算