成都 企业网站建设公司,如何查网站的百度快照,网站设计深圳网站建设公司,策划是做什么的文章目录 1、生产者服务搭建1.1、引入spring-kafka依赖1.2、application.yml配置----v1版1.3、使用Java代码创建主题分区副本1.4、发送消息 1、生产者服务搭建
1.1、引入spring-kafka依赖
?xml version1.0 encodingUTF-8?
project xml… 文章目录 1、生产者服务搭建1.1、引入spring-kafka依赖1.2、application.yml配置----v1版1.3、使用Java代码创建主题分区副本1.4、发送消息 1、生产者服务搭建
1.1、引入spring-kafka依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.0.5/versionrelativePath/ !-- lookup parent from repository --/parent!-- Generated by https://start.springboot.io --!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 https://springdoc.cn --groupIdcom.atguigu.kafka/groupIdartifactIdkafka-producer/artifactIdversion0.0.1-SNAPSHOT/versionnamekafka-producer/namedescriptionkafka-producer/descriptionpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.kafka/groupIdartifactIdspring-kafka/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project
1.2、application.yml配置----v1版
server:port: 8110# v1
spring:kafka:bootstrap-servers: 192.168.74.148:9095,192.168.74.148:9096,192.168.74.148:9097producer: # producer 生产者retries: 0 # 重试次数 0表示不重试acks: -1 # 应答级别:多少个分区副本备份完成时向生产者发送ack确认(可选0、1、-1/all)batch-size: 16384 # 批次大小 单位bytebuffer-memory: 33554432 # 生产者缓冲区大小 单位bytekey-serializer: org.apache.kafka.common.serialization.StringSerializer # key的序列化器value-serializer: org.apache.kafka.common.serialization.StringSerializer # value的序列化器
1.3、使用Java代码创建主题分区副本
package com.atguigu.kafka.config;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.stereotype.Component;
Component
public class KafkaTopicConfig {Beanpublic NewTopic myTopic1() {//相同名称的主题 只会创建一次后面创建的主题名称相同配置不同可以做增量更新分区、副本数return TopicBuilder.name(my_topic1)//主题名称.partitions(3)//主题分区.replicas(3)//主题分区副本数.build();//创建}
} 1.4、发送消息
package com.atguigu.kafka.producer;import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;SpringBootTest
class KafkaProducerApplicationTests {//装配kafka模板类 springboot启动时会自动根据配置文初始化kafka模板类对象注入到容器中ResourceKafkaTemplate kafkaTemplate;Testvoid contextLoads() {kafkaTemplate.send(my_topic1, spring-kafka);}
} [[{partition: 0,offset: 0,msg: spring-kafka,timespan: 1717483674110,date: 2024-06-04 06:47:54}]
]