Kafka 监听器中的钩子
Hooks in Kafka Listener
kafka 收听消息之前/之后是否有可用的挂钩?
用例:
必须设置 MDC 关联 ID 才能执行日志可追溯性
我在找什么?
before/after 回调方法,以便可以在进入时设置 MDC 关联 ID,并最终在退出时清理 MDC。
已编辑场景:
我正在获取作为 Kafka Headers 一部分的关联 ID,我想在 Kafka Listener
中收到消息后立即在 MDC 中设置相同的 ID
感谢帮助
您可以向侦听器 bean 添加环绕建议...
@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
。
kafka 收听消息之前/之后是否有可用的挂钩?
用例: 必须设置 MDC 关联 ID 才能执行日志可追溯性
我在找什么? before/after 回调方法,以便可以在进入时设置 MDC 关联 ID,并最终在退出时清理 MDC。
已编辑场景: 我正在获取作为 Kafka Headers 一部分的关联 ID,我想在 Kafka Listener
中收到消息后立即在 MDC 中设置相同的 ID感谢帮助
您可以向侦听器 bean 添加环绕建议...
@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
。