1、kafka控制台创建公网类型实例
2、创建SpringBoot项目集成阿里云Kafka
3、发送接收测试
1.1 Kafka控制台创建实例
1.2 获取认证参数
二、创建SpringBoot项目集成阿里云Kafka2.1 创建Spring Boot(2.5.2)项目
2.2 依赖
properties java.version 1.8 /java.version /properties dependencies dependency groupId org.springframework.boot /groupId artifactId spring-boot-starter-web /artifactId /dependency dependency groupId org.springframework.kafka /groupId artifactId spring-kafka /artifactId /dependency dependency groupId org.springframework.boot /groupId artifactId spring-boot-starter-test /artifactId scope test /scope /dependency dependency groupId org.springframework.kafka /groupId artifactId spring-kafka-test /artifactId scope test /scope /dependency /dependencies
2.3 Sender.class
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @Component public class Sender { @Autowired private KafkaTemplate String, String template; public void send(String msg) { this.template.sendDefault("my_msg", msg); System.out.println("send message:" + msg); }
2.4 Receiver.class
import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class Receiver { @KafkaListener(topics = { "taro_topic" }) // 参数配置要监听的Topic public void receiveMessage(ConsumerRecord String, String record) { System.out.println("Receive Message"); System.out.println("【*** Message: ***】key = " + record.key() + "、value = " + record.value()); }
2.5 KafkaController.class
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class KafkaController { @Autowired private Sender sender; @PostMapping("/send/{msg}") // 发送消息测试,注意此处为Post public String send(@PathVariable("msg") String msg) { sender.send(msg); return msg; }
2.6 application.yml
spring: kafka: template: default-topic: topic bootstrap-servers: SSL接入点 jaas: enabled: true loginModule: org.apache.kafka.common.security.plain.PlainLoginModule options: username: 用户名 password: 密码 consumer: ssl: truststoreLocation: file:/kafka.client.truststore.jks properties: sasl.mechanism: PLAIN security.protocol: SASL_SSL ssl.endpoint.identification.algorithm: group-id: group max-poll-records: 2 producer: ssl: truststoreLocation: file:/kafka.client.truststore.jks retries: 3 acks: 1 compression-type: lz4 buffer-memory: 33554432 batch-size: 51200 properties: send.buffer.bytes: 262144 sasl.mechanism: PLAIN security.protocol: SASL_SSL ssl.endpoint.identification.algorithm:kafka.client.truststore.jks 下载地址,证书下载后直接放在C盘根目录下。
2.7 项目结构
三、发送接收测试3.1 启动项目,使用PostMan发送Post请求
3.2 项目日志
3.3 控制台消息监控查看
更多参考想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://...
本文转载自公众号读芯术(ID:AI_Discovery)。 这一刻你正在应对什么挑战?这位前...
TIOBE 公布了 2021 年 3 月的编程语言排行榜。 本月 TIOBE 指数没有什么有趣的变...
前言 统计科学家使用交互式的统计工具(比如R)来回答数据中的问题,获得全景的认...
本文转载自微信公众号「bugstack虫洞栈」,作者小傅哥 。转载本文请联系bugstack...
溢价 域名 的续费价格如何?通常来说,因为溢价域名的价值高于普通域名,所以溢...
基本介绍 给定 n 个权值作为 n 个叶子节点,构造一颗二叉树,若该树的带权路径长...
背景 我们知道 如果在Kubernetes中支持GPU设备调度 需要做如下的工作 节点上安装...
在Python开发过程中,我们难免会遇到多重条件判断的情况的情况,此时除了用很多...
近几年,互联网行业蓬勃发展,在互联网浪潮的冲击下,互联网创业已成为一种比较...