如果所有代理都不可用,如何关闭KStream

时间:2019-01-16 10:13:21

标签: java spring-boot apache-kafka apache-kafka-streams spring-kafka

我在SpringBoot应用程序中使用Kstreams。如果Kafka已关闭或无法访问,则仅锁定以下内容:

  

无法建立到节点-1的连接。经纪人可能不是   可用。

如果发生这种情况,我想关闭应用程序或至少记录异常。有什么办法可以做到这一点。

2 个答案:

答案 0 :(得分:0)

您可以在发生此类异常时添加关闭挂钩。可能与Kafka Stream: Graceful shutdown

重复

Check here

答案 1 :(得分:0)

我添加了一个CustomHealthIndicator,它可以连续进行连接检查,如果发现Kafka无法访问,它将抛出异常并关闭应用程序。

示例代码在这里:

public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return Health.up().withDetail("Status Code", "SUCCESS").build();
    }

    private void doHealthCheck() {
        Properties props = new Properties();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, localhost:9092);
        AdminClient adminClient =  AdminClient.create(props);
        try {
            DescribeClusterOptions dco = new DescribeClusterOptions();
            dco.timeoutMs(30000);
            adminClient.describeCluster(dco).clusterId().get();
        } catch (Exception e) {
            System.exit(-1);
        } finally {
            adminClient.close();
            adminClient = null;
        }
    }

}