重生之 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 小时前
hadoop集群启动没有datanode解决
大数据·hadoop·分布式
UCoding1 小时前
我们来学zookeeper -- 集群搭建
分布式·zookeeper
ghost1432 小时前
C#学习第27天:时间和日期的处理
开发语言·学习·c#
小马哥编程2 小时前
【ISAQB大纲解读】Kafka消息总线被视为“自下而上设计”?
分布式·kafka·系统架构·linq
shangjg32 小时前
Kafka 的 ISR 机制深度解析:保障数据可靠性的核心防线
java·后端·kafka
m0_634448893 小时前
从上下文学习和微调看语言模型的泛化:一项对照研究
学习·算法·语言模型
大数据003 小时前
Docker慢慢学
mysql·docker·kafka·n8n
hello kitty w4 小时前
Python学习(6) ----- Python2和Python3的区别
开发语言·python·学习
虾球xz5 小时前
CppCon 2014 学习: An Overview of C++11/14
开发语言·c++·学习
viperrrrrrrrrr75 小时前
大数据学习(128)-数据分析实例
大数据·学习·数据分析