spring boot学习第八篇:kafka监听消费

为了实现监听器功能

pom.xml文件内容如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hmblogs
    hmblogs
    0.0.1-SNAPSHOT
    hmblogs
    hmblogs
    
        8
        1.2.8
        1.16
    
    
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.version}
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.3.1
        

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

        
            org.projectlombok
            lombok
            true
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.bgee.log4jdbc-log4j2
            log4jdbc-log4j2-jdbc4.1
            ${log4jdbc.version}
        

        
            com.alibaba
            fastjson
            1.2.9
        

        
            redis.clients
            jedis
        

        
            org.apache.kafka
            kafka-clients
            2.8.0
        

        
            org.springframework.kafka
            spring-kafka
            2.7.2
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 application.yml文件内容如下:

server:
  port: 8081
  servlet.context-path: /

#配置数据源
spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
      url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
      username: ${DB_USER:root}
      password: ${DB_PWD:123456}
  redis:
    host: localhost
    port: 6379
    password: heming
    database: 10

logback.xml文件内容如下:



    
    
    
    
    logback
    
    

    
        
            ${Console_Pattern}
            
            UTF-8
        
    

    
    
        ${log.path}/hmblogs.log
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%logger{50}] - %msg%n
            UTF-8
        
        
        
            
            ${log.path}/hmblogs/log-hmblogs-%d{yyyy-MM-dd}.%i.log
            
                100MB
            
            
            15
        
        
        
            Info
            ACCEPT
            DENY
        
    

    
    
        
        
    
    
    
        
        
    
    
    
    
    
        
        
    

BackendApplication.java文件内容如下:

package com.hmblogs.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }

}

然后添加了kafkaConsumerListenerExample.java文件

package com.hmblogs.backend.util;

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

import java.util.Optional;

/**
 *
 * @description:  kafka 消费者
 * @copyright: @Copyright (c) 2022
 * @company: hmblogs
 * @author: heming
 * @version: 1.0.0
 * @createTime: 2024-01-18 8:31
 */
@Component
@Slf4j
public class kafkaConsumerListenerExample {

    @KafkaListener(topics = "test", groupId = "0")
    public void consume(ConsumerRecord record) {
        Optional value = Optional.ofNullable(record.value());
        // 进行消息处理逻辑
        log.info("print message: " + value);
    }
}


发到服务器上,启动hmblogs报错,截图如下:

spring boot学习第八篇:kafka监听消费_第1张图片

Caused by: java.lang.TypeNotPresentException: Type org.springframework.kafka.listener.CommonErrorHandler not present

java.lang.ClassNotFoundException: org.springframework.kafka.listener.CommonErrorHandler

网上搜索资料,大部分讲的都是包冲突,在本地启动也是报这样的错,如下所示:

spring boot学习第八篇:kafka监听消费_第2张图片

spring boot学习第八篇:kafka监听消费_第3张图片 

你可能感兴趣的:(Spring,Boot,spring,boot,学习,kafka)