设计网站大全铲鼠湖南岚鸿相信,求几个微信推广平台,项目宣传网站模板免费下载,免费网络推广网址问题#xff1a;springboot整合kafka#xff0c;作为消费端#xff0c;对端的kafka系统是在生产环境#xff0c;在本地开发测试时配置了对端的生产环境的kafka地址。因为开发环境和对端生产环境是不通的#xff0c;所以连接肯定是失败的#xff0c;kafka的连接失败导致sp…问题springboot整合kafka作为消费端对端的kafka系统是在生产环境在本地开发测试时配置了对端的生产环境的kafka地址。因为开发环境和对端生产环境是不通的所以连接肯定是失败的kafka的连接失败导致springboot项目启动时停机。
思路
将kafka消费端配置自启动关闭。方法1创建ConcurrentKafkaListenerContainerFactory时配置setAutoStartup(false)。方法2使用kafkaKafkaListener注解时配置autoStartup “false”。 这样kakfa的消费端不会自己启动也就不会影响springboot项目的启动。springboot项目启动完成后再手动启动kafka消费端。使用kafkaKafkaListener注解时配置id kafkaTest。创建MyApplicationReadyEventListener在spingboot项目启动完成后再手动启动kafka消费端
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.listener.MessageListenerContainer;
import org.springframework.stereotype.Component;import java.util.Objects;Component
public class MyApplicationReadyEventListener implements ApplicationListenerApplicationReadyEvent {Autowiredprivate KafkaListenerEndpointRegistry registry;Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {MessageListenerContainer messageListenerContainer registry.getListenerContainer(kafkaTest);if(Objects.nonNull(messageListenerContainer)) {if(!messageListenerContainer.isRunning()) {messageListenerContainer.start();} else {if(messageListenerContainer.isContainerPaused()) {messageListenerContainer.resume();}}}}
}