kafka消费/发送消息,消息过大报错解决whose size is larger than the fetch size 1048576

一、kafka消费报错原因

  • 问题原因一:个是kafka本身的配置没有调整到上限
    在这里插入图片描述

  • 问题原因二:就是我们自己写python消费kafka代码的时候没有参数配置没有限制
    在这里插入图片描述

  • RecordTooLargeError: ("There are some messages at [Partition=Offset]: {TopicPartition(topic='psadad', partition=1): 75} whose size is larger than the fetch size 1048576 and hence cannot be ever returned. Increase the fetch size, or decrease the maximum message size the broker will allow.", {TopicPartition(topic='psadad', partition=1): 75})
    在这里插入图片描述

二、解决方案

1、原因一的解决方案
  • 在kafka中,默认的消息体大小为1M。在实际的业务场景,有时候需要修改kafka消息体大小,修改kafka消息体大小需要注意下面的几个方面
    • message.max.bytes (broker配置)
    • replica.fetch.max.bytes (partition配置)
    • max.message.bytes (topic配置)
  • 在生产者端配置max.request.size,这是单个消息最大字节数,根据实际调整,max.request.size 必须小于 message.max.bytes 以及消费者的 max.partition.fetch.bytes。这样消息就能不断发送
  • kafka config服务端配置文件server.properties, server.properties中加上的message.max.bytes配置
    在这里插入图片描述
2、原因二的解决方案
  • 如图添加参数max_partition_fetch_bytes=1048576 * 10
    在这里插入图片描述
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty客户端可以使用Kafka Consumer API消费Kafka消息,并使用Netty的Channel发送消息到Netty服务器端。以下是一些可能的步骤: 1. 在Netty客户端中创建Kafka Consumer对象,订阅Kafka Topic并获取消息。 2. 在Netty客户端中创建Netty Channel对象,并将其连接到Netty服务器端。 3. 在Kafka Consumer回调中,将Kafka消息封装为Netty消息,并使用Netty Channel发送到Netty服务器端。 下面是一个简单的示例代码: ```java public class NettyClient { private final Bootstrap bootstrap; private final EventLoopGroup group; private final KafkaConsumer<String, String> consumer; public NettyClient(KafkaConsumer<String, String> consumer) { this.consumer = consumer; this.group = new NioEventLoopGroup(); this.bootstrap = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(), new StringDecoder(), new NettyClientHandler()); } }); } public void start(String host, int port) throws InterruptedException { ChannelFuture future = bootstrap.connect(host, port).sync(); consumer.subscribe(Collections.singletonList("my-topic")); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1)); for (ConsumerRecord<String, String> record : records) { String message = record.value(); future.channel().writeAndFlush(message); } } } public void stop() { group.shutdownGracefully(); } } ``` 在上面的代码中,我们创建了一个Netty客户端,并在构造函数中传递了Kafka Consumer对象。在start()方法中,我们连接到Netty服务器端,并订阅了Kafka Topic。然后,在一个无限循环中,我们从Kafka Consumer中获取消息,并将其发送到Netty服务器端。Netty消息的编码和解码由Netty的StringEncoder和StringDecoder处理。最后,我们在stop()方法中关闭了Netty客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值