Kafka《倾听者》中的Spring钩

8aqjt8rx  于 2021-06-05  发布在  Kafka
关注(0)|答案(1)|浏览(271)

在Kafka听留言之前/之后有什么可用的钩子吗?
用例:必须为设置mdc co-relationid以执行日志跟踪
我在找什么?一种before/after回调方法,这样就可以在进入时设置mdc关联id,并最终在退出时清除mdc。
已编辑的场景:我正在获取作为kafka头的一部分的co-relationid,并且我希望在kafka侦听器中收到消息后立即在mdc中设置相同的关系id
谢谢你的帮助

eivnm1vs

eivnm1vs1#

您可以向侦听器bean添加一个around建议。。。

@SpringBootApplication
public class So59854374Application {

    public static void main(String[] args) {
        SpringApplication.run(So59854374Application.class, args);
    }

    @Bean
    public static BeanPostProcessor bpp() { // static is important
        return new BeanPostProcessor() {

            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof MyListener) {
                    ProxyFactoryBean pfb = new ProxyFactoryBean();
                    pfb.setTarget(bean);
                    pfb.addAdvice(new MethodInterceptor() {

                        @Override
                        public Object invoke(MethodInvocation invocation) throws Throwable {
                            try {
                                System.out.println("Before");
                                return invocation.proceed();
                            }
                            finally {
                                System.out.println("After");
                            }
                        }

                    });
                    return pfb.getObject();
                }
                return bean;
            }

        };
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("so59854374").partitions(1).replicas(1).build();
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template) {
        return args -> template.send("so59854374", "foo");
    }

}

@Component
class MyListener {

    @KafkaListener(id = "so59854374", topics = "so59854374")
    public void listen(String in) {
        System.out.println(in);
    }

}

Before
foo
After

编辑
如果你加上 @Header("myMdcHeader") byte[] mdc 作为kafka侦听器方法的附加参数,可以使用 getArguments()[1] 在召唤上。
另一个解决方案是添加 RecordInterceptor 侦听器容器工厂,它允许您访问原始 ConsumerRecord 在传递给侦听器适配器之前。

/**
 * An interceptor for {@link ConsumerRecord} invoked by the listener
 * container before invoking the listener.
 *
 * @param <K> the key type.
 * @param <V> the value type.
 *
 * @author Gary Russell
 * @since 2.2.7
 *
 */
@FunctionalInterface
public interface RecordInterceptor<K, V> {

    /**
     * Perform some action on the record or return a different one.
     * If null is returned the record will be skipped.
     * @param record the record.
     * @return the record or null.
     */
    @Nullable
    ConsumerRecord<K, V> intercept(ConsumerRecord<K, V> record);

}
/**
 * Set an interceptor to be called before calling the listener.
 * Does not apply to batch listeners.
 * @param recordInterceptor the interceptor.
 * @since 2.2.7
 */
public void setRecordInterceptor(RecordInterceptor<K, V> recordInterceptor) {
    this.recordInterceptor = recordInterceptor;
}

如果您使用的是批处理侦听器,kafka提供 ConsumerInterceptor .

相关问题