Kafka-入门及简单示例

启动与简单示例

java 复制代码
# 命令行1
#开启Zookeeper
E:\>cd E:\kafka_2.13-3.6.0

E:\kafka_2.13-3.6.0>bin\windows\zookeeper-server-start.bat config\zookeeper.properties
# 命令行2
#开启Kafka
E:\>cd E:\kafka_2.13-3.6.0
E:\kafka_2.13-3.6.0>bin\windows\kafka-server-start.bat config\server.properties
# 命令行3
#创建主题
E:\kafka_2.13-3.6.0\bin\windows>kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
#往主题发送消息 生产者命令
E:\kafka_2.13-3.6.0\bin\windows>kafka-console-producer.bat --broker-list localhost:9092 --topic test
>hello
>woe
#查看发送的消息 消费者命令
#命令行4
E:\kafka_2.13-3.6.0\bin\windows>kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning

结果

Spring整合Kafka--简单示例

pom.xml

java 复制代码
		<dependency>
			<groupId>org.springframework.kafka</groupId>
			<artifactId>spring-kafka</artifactId>
		</dependency>

application.properties

java 复制代码
#Kafka
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=community-consumer-group
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto-commit-interval=3000

一个生产者 一个消费者

生产者在某些事件时触发消息产生

消费者根据topic监听事件 如果有生产者产生消息 自动进行消费

java 复制代码
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class KafkaTest {
    @Autowired
    private KafkaProducer kafkaProducer;
    @Test
    public void testKafka(){
        kafkaProducer.sendMessage("test","你好啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
        kafkaProducer.sendMessage("test","在吗啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
        try {
            Thread.sleep(1000*10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
//生产者
@Component
class KafkaProducer{
    @Autowired
    private KafkaTemplate kafkaTemplate;
    public void sendMessage(String topic,String content){
        kafkaTemplate.send(topic,content);
    }
}
//消费者
@Component
class KafkaConsumer{
    @KafkaListener(topics = {"test"})
    public void handleMessage(ConsumerRecord record){
        System.out.println(record.value());
    }
}
相关推荐
面向Google编程8 小时前
从零学习Kafka:消费者组重平衡
大数据·kafka·负载均衡
还在忙碌的吴小二8 小时前
XXL-JOB - 分布式任务调度平台新手入门指南
分布式
Jackeyzhe8 小时前
从零学习Kafka:消费者组重平衡
kafka
ClouGence15 小时前
TiCDC 够用吗?聊聊 TiDB 同步的几个关键问题
数据库·分布式·后端
Mr_pyx16 小时前
分布式事务解决方案:6个生活中的小故事
分布式·生活
我只想困告16 小时前
day01-RabbitMQ_2026-05-13
分布式·rabbitmq
cheems952717 小时前
[RabbitMQ] RabbitMQ 工作流程全解析
分布式·rabbitmq
敖正炀19 小时前
读写分离与数据库中间件选型
分布式
Mahir0820 小时前
Redis 分布式锁与 Redisson 深度解析:从原生实现到工业级解决方案
数据库·redis·分布式·缓存·面试
敖正炀20 小时前
分布式事务监控与手动恢复平台设计
分布式