spring-boot集成kafka

1 配置pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

2 配置application.properties

springboot已经将支持此配置,可以使用代码提示进行配置:

spring.kafka.bootstrap-servers=192.168.200.64:9092
spring.kafka.consumer.group-id=dpiFilterToLog
spring.kafka.consumer.auto-offset-reset=latest

3 编写consumer监听程序

消费者监听中使用了线程池,无用代码可以无视

package com.eversec.windowtool.modules.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.eversec.windowtool.modules.scene.service.SceneLookupService;

@Component
public class ConsumerThread implements MessageListener<String, String>{
    private static final Logger dmpData = Logger.getLogger("dmpData");
    @Autowired
    private SceneLookupService sceneLookupService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Override
    public void onMessage(ConsumerRecord<String, String> data) {
        String dpiValue = data.value();
        dmpData.info(dpiValue);
        this.threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                sceneLookupService.dmpLogLookup(dpiValue);
            }
        });

    }

}

4 编写配置类

package com.eversec.windowtool.common.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.config.ContainerProperties;
import org.springframework.stereotype.Component;

import com.eversec.windowtool.modules.kafka.ConsumerThread;

@Configuration
@Component
public class KafkaConfig {

    @Autowired
    private ConsumerThread consumerThread;

    @Bean
    ContainerProperties containerProperties(){
        //配置监听topic
         ContainerProperties containerProperties = new ContainerProperties("dpiLogNeedLookup");
         containerProperties.setMessageListener(consumerThread);
         return containerProperties;
    }
    @Bean
    KafkaMessageListenerContainer<String, String> messageListenerContainer(ConsumerFactory<String, String> consumerFactory,
            ContainerProperties containerProperties){
        return new KafkaMessageListenerContainer<String, String>(consumerFactory, containerProperties);
    }


}

5 生产者不需要配置,自己网上搜索怎么用就ok了。

猜你喜欢

转载自blog.csdn.net/u010207995/article/details/67641321