springboot kafka集成

背景

spring对Kafka有原生的支持,有对应的KafkaTemplate
kafka官网:http://kafka.apache.org/

对应kafka的安装,本篇就不再叙述。
demo源码:https://github.com/jc0803kevin/Queue/tree/master/springboot-kafka

依赖

<dependency>
   <groupId>org.springframework.kafka</groupId>
   <artifactId>spring-kafka</artifactId>
   <version>2.2.8.RELEASE</version>
</dependency>

项目结构:
在这里插入图片描述

生产者

@RestController
@RequestMapping("/kafka")
public class TestKafkaProducerController {

    @Autowired(required = false )
    private KafkaTemplate<String, String> kafkaTemplate;

    @RequestMapping("/send")
    public String send(String message){
        String temp = String.valueOf(System.currentTimeMillis());
        message = message + temp;
        kafkaTemplate.send("test_topic", message);
        kafkaTemplate.send("test_topic2", message);
        System.out.println("producer send message-->" + message);
        return message;
    }

}

消费者

消费者只需要实现监听,并且指定主题,若有多个customer不同group的需要,只需要配置不同的连接工厂即可
参考:https://blog.csdn.net/caijiapeng0102/article/details/80765923

@Component
public class RawDataConsumerListener {

    @KafkaListener(topics = {"test_topic", "test_topic2"})
    public void onMessage(String message){
        System.out.println("test_topic consumer thread name->"+  Thread.currentThread().getName());
        System.out.println("test_topic consumer receive message->"+  message);
    }

    @KafkaListener(topics = {"test_topic2"})
    public void listence(ConsumerRecord<String, String> context){
        //ConsumerRecord(topic = test_topic2, partition = 0, offset = 0, CreateTime = 1569395997850, serialized key size = -1,
        //        serialized value size = 18, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = kevin1569395997499)

        System.out.println("test_topic2 consumer thread name->"+  Thread.currentThread().getName());
        System.out.println("test_topic2 consumer topic name->"+  context.topic());
        System.out.println("test_topic2 consumer receive message->"+  context);
    }

    /**
     * @Author kevin
     * @Description 同一个主题 配置多个消费者 只会消费一次
     * @Date Created on 2019/9/25 15:39
     * @param
     * @return
     */
    @KafkaListener(topics = {"test_topic2"})
    public void receive(ConsumerRecord<String, String> context){

        System.out.println("receive test_topic2 consumer thread name->"+  Thread.currentThread().getName());
        System.out.println("receive test_topic2 consumer topic name->"+  context.topic());
        System.out.println("receive test_topic2 consumer receive message->"+  context);
    }

}

配置文件

server.port=8082
spring.kafka.consumer.group-id=test
#kafka 集群服务地址 host:post 形式 逗号分隔
spring.kafka.consumer.bootstrap-servers=127.0.0.1:9092,127.0.0.1:9093

有了spring的支持,项目集成Kafka还是比较简单的,只需要对应配置一下。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jc0803kevin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值