繁体   English   中英

如果 Kafka 和 Zookeeper 没有运行,为什么 Maven Clean Install 不会完成?

[英]Why won't Maven Clean Install complete if Kafka and Zookeeper are not running?

为了学习Apache Kafka ,我开发了一个Spring Boot应用程序,如果我向调用KafkaTemplate发送方法的控制器发送 POST 请求,该应用程序会向Kafka主题发送消息。 我正在运行 Ubuntu 19.04 并在本地成功设置并安装了KafkaZookeeper 一切正常。

当我关闭ZookeeperKafka时会出现问题。 如果我这样做,那么在启动时,我的应用程序的Kafka AdminClient定期尝试查找兄弟,但将此消息发送到控制台

Connection to node -1 could not be established. Broker may not be available.

我实施了此处建议的修复Kafka + Zookeeper:无法建立与节点 -1 的连接。 Broker 可能不可用,这里Spring-Boot 和 Kafka:如何处理 Broker 不可用? . 但是,如果我运行maven clean install那么如果ZookeeperKafka没有运行,构建将永远不会完成。 这是为什么,有没有办法配置应用程序,以便它在启动时检查Kafka可用性并在服务不可用时正常处理?

这是我调用KafkaTemplate服务类

@Autowired
public PingMessageServiceImpl(KafkaTemplate kafkaTemplate, KafkaTopicConfiguration kafkaTopicConfiguration) {
    this.kafkaTemplate = kafkaTemplate;
    this.kafkaTopicConfiguration = kafkaTopicConfiguration;
}

@Override
public void sendMessage(String message) {
    log.info(String.format("Received following ping message %s", message));

    if (!isValidPingRequest(message)) {
        log.warn("Received invalid ping request");
        throw new InvalidPingRequestException();
    }
    log.info(String.format("Sending message=[%s]", message));
    ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(kafkaTopicConfiguration.getPingTopic(), message);
    future.addCallback(buildListenableFutureCallback(message));
}

private boolean isValidPingRequest(String message) {
    return "ping".equalsIgnoreCase(message);
}

private ListenableFutureCallback<SendResult<String, String>> buildListenableFutureCallback(String message) {
    return new ListenableFutureCallback<SendResult<String, String>>() {
        @Override
        public void onSuccess(SendResult<String, String> result) {
            log.info(String.format("Sent message=[%s] with offset=[%d]", message, result.getRecordMetadata().offset()));
        }
        @Override
        public void onFailure(Throwable ex) {
            log.info(String.format("Unable to send message=[%s] due to %s", message, ex.getMessage()));
        }
    };
}

这是我用来从属性文件中提取 Kafka 配置属性的配置类

@NotNull(message = "bootstrapAddress cannot be null")
@NotBlank(message = "bootstrapAddress cannot be blank")
private String bootstrapAddress;

@NotNull(message = "pingTopic cannot be null")
@NotBlank(message = "pingTopic cannot be blank")
private String pingTopic;

@NotNull(message = "reconnectBackoffMs cannot be null")
@NotBlank(message = "reconnectBackoffMs cannot be blank")
@Value("${kafka.reconnect.backoff.ms}")
private String reconnectBackoffMs;

@Bean
public KafkaAdmin kafkaAdmin() {
    Map<String, Object> configurations = new HashMap<>();
    configurations.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    configurations.put(AdminClientConfig.RECONNECT_BACKOFF_MS_CONFIG, reconnectBackoffMs);
    return new KafkaAdmin(configurations);
}

@Bean
public NewTopic pingTopic() {
    return new NewTopic(pingTopic, 1, (short) 1);
}

@PostConstruct
private void displayOnStartup() {
    log.info(String.format("bootstrapAddress is %s", bootstrapAddress));
    log.info(String.format("reconnectBackoffMs is %s", reconnectBackoffMs));
}

如果在加载ApplicationContext spring kafka bean 时有任何Spring-boot集成测试,例如KafakTemplateKafkaAdmin将尝试使用ymlproperties文件中指定的属性连接 kafka 服务器

因此,为了避免这种情况,您可以使用spring-embedded-kafka-server ,以便 kafka bean 在测试执行期间连接到嵌入式服务器。

或者很简单,您可以在集成测试用例中使用@MockBean注释来模拟KafakTemplateKafkaAdmin bean

暂无
暂无

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

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