SpringBoot整合kafka

①引入maven依赖



         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">
    4.0.0

    test
    test
    1.0-SNAPSHOT

    
        1.8
        UTF-8
        UTF-8
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
         
    
    
        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.apache.kafka
            kafka-streams
        
        
            org.springframework.kafka
            spring-kafka
        
    


②application.yml配置

server:
  port: 9012
  servlet:
    context-path: /


spring:
  kafka:
    bootstrap-servers: 192.168.56.104:9092
    producer:
      # 每次批量发送消息的数量
      batch-size: 16
      # 缓存容量
      buffer-memory: 33554432
      #设置大于0的值,则客户端会将发送失败的记录重新发送
      retries: 0
      # 指定消息key和消息体的编解码方式 UTF-8
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

kafka:
  #订阅的主题
  topic: topic-test-01
  #主题消费分组
  group: group-test-01

③消息发布KafkaProducer

package com.test;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * @Description //TODO
 * @Date 2020/2/26 17:11
 * @Author GaoX
 */
@Component
@AllArgsConstructor
@Slf4j
public class KafkaProducer {

    private final KafkaTemplate kafkaTemplate;

    public void data(String data){

        try {
            kafkaTemplate.send("topic-test-01", "这是测试的数据==>"+data );
        }catch (Exception e){
            e.printStackTrace();
            log.error("出错!!!!!!!!!!!");
        }

    }

}

④消息消费KafkaConsumer

package com.test;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @Description //TODO
 * @Date 2020/2/26 17:13
 * @Author GaoX
 */
@Component
@AllArgsConstructor
@Slf4j
public class KafkaConsumer {

    @KafkaListener(topics = "${kafka.topic}", groupId = "${kafka.group}")
    public void data(ConsumerRecord consumerRecord) {
        Object value = consumerRecord.value();
        if (log.isInfoEnabled()) {
            log.info("offset {}, value {}", consumerRecord.offset(), consumerRecord.value());
        }
        if (null == value) {
            log.error("kafka消费数据为空");
        }
        log.info((String) value);
    }

}

⑤调用消息发布测试

package com.test;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description //TODO
 * @Date 2020/2/26 16:58
 * @Author GaoX
 */
@RestController
@AllArgsConstructor
public class TestController {

    private final KafkaProducer kafkaProducer;

    @GetMapping("/test")
   public String aVoid(String str){
        try {
            kafkaProducer.data(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "成功==============================";
   }

}

浏览器访问

SpringBoot整合kafka_第1张图片

 

 控制台打印

 

你可能感兴趣的:(SpringBoot整合kafka)