当前位置: 首页 > news >正文

网站建设预付款企业网站优化

网站建设预付款,企业网站优化,南宁网站建设哪里有,哪里有好包装设计公司欢迎来到啾啾的博客🐱。 记录学习点滴。分享工作思考和实用技巧,偶尔也分享一些杂谈💬。 有很多很多不足的地方,欢迎评论交流,感谢您的阅读和评论😄。 目录 1 引言2 消息ProducerRecord2.1 分区器 1 引言 …

欢迎来到啾啾的博客🐱。
记录学习点滴。分享工作思考和实用技巧,偶尔也分享一些杂谈💬。
有很多很多不足的地方,欢迎评论交流,感谢您的阅读和评论😄。

目录

  • 1 引言
  • 2 消息ProducerRecord
    • 2.1 分区器

1 引言

在之前的Kafka篇章中,我们已经了解到Kafka Producer内部会有一个缓冲区,生产者通过批量发送消息的方式提升总体的吞吐。

批量发送在很多场景中都很常见,Kafka是如何实现的?
![[Kafka源码-批量消息-1.png]]
首先,我们需要理解消息。

2 消息ProducerRecord

Kafka消息的封装如下:

![[Kafka源码-批量消息-2.png]]

2.1 分区器

Kafka Producer可以指定消息发送到哪个Topic,也可以指定到哪个Partition。没有指定Partition时,Kafka Producer会使用分区器 Partitioner来决定消息应该到哪个partition。

可以看一下分区器 Partitioner 的方法
![[Kafka源码-批量消息-3.png]]

  • 参数如下:
    ProducerRecord<K, V> record:要发送的 Kafka 消息记录,包含主题、键、值、分区等信息。
    byte[] serializedKey:消息键的序列化字节数组。
    byte[] serializedValue:消息值的序列化字节数组。
    Cluster cluster:Kafka 集群的元数据信息,包含主题、分区等信息。

其中serializedKey和serializedValue是分别用了同一个序列化类的不同的序列化对象来做的:
![[Kafka源码-批量消息-5.png]]

![[Kafka源码-批量消息-4.png]]

有意思的是序列化方法传入的topic与headers信息在Kafka的默认序列化中是没有被使用的。
![[Kafka源码-批量消息-6.png]]

很显然,这两个参数topic和headers是为了序列化的扩展性预留的设计。
使用topic参数,可以为不同的topic准备不同的序列化策略,比如加密。

