Spring Boot系列 八、集成Kafka

一、引入依赖

 <dependency>
            <groupId>org.springframework.kafkagroupId>
            <artifactId>spring-kafkaartifactId>
            <version>2.4.0.RELEASEversion>
        dependency>

二、编写配置文件

spring:
  application: kafka
  kafka:
    bootstrap-servers: ip:9092
    producer:
      retries: 1
      batch-size: 16384
      buffer-memory: 1024000
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
    consumer:
      enable-auto-commit: true
      auto-commit-interval: 10
      auto-offset-reset: earliest
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

三、注入生产者

@Configuration
public class ProductorConfiugutration {

    @Value("${spring.kafka.bootstrap-servers}")
    private String server;
    @Value("${spring.kafka.producer.buffer-memory}")
    private Integer bufferMemory;
    @Value("${spring.kafka.producer.batch-size}")
    private Integer batchSize;
    @Value("${spring.kafka.producer.retries}")
    private int retries;
    @Value("${spring.kafka.producer.key-serializer}")
    private String keySerializer;
    @Value("${spring.kafka.producer.value-serializer}")
    private  String valueSerlializer;

    @Bean("peopleTemplate")
    public KafkaTemplate getPeopleTemplate() {
        KafkaTemplate peopleTemple = new KafkaTemplate(getFactory());
        peopleTemple.setDefaultTopic("topic.people");
        return peopleTemple;
    }

    public ProducerFactory getFactory() {
        return new DefaultKafkaProducerFactory(getProductorConfig());
    }

    public Map getProductorConfig() {
        Map hashMap = new HashMap();
        hashMap.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, server);
        hashMap.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
        hashMap.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
        hashMap.put(ProducerConfig.RETRIES_CONFIG, retries);
        hashMap.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,keySerializer);
        hashMap.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,valueSerlializer);
        return hashMap;
    }
}

四、注入消费者

@Configuration
public class ConsumerConfiguration {

    @Value("${spring.kafka.consumer.enable-auto-commit}")
    private boolean autoCommit;
    @Value("${spring.kafka.consumer.auto-commit-interval}")
    private String interval;
    @Value("${spring.kafka.consumer.auto-offset-reset}")
    private String rest;
    @Value("${spring.kafka.consumer.key-deserializer}")
    private String keyDeserializer;
    @Value("${spring.kafka.consumer.value-deserializer}")
    private String valueDeserializer;
    @Value("${spring.kafka.bootstrap-servers}")
    private String server;


    @Bean("userConsumer")
    public  KafkaConsumer getUserConsumer(){
        List topic=new ArrayList();
        topic.add("topic.people");
        KafkaConsumer userConsumner=new KafkaConsumer(getConfig());
        userConsumner.subscribe(topic);
        return  userConsumner;
    }


    public ConsumerFactory getFaactory() {
        return new DefaultKafkaConsumerFactory(getConfig());
    }

    public Map getConfig() {
        Map hashMap = new HashMap();
        hashMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, server);
        hashMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, rest);
        hashMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, interval);
        hashMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, autoCommit);
        hashMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializer);
        hashMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer);
        hashMap.put(ConsumerConfig.GROUP_ID_CONFIG,"2");
        return hashMap;
    }
}

五、发送消息和消费消息

@RestController
public class KafkaController {

    @Resource
    KafkaTemplate peopleTemplate;

    @Resource
    KafkaConsumer userConsumer;


    @RequestMapping("/send")
    public  String sendMessage(){

        People people = new People();
        people.setAddress("北京通州");
        people.setAge(24);
        people.setName("小白");
        people.setSex(1);
        String jsonString = JSON.toJSONString(people);
        peopleTemplate.sendDefault(jsonString);


        return  "发送成功";
    }

    @RequestMapping("getmesssage")
    public  String getMessage(){

        ConsumerRecords message= userConsumer.poll(100);

        for(ConsumerRecord msg:message){
            System.out.println(msg.value());
        }


        return  "获取消息";
    }
}

结果

Spring Boot系列 八、集成Kafka_第1张图片

 

 本文简单粗暴,爱看不看

你可能感兴趣的:(Spring Boot系列 八、集成Kafka)