码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot---Kafka

时间:2019-12-13 09:36:27      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:lin   消费   turn   create   npe   package   http   ati   context   

1、实战

    

<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.12</artifactId>
            <version>2.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-stream-kafka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>

  

package com.an.kafka.config;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 生产者配置
 * @author: anpeiyong
 * @date: Created in 2019/12/12 14:44
 * @since:
 */
@Configuration
@EnableKafka
public class KafkaProducerConfig {

    @Bean
    public ProducerFactory producerFactory() {
        return new DefaultKafkaProducerFactory(producerConfigs());
    }

    @Bean
    public Map producerConfigs() {
        Map props =new HashMap();
        //Kafka生产者配置 
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.8.172:9092");
        props.put(ProducerConfig.RETRIES_CONFIG,0);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG,16384);
        props.put(ProducerConfig.LINGER_MS_CONFIG,1);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG,33554432);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }

    @Bean
    public KafkaTemplate kafkaTemplate() {
        return new KafkaTemplate(producerFactory());
    }

}

  

package com.an.kafka.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: Kafka生产者
 * @author: anpeiyong
 * @date: Created in 2019/12/12 14:54
 * @since:
 */
@RestController
@RequestMapping("kafkaProducer")
public class KafkaProducer {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping("send/{msg}")
    public String send(@PathVariable("msg") String msg){
        kafkaTemplate.send("an","antest", msg);//topic_name是指发送信息的目标主题
        return "success";
    }
}

  

package com.an.kafka.config;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 消费者配置
 * @author: anpeiyong
 * @date: Created in 2019/12/12 15:06
 * @since:
 */
@Configuration
@EnableKafka
public class KafkaConsumerConfig {

    @Bean
    public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }

    @Bean
    public ConsumerFactory consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public Map consumerConfigs() {
        Map props =new HashMap<>();
        //消费者参数设置
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.8.172:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG,"test-consumer-group");
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true);
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,"15000");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return props;
    }

    @Bean
    public KafkaProperties.Listener listener() {
        return new KafkaProperties.Listener();
    }

}

  

package com.an.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @description:  kafka消费者
 * @author: anpeiyong
 * @date: Created in 2019/12/12 15:10
 * @since:
 */
@Component
public class KafkaConsumer {

    @KafkaListener(topics ="an")
    public void listen (ConsumerRecord record){
        System.out.printf("topic = %s, offset = %d, value = %s \n", record.topic(), record.offset(), record.value());
    }
}

  

结果:

topic = an, offset = 0, value = hello 

  

SpringBoot---Kafka

标签:lin   消费   turn   create   npe   package   http   ati   context   

原文地址:https://www.cnblogs.com/anpeiyong/p/12033176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!