[英]How can I make the most efficient Kafka healtcheck control?
我使用彈簧卡夫卡。 我想確定 Kafka 已關閉。 kafka 有這樣的實現嗎? 我找到了各種解決方案。 但是我發現的所有解決方案都必須編寫預定的作業。 是否有一種方法可以顯示 kafka 已關閉而不編寫預定的作業?
我可以找到的解決方案:
由於在
https://github.com/spring-projects/spring-boot/issues/12225
https://github.com/spring-projects/spring-boot/issues/12222
https://github.com/spring-projects/spring-boot/pull/12200
所以,我不想使用這個解決方案。
@Configuration
public class KafkaConfig {
@Autowired
private KafkaAdmin admin;
@Autowired
private MeterRegistry meterRegistry;
@Autowired
private Map<String, KafkaTemplate<?, ?>> kafkaTemplates;
@Bean
public AdminClient kafkaAdminClient() {
return AdminClient.create(admin.getConfig());
}
@SuppressWarnings("deprecation") // Can be avoided by relying on Double.NaN for non doubles.
@PostConstruct
private void initMetrics() {
final String kafkaPrefix = "kafka.";
for (Entry<String, KafkaTemplate<?, ?>> templateEntry : kafkaTemplates.entrySet()) {
final String name = templateEntry.getKey();
final KafkaTemplate<?, ?> kafkaTemplate = templateEntry.getValue();
for (Metric metric : kafkaTemplate.metrics().values()) {
final MetricName metricName = metric.metricName();
final Builder<Metric> gaugeBuilder = Gauge
.builder(kafkaPrefix + metricName.name(), metric, Metric::value) // <-- Here
.description(metricName.description());
for (Entry<String, String> tagEntry : metricName.tags().entrySet()) {
gaugeBuilder.tag(kafkaPrefix + tagEntry.getKey(), tagEntry.getValue());
}
gaugeBuilder.tag("bean", name);
gaugeBuilder.register(meterRegistry);
}
}
}
@Bean
public HealthIndicator kafkaHealthIndicator() {
final DescribeClusterOptions describeClusterOptions = new DescribeClusterOptions().timeoutMs(1000);
final AdminClient adminClient = kafkaAdminClient();
return () -> {
final DescribeClusterResult describeCluster = adminClient.describeCluster(describeClusterOptions);
try {
final String clusterId = describeCluster.clusterId().get();
final int nodeCount = describeCluster.nodes().get().size();
return Health.up()
.withDetail("clusterId", clusterId)
.withDetail("nodeCount", nodeCount)
.build();
} catch (InterruptedException | ExecutionException e) {
return Health.down()
.withException(e)
.build();
}
};
}
}
我發現的另一個解決方案:
public class KafkaHealthIndicator implements HealthIndicator {
private final Logger log = LoggerFactory.getLogger(KafkaHealthIndicator.class);
private KafkaTemplate<String, String> kafka;
public KafkaHealthIndicator(KafkaTemplate<String, String> kafka) {
this.kafka = kafka;
}
/**
* Return an indication of health.
*
* @return the health for
*/
@Override
public Health health() {
try {
kafka.send("kafka-health-indicator", "❥").get(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
return Health.down(e).build();
}
return Health.up().build();
}
}
在兩種解決方案中,我必須使用預定作業。 是否有一種方法可以顯示 kafka 已關閉而不編寫預定的作業? 如果沒有可用的方法,第二個解決方案是否足以顯示kafka已關閉?
AFAIK,所有 Spring 健康檢查都是預定檢查。
我信任您在其他項目中放置的第一個代碼塊,因為它依賴於健康的 Kafka 控制器來返回活動代理列表。 我建議的一個補充是在類中添加一個字段,用於您希望成為nodeCount
一部分的最少數量的經紀人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.