Spring-boot Integrated Kafka

Source: Internet
Author: User
1 Configuring 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 Configuring Application.properties

Springboot will already support this configuration and can be configured with code hints:

spring.kafka.bootstrap-servers=192.168.200.64:9092
Spring.kafka.consumer.group-id=dpifiltertolog
Spring.kafka.consumer.auto-offset-reset=latest
3 Writing consumer listeners

A thread pool is used in consumer monitoring, and useless code can ignore

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 D
    Mpdata = 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 Writing configuration Classes
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 () {//Configure monitoring 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 Producers do not need to configure, their own online search how to use the OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.