Spring整合Kafka的java例子demo

前提

zookeeper和kafka的win10下单机伪集群请参考:
https://blog.csdn.net/sndayYU/article/details/90718238
https://blog.csdn.net/sndayYU/article/details/90718786

代码

目录如下:
在这里插入图片描述

maven

 <!-- kafka与spring整合 -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>

整个pom.xml可以参考

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>helloworld</artifactId>
        <groupId>com.ydfind</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>kafka-demo</artifactId>
    
    <dependencies>
        <!-- 日志 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- kafka与spring整合 -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>
    </dependencies>
</project>

生产者KfkSpringProducer.java

package com.ydfind.kafka.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.kafka.core.KafkaTemplate;

public class KfkSpringProducer {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-kafka.xml");
        // KafkaTemplate是spring用来发送消息的模板类
        KafkaTemplate<String, String> kafkaTemplate = (KafkaTemplate) context.getBean("kafkaTemplate");
        kafkaTemplate.send("testGroup2", "我的测试消息1");
        System.out.println("have send msg = 我的测试消息1");
        kafkaTemplate.send("testGroup2", "我的测试消息2");
        System.out.println("have send msg = 我的测试消息2");
        kafkaTemplate.send("testGroup2", "我的测试消息3");
        System.out.println("have send msg = 我的测试消息3");
    }
}

消费者KfkConsumerListener.java

package com.ydfind.kafka.spring;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.MessageListener;
public class KfkConsumerListener implements MessageListener<String, String> {
    /**
     * 监听器自动执行该方法
     * 消费消息
     * 自动提交offset
     *
     * @param record
     */
    @Override
    public void onMessage(ConsumerRecord<String, String> record) {
        System.out.printf("partition = %d, offset = %d, key = %s, value = %s%n", record.partition(), record.offset(), record.key(), record.value());
    }
}

配置文件spring-kafka.xml

放在maven目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!--生产者配置-->
    <bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
        <constructor-arg name="configs">
            <map>
                <entry key="bootstrap.servers" value="localhost:9092,localhost:9093,localhost:9094"/>
                <entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer"/>
                <entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer"/>
                <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="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
        <constructor-arg ref="producerFactory"/>
        <constructor-arg name="autoFlush" value="true"/>
    </bean>

    <!--消费者配置 -->
    <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
        <constructor-arg name="configs">
            <map>
                <entry key="bootstrap.servers" value="localhost:9092,localhost:9093,localhost:9094"/>
                <entry key="group.id" value="testGroup2"/>
                <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="consumerListener" class="com.ydfind.kafka.spring.KfkConsumerListener"/>
    <bean id="containerProperties_example" class="org.springframework.kafka.listener.config.ContainerProperties">
        <constructor-arg value="testGroup2"/>
        <property name="messageListener" ref="consumerListener"/>
    </bean>
    <bean id="messageListenerContainer_example" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
        <constructor-arg ref="consumerFactory"/>
        <constructor-arg ref="containerProperties_example"/>
    </bean>
</beans>

运行

运行生产者KfkSpringProducer.java的main函数,结果如下面所示,可以看到,发送的消息,在消费者端能够被正常接收。
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值