springboot整合kafka应用

简介: 1、kafka在消息传递的使用非常普遍,相对于activemq来说kafka的分布式管理和使用更加灵活。2、activemq的搭建和使用可以参考:  activemq搭建和springmvc的整合:http://www.

1、kafka在消息传递的使用非常普遍,相对于activemq来说kafka的分布式管理和使用更加灵活。

2、activemq的搭建和使用可以参考:

  activemq搭建和springmvc的整合:http://www.cnblogs.com/ll409546297/p/6898155.html

  springboot和springboot的整合:http://www.cnblogs.com/ll409546297/p/7805072.html

3、kafka的搭建:

  http://www.cnblogs.com/ll409546297/p/7810302.html

4、下面介绍kafka和springboot的整合

  1)目录结构

  

  2)需要的基础包:pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.troy</groupId>
    <artifactId>springbootkafka</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>
    </dependencies>

</project>

  3)基本配置:application.yml

server:
  port: 8090
spring:
  kafka:
    bootstrap-servers: 192.168.5.10:9092 #kafka的访问地址,多个用","隔开
    consumer:
      enable-auto-commit: true
      group-id: kafka #群组ID
      auto-offset-reset: earliest #启东时接收没有接收到的数据

  如果存在集群的话,配置如下

server:
  port: 8090
spring:
  kafka:
    consumer:
      enable-auto-commit: true
      group-id: kafka
      auto-offset-reset: earliest
      bootstrap-servers: 192.168.5.11:9092
    producer:
      bootstrap-servers: 192.168.5.10:9092

   4)生产者:KafkaProducer.class

@Component //这个必须加入容器不然,不会执行
@EnableScheduling //这里是为了测试加入定时调度
public class KafkaProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Scheduled(cron = "00/30 * * * * ?")
    public void send(){
        System.out.println("send data");
        kafkaTemplate.send("topic","kafka data");
        //发送方式很多种可以自己研究一下
    }
}

  5)消费者:KafkaCustomer.class

@Component //同样这里是必须的
public class KafkaCustomer {

    @KafkaListener(topics = {"topic"})
    public void receive(String message){
        System.out.println("topic========topic");
        System.out.println(message);
    }
}

  6)测试结果:

 

相关文章
|
1月前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
3月前
|
存储 Java Maven
QR码应用实战:Spring Boot与ZXing完美结合
QR码应用实战:Spring Boot与ZXing完美结合
38 0
|
1月前
|
消息中间件 Java Kafka
Spring整合kafka
Spring整合kafka
|
3月前
|
消息中间件 存储 监控
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
36 1
|
22天前
|
消息中间件 分布式计算 监控
Python面试:消息队列(RabbitMQ、Kafka)基础知识与应用
【4月更文挑战第18天】本文探讨了Python面试中RabbitMQ与Kafka的常见问题和易错点,包括两者的基础概念、特性对比、Python客户端使用、消息队列应用场景及消息可靠性保证。重点讲解了消息丢失与重复的避免策略,并提供了实战代码示例,帮助读者提升在分布式系统中使用消息队列的能力。
34 2
|
2月前
|
Kubernetes Java 容器
部署 Spring Boot 应用到 K8S 教程
部署 Spring Boot 应用到 K8S 教程
66 0
|
2天前
|
Java 应用服务中间件 测试技术
深入探索Spring Boot Web应用源码及实战应用
【5月更文挑战第11天】本文将详细解析Spring Boot Web应用的源码架构,并通过一个实际案例,展示如何构建一个基于Spring Boot的Web应用。本文旨在帮助读者更好地理解Spring Boot的内部工作机制,以及如何利用这些机制优化自己的Web应用开发。
14 3
|
6天前
|
消息中间件 Java Kafka
spring kafka的问题集锦
spring kafka的问题集锦
17 0
|
7天前
|
消息中间件 存储 传感器
Kafka消息队列原理及应用详解
【5月更文挑战第6天】Apache Kafka是高性能的分布式消息队列,常用于实时数据管道和流应用。它提供高性能、持久化、分布式和可伸缩的消息处理,支持解耦、异步通信和流量控制。Kafka的核心概念包括Broker、Topic、Partition、Producer、Consumer和Consumer Group。其特点是高吞吐、低延迟、数据持久化、分布式架构和容错性。常见应用包括实时数据流处理、日志收集、消息传递和系统间数据交换。
|
2月前
|
Prometheus 监控 Cloud Native
Spring Boot 应用可视化监控
Spring Boot 应用可视化监控
24 0

热门文章

最新文章