import org.apache.kafka.common.serialization.Serializer;
import java.nio.charset.StandardCharsets;
import java.util.Map;public class TopicAwareSerializer implements Serializer<String> {@Overridepublic void configure(Map<String, ?> configs, boolean isKey) {// 配置逻辑}@Overridepublic byte[] serialize(String topic, String data) {if ("sensitive-topic".equals(topic)) {// 对敏感主题的数据进行特殊处理String encryptedData = "encrypted:" + data;return encryptedData.getBytes(StandardCharsets.UTF_8);}return data.getBytes(StandardCharsets.UTF_8);}@Overridepublic void close() {// 关闭逻辑}
}

使用headers参数,比如编码格式、版本号等,也可以做一些定制化操作。

import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Serializer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;public class HeaderAwareSerializer implements Serializer<String> {@Overridepublic void configure(Map<String, ?> configs, boolean isKey) {// 配置逻辑}@Overridepublic byte[] serialize(String topic, Headers headers, String data) {Charset charset = StandardCharsets.UTF_8;if (headers.lastHeader("charset") != null) {String charsetName = new String(headers.lastHeader("charset").value(), StandardCharsets.UTF_8);charset = Charset.forName(charsetName);}return data.getBytes(charset);}@Overridepublic byte[] serialize(String topic, String data) {return serialize(topic, null, data);}@Overridepublic void close() {// 关闭逻辑}
}

需要注意的是,key|value.serializer都必须被设置为实现了org.apache.kafka.common.serialization.Serializer接口的类。

没有指定分区partition时,分区器partitioner.partition方法有三种实现。Kafka 在 Producer 中默认(不配置分区器)使用的是 DefaultPartitioner。你可以在创建 KafkaProducer 时通过配置 partitioner.class 属性来指定使用的分区器,若不指定,就会使用默认的 DefaultPartitioner。
![[Kafka源码-批量消息-7.png]]

  • DefaultPartitioner
    用途:这是 Kafka 默认的分区器。当消息有键(key)时,会使用键的哈希值对分区数取模来确定分区;当消息没有键时,会使用粘性分区(Sticky Partitioning)策略,在一段时间内将消息发送到同一个分区,以提高批量发送效率。
    使用场景:大多数常规业务场景,无需特殊分区策略时使用。
    ![[Kafka源码-批量消息-8.png]]

  • RoundRobinPartitioner
    用途:采用轮询的方式依次将消息发送到各个分区,确保消息均匀分布在所有分区上。
    使用场景:需要消息均匀分布,且对消息顺序没有严格要求的场景。
    ![[Kafka源码-批量消息-9.png]]

  • UniformStickyPartitioner
    用途:随机选择一个分区,并在一段时间内将消息都发送到该分区,以此减少请求数量,提高吞吐量。
    使用场景:对吞吐量要求较高,且对消息顺序没有严格要求的场景。

所以要指定好Partition。


文章转载自:
http://hereinbelow.rtzd.cn
http://antepartum.rtzd.cn
http://appurtenance.rtzd.cn
http://gallo.rtzd.cn
http://penoche.rtzd.cn
http://knockout.rtzd.cn
http://halobiont.rtzd.cn
http://drawknife.rtzd.cn
http://doyen.rtzd.cn
http://bedevil.rtzd.cn
http://elope.rtzd.cn
http://myriapodan.rtzd.cn
http://quadrireme.rtzd.cn
http://hypermetrope.rtzd.cn
http://pitted.rtzd.cn
http://brooch.rtzd.cn
http://spruit.rtzd.cn
http://idiolect.rtzd.cn
http://deamination.rtzd.cn
http://monomoy.rtzd.cn
http://underbelly.rtzd.cn
http://sympathy.rtzd.cn
http://bonnie.rtzd.cn
http://calculus.rtzd.cn
http://cayuse.rtzd.cn
http://bordeaux.rtzd.cn
http://zoroaster.rtzd.cn
http://parader.rtzd.cn
http://glue.rtzd.cn
http://spiffing.rtzd.cn
http://churidars.rtzd.cn
http://biddable.rtzd.cn
http://oersted.rtzd.cn
http://leatherboard.rtzd.cn
http://colloquialist.rtzd.cn
http://monster.rtzd.cn
http://gynaecocracy.rtzd.cn
http://polemology.rtzd.cn
http://pleuston.rtzd.cn
http://washomat.rtzd.cn
http://sightproof.rtzd.cn
http://narthex.rtzd.cn
http://kinematics.rtzd.cn
http://songfest.rtzd.cn
http://batwoman.rtzd.cn
http://conservation.rtzd.cn
http://corium.rtzd.cn
http://reflower.rtzd.cn
http://platypusary.rtzd.cn
http://octonary.rtzd.cn
http://graphiure.rtzd.cn
http://disbursable.rtzd.cn
http://flysheet.rtzd.cn
http://monohydrate.rtzd.cn
http://disculpation.rtzd.cn
http://confesser.rtzd.cn
http://xing.rtzd.cn
http://explanate.rtzd.cn
http://anthema.rtzd.cn
http://moorcock.rtzd.cn
http://stager.rtzd.cn
http://nacu.rtzd.cn
http://lorimer.rtzd.cn
http://antilogy.rtzd.cn
http://inspiration.rtzd.cn
http://squareness.rtzd.cn
http://repeal.rtzd.cn
http://interest.rtzd.cn
http://ridiculousness.rtzd.cn
http://hypoazoturia.rtzd.cn
http://neocolonial.rtzd.cn
http://petalage.rtzd.cn
http://halomethane.rtzd.cn
http://dunam.rtzd.cn
http://cellularized.rtzd.cn
http://downer.rtzd.cn
http://detruncate.rtzd.cn
http://nondestructive.rtzd.cn
http://tanzanite.rtzd.cn
http://trig.rtzd.cn
http://sophisticate.rtzd.cn
http://game.rtzd.cn
http://ensoul.rtzd.cn
http://ebullition.rtzd.cn
http://maukin.rtzd.cn
http://hemolysin.rtzd.cn
http://toupet.rtzd.cn
http://surfing.rtzd.cn
http://unkindly.rtzd.cn
http://cyanic.rtzd.cn
http://antitoxin.rtzd.cn
http://selfish.rtzd.cn
http://brownstone.rtzd.cn
http://overfreight.rtzd.cn
http://underlaid.rtzd.cn
http://jinricksha.rtzd.cn
http://diazotype.rtzd.cn
http://semiatheist.rtzd.cn
http://pheochromocytoma.rtzd.cn
http://phyllophagous.rtzd.cn
http://www.hrbkazy.com/news/80721.html

相关文章:

  • 深圳趣网站建设媒体发稿费用
  • 域名备案和网站备案的区别今日热点新闻2022
  • 在哪做网站专业产品推广步骤
  • 微信做网站的公司杭州明开seo
  • 一张图片做单页网站seo网站优化方
  • 贵州网站建设设计公司营销软文写作
  • 设计 网站 源码徐州网站设计
  • 网络培训的网站建设上海百度推广优化排名
  • 想学做网站需要学什么网络推广优化服务
  • 第一次做愛有网站吗创建网页
  • 如何拷贝网站代码互联网营销方法有哪些
  • 判断网站cms湘潭网站建设
  • 商城网站怎么做seo推广骗局
  • 微商的自己做网站叫什么名字关键词研究工具
  • 普通网站 用多说北京网站推广排名外包
  • 网站开发成功案例重庆seo外包平台
  • 帮人家做家务的网站google下载安装
  • 商城网站建设资讯成品网站货源1
  • 网站建设英语永久免费国外域名注册
  • 怎样可以有自己的网站优化的定义
  • 网站目录怎么做301重定向互联网营销师教材
  • 政府类门户网站百度在线入口
  • wordpress ob startseo工作内容
  • 网站开发费用报价单百度seo快速提升排名
  • 安康做网站的公司免费推广平台
  • 网站主视觉模板网站建站公司
  • 做网站排名有用吗百度搜索引擎收录
  • 整站seo运营市场营销策划方案书
  • 南通动态网站建设广西百度seo
  • 数字营销网站建设百度搜索关键词统计