Kafka 下载安装及使用总结

1. 下载安装

官网下载地址:Apache Kafka

下载对应的文件

上传到服务器上,解压

bash 复制代码
tar -xzf kafka_2.13-3.7.0.tgz

目录结果如下

bash 复制代码
├── bin
│   └── windows
├── config
│   └── kraft
├── libs
├── licenses
└── site-docs

官方文档:Apache Kafka

kafka 有两种启动方式,ZooKeeper 和 KRaft,这里采用 KRaft 的方式,使用 kraft 目录下的配置文件

bash 复制代码
config
├── connect-console-sink.properties
├── connect-console-source.properties
├── connect-distributed.properties
├── connect-file-sink.properties
├── connect-file-source.properties
├── connect-log4j.properties
├── connect-mirror-maker.properties
├── connect-standalone.properties
├── consumer.properties
├── kraft
│   ├── broker.properties
│   ├── controller.properties
│   └── server.properties
├── log4j.properties
├── producer.properties
├── server.properties
├── tools-log4j.properties
├── trogdor.conf
└── zookeeper.properties

修改 server.properties 文件

  • process.roles:KRaft 模式角色
    • broker
    • controller
    • broker,controller
  • node.id:节点 ID,需要为每个节点分配一个唯一的 ID
  • controller.quorum.voters:Controller 的投票者配置
  • log.dirs:日志目录

初始化集群,先生成一个 UUID

bash 复制代码
./bin/kafka-storage.sh random-uuid

再执行命令,使用生成的 UUID 完成集群初始化

bash 复制代码
./bin/kafka-storage.sh format -t thCDFveGRleJro7zTaOOGA -c ./config/kraft/server.properties

然后启动 Kafka

bash 复制代码
./bin/kafka-server-start.sh -daemon ./config/kraft/server.properties

2. Spring Boot 集成

2.1 引入依赖

xml 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>3.1.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.kafka</groupId>
                    <artifactId>kafka-clients</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>3.7.0</version>
        </dependency>
</dependencies>

2.2 配置文件

yaml 复制代码
spring:
  kafka:
    bootstrap-servers: 127.0.0.1:9092
    consumer:
      group-id: fable-group

2.3 生产者

使用 KafkaTemplate 实现消息的生产

java 复制代码
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.RestController;

@RestController
public class ProducerController {

    private final KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    public ProducerController(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    @GetMapping("/send/message")
    public String sendMessage() {
        kafkaTemplate.send("test", "Hello, Kafka!");
        return "Message sent successfully";
    }
}

2.4 消费者

添加 @KafkaListener 注解,监听消息

java 复制代码
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class ConsumerService {

    @KafkaListener(topics = "test", groupId = "fable-group")
    public void listen(ConsumerRecord<String, String> consumerRecord) {
        System.out.println("Received message: " + consumerRecord.value());
    }
}

2.5 启动类

添加 @EnableKafka 注解

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.EnableKafka;

@SpringBootApplication
@EnableKafka
public class FableApplication {
    public static void main(String[] args) {
        SpringApplication.run(FableApplication.class, args);
    }
}

2.6 测试

访问 /send/message 接口,可以看到控制台打印出接收到的消息

bash 复制代码
Received message: Hello, Kafka!
相关推荐
深圳蔓延科技10 小时前
Kafka的高性能之路
后端·kafka
努力的小郑16 小时前
从一次分表实践谈起:我们真的需要复杂的分布式ID吗?
分布式·后端·面试
AAA修煤气灶刘哥1 天前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
阿里云云原生1 天前
嘉银科技基于阿里云 Kafka Serverless 提升业务弹性能力,节省成本超过 20%
kafka·serverless
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
程序消消乐2 天前
Kafka 入门指南:从 0 到 1 构建你的 Kafka 知识基础入门体系
分布式·kafka
智能化咨询2 天前
Kafka架构:构建高吞吐量分布式消息系统的艺术——进阶优化与行业实践
分布式·架构·kafka
Chasing__Dreams2 天前
kafka--基础知识点--5.2--最多一次、至少一次、精确一次
分布式·kafka
在未来等你2 天前
Elasticsearch面试精讲 Day 17:查询性能调优实践
大数据·分布式·elasticsearch·搜索引擎·面试