重生之 SpringBoot3 入门保姆级学习(24、场景整合 kafka 消息发送服务)

重生之 SpringBoot3 入门保姆级学习(24、场景整合 kafka 消息发送服务)

    • [6.4 消息发送服务](#6.4 消息发送服务)

6.4 消息发送服务


  • 创建成功
  • application.properties 配置文件(注意需要改成你的 kafka 地址,也就是浏览器访问的主机 IP )
yaml 复制代码
spring.kafka.bootstrap-servers=192.168.1.4:9092
  • 测试代码
java 复制代码
package com.zhong.message;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.StopWatch;

import java.util.concurrent.CompletableFuture;

@SpringBootTest
class Boot309MessageApplicationTests {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Test
    void contextLoads() {
        // 秒表功能
        StopWatch stopWatch = new StopWatch();

        // 异步处理等待处理完
        CompletableFuture[] futures = new CompletableFuture[10000];

        // 开始计时
        stopWatch.start();
        for (int i = 0; i < 10000; i++) {
            // 异步的
            CompletableFuture future = kafkaTemplate.send("newshh", "name", "小钟");
            futures[i] = future;
        }
        CompletableFuture.allOf(futures).join();
        // 停止计时
        stopWatch.stop();
        // 统计计时从开始到结束用了多少毫秒
        long millis = stopWatch.getTotalTimeMillis();
        System.out.println("10000个消息发送完成!ms时间:" + millis);
    }

}
  • 运行测试
  • 发送成功
  • 新建 Person 类
java 复制代码
package com.zhong.message.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * @ClassName : Person
 * @Description :
 * @Author : zhx
 * @Date: 2024-06-14 16:11
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
  • 新建测试代码
java 复制代码
package com.zhong.message;

import com.zhong.message.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.StopWatch;

import java.util.concurrent.CompletableFuture;

@SpringBootTest
class Boot309MessageApplicationTests {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Test
    void send() {
        CompletableFuture future = kafkaTemplate.send("newsPerson", "person", new Person(1L, "小王", 21, "testemal"));
        future.join();
        System.out.println("消息发送成功!");
    }

}
  • 如果发送出现问题请配置值的序列化规则
xml 复制代码
# 值的序列化规则
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
  • 测试成功
相关推荐
一只小菜鸡..1 小时前
南京大学 操作系统 (JYY) 学习笔记:从 UNIX 到 Linux 与庞大的应用生态
笔记·学习·unix
MartinYeung52 小时前
[论文学习]迈向自主医疗人工智能智能体:MIRA系统深度分析
人工智能·学习
熊猫钓鱼>_>3 小时前
ArkTS 装饰器总览:V1 / V2 / 通用装饰器完整学习笔记
笔记·学习·华为·交互·harmonyos·arkts·arkweb
梅头脑6 小时前
交易跨10个库、日均千万订单——选错一次分布式事务方案,加班三个月重写
java·分布式
茯苓gao7 小时前
嵌入式开发笔记:CAN与CAN FD完全指南——从帧格式差异到MCU选型实战
网络·笔记·stm32·嵌入式硬件·学习·汽车
木木_王7 小时前
嵌入式学习 | STM32 裸板驱动开发(Day06) | DHT11温湿度传感器超详解(单总线时序+全套驱动代码+智能温湿度报警项目)
驱动开发·stm32·学习
FellAveal8 小时前
【Go语言入门学习笔记】Part19.速率控制、原子计数器、互斥锁(Mutex)、状态协程
笔记·学习·golang
你怎么知道我是队长8 小时前
经典蓝牙(Bluetooth Classic / BR/EDR)学习计划
学习
极地野狼 音乐哔哔8 小时前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
学习·microsoft
霸道流氓气质9 小时前
分布式系统中接口时序不确定性处理
java·开发语言·分布式