“Tombstone”记录的 Null 负载和日志压缩

使用日志压缩时,您可以使用nullpayloads 来标识密钥的删除。 您还可以接收null值,例如可能返回null当它无法反序列化值时。spring-doc.cadn.net.cn

生成 Null 有效负载

要发送nullpayload 使用PulsarTemplate中,您可以使用 Fluent API 并将 null 传递到newMessage()方法,例如:spring-doc.cadn.net.cn

pulsarTemplate
        .newMessage(null)
        .withTopic("my-topic")
        .withSchema(Schema.STRING)
        .withMessageCustomizer((mb) -> mb.key("key:1234"))
        .send();
发送 null 值时,必须指定架构类型,因为系统无法从null有效载荷。

使用 Null 负载

@PulsarListener@PulsarReadernullpayload 根据 Listener 的 message 参数的类型传递到 Listener 方法中,如下所示:spring-doc.cadn.net.cn

参数类型 传入值

原始spring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

用户定义spring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

org.apache.pulsar.client.api.Message<T>spring-doc.cadn.net.cn

非 null Pulsar 消息,其getValue()返回nullspring-doc.cadn.net.cn

org.springframework.messaging.Message<T>spring-doc.cadn.net.cn

非 null Spring 消息,其getPayload()返回PulsarNullspring-doc.cadn.net.cn

List<X>spring-doc.cadn.net.cn

其条目 (X)是上述类型之一,并相应地执行作(即原始条目是null等等。spring-doc.cadn.net.cn

org.apache.pulsar.client.api.Messages<T>spring-doc.cadn.net.cn

非 null Pulsar 消息的非 null 容器,其getValue()返回nullspring-doc.cadn.net.cn

当传入值为null(即具有原始类型或用户定义类型的单记录侦听器)您必须使用@Payloadparameter 注解替换为required = false.
使用 Spring 时org.springframework.messaging.Message对于侦听器有效负载类型,其泛型类型信息必须足够宽才能接受Message<PulsarNull>(例如。Message,Message<?>Message<Object>). 这是因为 Spring Message 不允许其有效负载使用 null 值,而是使用PulsarNull占 位 符。

如果它是压缩日志的逻辑删除消息,则通常还需要 key,以便您的应用程序可以确定哪个 key 是”deleted". 以下示例显示了此类配置:spring-doc.cadn.net.cn

@PulsarListener(
        topics = "my-topic",
        subscriptionName = "my-topic-sub",
        schemaType = SchemaType.STRING)
void myListener(
        @Payload(required = false) String msg,
        @Header(PulsarHeaders.KEY) String key) {
    ...
}
@PulsarReader尚不支持@Header参数,因此它在日志压缩场景中的用处不大。