You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kafka宕机时如何让Spring Cloud Sleuth应用正常启动并提供服务

Fix: Application endpoints inaccessible when Kafka is down with Spring Cloud Sleuth

Hey there, let's tackle this issue where your app's endpoints become unreachable when Kafka is down—since tracing is a non-core feature, we definitely don't want it bringing down your main business functionality.

What's causing this?

The root problem here is that the default configuration for Spring Cloud Sleuth Stream with Kafka uses blocking synchronous message sending. When Kafka is unavailable, the producer threads get stuck trying to connect or send data, and this ends up hogging the app's request-handling threads, making your endpoints unresponsive.

Solutions to isolate tracing from core business

Here are a few actionable fixes tailored to your Spring Cloud Sleuth 1.x setup:

1. Configure Kafka Producer for Async Sending & Timeouts

Add these properties to your application.properties or application.yml to make the producer non-blocking and set hard timeouts, so it doesn't hang indefinitely:

# Kafka Producer tweaks to avoid blocking
spring.cloud.stream.kafka.binder.configuration.producer.type=async
spring.cloud.stream.kafka.binder.configuration.request.timeout.ms=5000
spring.cloud.stream.kafka.binder.configuration.retries=0
spring.cloud.stream.kafka.binder.configuration.max.block.ms=3000
  • producer.type=async: Switches the producer to send messages asynchronously, so your main threads aren't blocked waiting for Kafka
  • request.timeout.ms: Sets a hard limit for how long the producer waits for a response from Kafka before giving up
  • retries=0: Disables retries—no need to keep hammering an unavailable broker
  • max.block.ms: Limits how long the producer can block when fetching metadata or when the buffer is full, preventing thread starvation

2. Tell Sleuth to Ignore Send Failures

This config makes Sleuth swallow any errors when sending tracing data, so failures don't bubble up and affect your app:

# Sleuth Stream error handling
spring.cloud.sleuth.stream.send-error-handler-mode=ignore

With this, if Kafka is down, Sleuth will just log the failure (if enabled) and move on without blocking your app.

3. Dynamic Fallback: Disable Tracing Push When Kafka is Down

For more control, you can add a toggle to disable Kafka-based tracing when the broker is unavailable:

  • First, add a config toggle:
# Custom toggle to enable/disable Sleuth Kafka push
app.sleuth.kafka.enabled=true
  • Then create a fallback Sender bean that does nothing when the toggle is off (you can tie this to Kafka's health status with Spring Boot Actuator if you want auto-switching):
@Configuration
public class SleuthFallbackConfig {

    @Bean
    @ConditionalOnProperty(name = "app.sleuth.kafka.enabled", havingValue = "false")
    public Sender fallbackSleuthSender() {
        return spans -> {
            // No-op: don't send any tracing data when fallback is active
        };
    }
}

Quick Fix for Those Kafka Config Warnings

You might have noticed the key.deserializer/value.deserializer warnings in your logs—this is a minor quirk in older Spring Cloud Stream versions where consumer configs are accidentally applied to producers. Fix it by explicitly setting producer serializers:

# Resolve Kafka config warnings
spring.cloud.stream.kafka.binder.configuration.key.serializer=org.apache.kafka.common.serialization.StringSerializer
spring.cloud.stream.kafka.binder.configuration.value.serializer=org.springframework.kafka.support.serializer.JsonSerializer

内容的提问来源于stack exchange,提问作者PRATHAP S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:15:31