spring kafka ConcurrentMessageListenerContainer consumerr 中的线程名称约定

Thread name convention in spring kafka ConcurrentMessageListenerContainer consumerr

我正在使用 spring-kafka 从主题中消费,我们正在使用 ConcurrentMessageListenerContainer。

下面是我的application.yml

spring:
  kafka:
    listener:
      concurrency: 2
    consumer:
      group-id: test-consumer-group
      topic: CONSUMER-TOPIC

在日志中我们可以看到线程名称打印为 test-consumer-group-0-C-1。

2021-10-04 11:04:41.254 [test-consumer-group-0-C-1]

我已经检查了下面的文件,以了解该线程名称是如何到达的。

这里的线程名是这样的[group-id]-[concurrency]-C-X。 无法理解这个 X 是如何得出的。

文档中有很好的解释:https://docs.spring.io/spring-kafka/docs/current/reference/html/#container-thread-naming

So, with a bean name of container, threads in this container will be named container-0-C-1, container-1-C-1 etc., after the container is started the first time; container-0-C-2, container-1-C-2 etc., after a stop and subsequent start.

所以,你的等式中的X属于线程号,当它是从执行者那里得到的,用于处理KafkaConsumer交互。见 CustomizableThreadCreator:

/**
 * Return the thread name to use for a newly created {@link Thread}.
 * <p>The default implementation returns the specified thread name prefix
 * with an increasing thread count appended: e.g. "SimpleAsyncTaskExecutor-0".
 * @see #getThreadNamePrefix()
 */
protected String nextThreadName() {
    return getThreadNamePrefix() + this.threadCount.incrementAndGet();
}