当前位置:   article > 正文

Kafka:springboot集成kafka收发消息_srpingboot整合kafka发送接收消息

srpingboot整合kafka发送接收消息

kafka环境搭建参考Kafka:安装和配置_moreCalm的博客-CSDN博客

1、springboot中引入kafka依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- kafkfa -->
  7. <dependency>
  8. <groupId>org.springframework.kafka</groupId>
  9. <artifactId>spring-kafka</artifactId>
  10. <exclusions>
  11. <exclusion>
  12. <groupId>org.apache.kafka</groupId>
  13. <artifactId>kafka-clients</artifactId>
  14. </exclusion>
  15. </exclusions>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.kafka</groupId>
  19. <artifactId>kafka-clients</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.alibaba</groupId>
  23. <artifactId>fastjson</artifactId>
  24. </dependency>
  25. </dependencies>

2、配置application.yml

  1. server:
  2. port: 9991
  3. spring:
  4. application:
  5. name: kafka-demo
  6. kafka:
  7. bootstrap-servers: 192.168.200.130:9092
  8. producer:
  9. retries: 10
  10. key-serializer: org.apache.kafka.common.serialization.StringSerializer
  11. value-serializer: org.apache.kafka.common.serialization.StringSerializer
  12. consumer:
  13. group-id: ${spring.application.name}-test
  14. key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
  15. value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

传递String类型的消息

3、controller实现消息发送接口

  1. package com.heima.kafkademo.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.kafka.core.KafkaTemplate;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class HelloController {
  8. @Autowired
  9. KafkaTemplate<String,String> kafkaTemplate;
  10. @GetMapping("/hello")
  11. public String addMessage(){
  12. kafkaTemplate.send("lakers-topic","湖人总冠军!");
  13. return "ok";
  14. }
  15. }

4、component中实现接收类HelloListener 

  1. package com.heima.kafkademo.component;
  2. import org.springframework.kafka.annotation.KafkaListener;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class HelloListener {
  6. @KafkaListener(topics = "lakers-topic")
  7. public void onMessage(String msg){
  8. System.out.println(msg);
  9. }
  10. }

5、测试

浏览器访问该接口并查看控制台

        接收成功

 

传递对象类型的消息

思路:在传递消息时,将对象转为json字符串,在接收时再解析

1、controller实现发送

  1. package com.heima.kafkademo.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.heima.kafkademo.model.User;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.kafka.core.KafkaTemplate;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. public class HelloController {
  10. @Autowired
  11. KafkaTemplate<String,String> kafkaTemplate;
  12. @GetMapping("/hello")
  13. public String addMessage(){
  14. User user = new User();
  15. user.setName("勒布朗");
  16. user.setAge(37);
  17. user.setGender("男");
  18. user.setJob("NBA球员");
  19. kafkaTemplate.send("lakers-topic", JSON.toJSONString(user));
  20. return "ok";
  21. }
  22. }

2、component实现接收类

  1. package com.heima.kafkademo.component;
  2. import com.alibaba.fastjson.JSON;
  3. import com.heima.kafkademo.model.User;
  4. import org.springframework.kafka.annotation.KafkaListener;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class HelloListener {
  8. @KafkaListener(topics = "lakers-topic")
  9. public void onMessage(String msg){
  10. System.out.println(JSON.parseObject(msg, User.class));
  11. }
  12. }

3、打印测试结果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/726168
推荐阅读
相关标签
  

闽ICP备14008679号