如果所有经纪人都不可用,如何关闭 KStream
How to close KStream if all brokers are not available
我正在使用 Kstreams 和 SpringBoot 应用程序。如果 Kafka 已关闭或无法访问,则只有以下内容被锁定:
Connection to node -1 could not be established. Broker may not be
available.
如果发生这种情况,我想关闭应用程序或至少记录异常。有什么办法可以做到吗
您可以在发生此类异常时添加关闭钩子。可能是
的重复项
请
我添加了一个持续进行连接检查的 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;
}
}
}
我正在使用 Kstreams 和 SpringBoot 应用程序。如果 Kafka 已关闭或无法访问,则只有以下内容被锁定:
Connection to node -1 could not be established. Broker may not be available.
如果发生这种情况,我想关闭应用程序或至少记录异常。有什么办法可以做到吗
您可以在发生此类异常时添加关闭钩子。可能是
请
我添加了一个持续进行连接检查的 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;
}
}
}