SpringBoot集成Kafka

Kafka 介绍

Kafka 是一个分布式的基于发布/订阅模式的消息队列,它最大的特性是可以实时的处理大数据量以满足各种需求场景。其特点如下:

  • 高吞吐量、低延迟:Kafka 每秒可以处理几十万条消息,它的延迟最低只有几毫秒。
  • 可扩展性:kafka 集群支持热扩展
  • 持久性、可靠性:消息被持久化到本地磁盘,并且支持数据备份防止数据丢失
  • 容错性:允许集群中节点失败
  • 高并发:支持数千个客户端同时读写

SpringBoot集成Kafka

Springboot集成Kafka步骤如下:

1、添加依赖pom.xml

核心依赖:spring-kafka

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/>
    </parent>

    <groupId>com.gingko</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>

    <dependencies>
        <!-- 依赖spring-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot默认数据源(HikariCP)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- 整合MyBatis -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
           <version>3.4.2</version>
        </dependency>
        <!-- SpringBoot 的AOP实现 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- junit 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- SpringBoot 健康检查 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- jwt token -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.10.7</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.10.7</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.10.7</version>
            <scope>runtime</scope>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
        <!--kafka -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、Kafka配置 application.yml

3、编写 kafka发送消息工具类 KafkaProducerUtils

java 复制代码
package com.gingko.common;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * kafka发送消息工具类
 */
@Component
public class KafkaProducerUtils {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 发送消息
     * @param topic 消息主题
     * @param msgBody 消息体
     */
    public void sendMessage(String topic,String msgBody) {
        this.kafkaTemplate.send(topic,msgBody);
    }
}

4、测试类

sendMsg 方法发送请求信息到kafka,consumerMsg 监听kafka主题并消费消息

java 复制代码
package com.gingko.controller;

import com.alibaba.fastjson.JSON;
import com.gingko.common.KafkaProducerUtils;
import com.gingko.entity.Student;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("kafka")
public class KafkaController {

    @Resource
    private KafkaProducerUtils kafkaProducerUtils;

    @PostMapping("/sendMsg")
    public void sendMsg() {
        Student student = new Student("1","张三",18,"1");
        String studentStr = JSON.toJSONString(student);
        this.kafkaProducerUtils.sendMessage("helloTopic", studentStr);
        System.out.println("生产消息:" + studentStr);
    }

    @KafkaListener(topics = "helloTopic")
    public void consumerMsg(String msg) {
        Student student = JSON.parseObject(msg,Student.class);
        System.out.println("消费此消息:" + student);
    }
}

5、运行结果

从运行结果可以看出,发送kafka的学生信息被注解@KafkaListener标识的方法consumerMsg消费了。

相关推荐
Mephisto.java6 分钟前
【大数据学习 | Spark】Spark的改变分区的算子
大数据·elasticsearch·oracle·spark·kafka·memcache
编程重生之路19 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
politeboy29 分钟前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
世间万物皆对象7 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
KevinAha7 小时前
Kafka 3.5 源码导读
kafka
求积分不加C7 小时前
-bash: ./kafka-topics.sh: No such file or directory--解决方案
分布式·kafka
nathan05297 小时前
javaer快速上手kafka
分布式·kafka
qq_17448285758 小时前
springboot基于微信小程序的旧衣回收系统的设计与实现
spring boot·后端·微信小程序
代码小鑫10 小时前
A043-基于Spring Boot的秒杀系统设计与实现
java·开发语言·数据库·spring boot·后端·spring·毕业设计
真心喜欢你吖10 小时前
SpringBoot与MongoDB深度整合及应用案例
java·spring boot·后端·mongodb·spring