简体   繁体   English

Spring Kafka 模板 - 在 Spring 启动时连接到 Kafka 主题

[英]Spring Kafka Template - Connect to Kafka Topic on Spring Boot Startup

I have implemented a basic Spring Boot Application which uses Spring Kafka.我已经实现了一个基本的 Spring 引导应用程序,它使用 Spring Kafka。 I want my producer to connect to the Kafka Topic before the first .send() is called but I can't find a way to do so.我希望我的制作人在调用第一个.send()之前连接到 Kafka 主题,但我找不到这样做的方法。 Is that possible?那可能吗?

Logs to show that KafkaTemplate only connects to the Kafka Topic after I trigger the .send method at 16:12:44 :日志显示 KafkaTemplate 仅在我在16:12:44触发.send方法后连接到 Kafka 主题:

2021-11-24 16:12:12.602  INFO 63930 --- [           main] c.e.k.KafkaProducerExampleApplication    : The following profiles are active: dev
2021-11-24 16:12:13.551  INFO 63930 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-11-24 16:12:13.559  INFO 63930 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-24 16:12:13.559  INFO 63930 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.53]
2021-11-24 16:12:13.613  INFO 63930 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-11-24 16:12:13.613  INFO 63930 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 974 ms
2021-11-24 16:12:13.989  INFO 63930 --- [           main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2021-11-24 16:12:14.190  INFO 63930 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-24 16:12:14.190  INFO 63930 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2021-11-24 16:12:14.207  INFO 63930 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2021-11-24 16:12:14.239  INFO 63930 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
2021-11-24 16:12:14.336  INFO 63930 --- [           main] c.e.k.KafkaProducerExampleApplication    : Started KafkaProducerExampleApplication in 7.055 seconds (JVM running for 7.341)
2021-11-24 16:12:44.550  INFO 63930 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-11-24 16:12:44.550  INFO 63930 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-24 16:12:44.551  INFO 63930 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2021-11-24 16:12:44.649  INFO 63930 --- [nio-8080-exec-1] o.a.k.clients.producer.ProducerConfig    : ProducerConfig values: 

Regarding Linh Vu's answer, it's best not to create a connection in a bean definition - it's too early in the application context's lifecycle.关于 Linh Vu 的回答,最好不要在 bean 定义中创建连接——这在应用程序上下文的生命周期中为时过早。

Instead, add a bean that implements SmartLifecycle and create the connection in start() ;相反,添加一个实现SmartLifecycle的 bean 并在start()中创建连接; that way, the context will be completely initialized before connecting.这样,上下文将在连接之前完全初始化。

@Bean
SmartLifecycle connector(ProducerFactory<Object ,Object> pf) {
    return new SmartLifecycle() {
        
        @Override
        public void stop() {
        }
        
        @Override
        public void start() {
            pf.createProducer().close();
        }
        
        @Override
        public boolean isRunning() {
            return false;
        }
        
    };
}

With non-transactional producer (transactionIdPrefix is not supplied), when you first call KafkaTemplate.send, it will delegate to ProducerFactory to get a single instance of Producer .使用non-transactional producer (未提供transactionIdPrefix),当您第一次调用KafkaTemplate.send 时,它将委托给ProducerFactory以获取Producer单个实例 At this time, because there's no a single instance of Producer before, ProducerFactory will create this one for you (that's why you saw the log ProducerConfig: ProducerConfig values... ).这个时候,因为之前没有一个ProducerFactory实例Producer会为你创建这个(这就是你看到日志ProducerConfig: ProducerConfig values...的原因)。 This producer instance is now used/shared by all clients.这个生产者实例现在被所有客户端使用/共享。


So if you want to create the above producer instance beforehand, you could directly call it on the ProducerFactory , eg:所以如果你想预先创建上面的生产者实例,你可以直接在ProducerFactory上调用它,例如:

@Bean
public KafkaTemplate<?, ?> kafkaTemplate(ProducerFactory<Object, Object> kafkaProducerFactory) {
        KafkaTemplate<Object, Object> kafkaTemplate = new KafkaTemplate(kafkaProducerFactory);
        kafkaProducerFactory.createProducer();
        return kafkaTemplate;
...

SmartLifecycle bean works for us, thanks. SmartLifecycle bean 为我们工作,谢谢。

@Component
class KafkaProducer (
    private val userChangeLogTemplate: KafkaTemplate<String, UserChangeLog>
    private val kafkaProperties: MizenKafkaProperties
) : NotificationProducer{

    @Bean
    fun connector(pf: ProducerFactory<String, Any>): SmartLifecycle {
        return object : SmartLifecycle {
            override fun stop() {}
            override fun start() {
                pf.createProducer().close()
            }

            override fun isRunning(): Boolean {
                return false
            }
        }
    }

    override fun sendUserChangeLog(message: UserChangeLog) {
        userChangeLogTemplate.send(kafkaProperties.userChangeLogTopic, message)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM