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

Spring Boot集成Docker Kafka消息消费失败及Eureka缓存刷新异常问题求助

Hey there, let's tackle your two issues one by one—first the Kafka consumer conversion problem, then the Eureka client error.

1. Kafka Consumer: Cannot Convert String to Map

Root Cause

Your producer is sending a JSON string (you used ObjectMapper.writeValueAsString() to convert the Map to JSON), but your consumer is directly expecting a Map<String,String> as the payload. Spring Kafka doesn't automatically convert JSON strings to Map types out of the box—you need to explicitly configure a message converter or handle the conversion manually.

Solution 1: Configure a JSON Message Converter

Add a StringJsonMessageConverter to your Kafka listener container factory, which tells Spring to automatically parse incoming JSON strings into your target type (Map in this case):

@Configuration
public class KafkaConfig {
    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory(
            ConsumerFactory<String, Object> consumerFactory) {
        ConcurrentKafkaListenerContainerFactory<String, Object> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory);
        // Enable JSON-to-object conversion
        factory.setMessageConverter(new StringJsonMessageConverter());
        return factory;
    }
}

Then update your @KafkaListener annotation to use this custom container factory:

@KafkaListener(topics = TOPIC, groupId = groupId, containerFactory = "kafkaListenerContainerFactory")
public void processUpdateRating(@Payload Map<String,String> rate) {
    branchService.rateUpdate(Double.parseDouble(rate.get("rate")),Long.parseLong(rate.get("branchId")));
    log.debug("done update in queue");
}

Solution 2: Manually Parse the JSON String

If you don't want to configure a converter, you can modify the consumer to first receive the raw string, then parse it to a Map using ObjectMapper:

@KafkaListener(topics = TOPIC ,groupId = groupId)
public void processUpdateRating(@Payload String rateJson) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    // Use TypeReference to specify the generic Map type
    Map<String,String> rate = mapper.readValue(rateJson, new TypeReference<Map<String,String>>() {});
    branchService.rateUpdate(Double.parseDouble(rate.get("rate")),Long.parseLong(rate.get("branchId")));
    log.debug("done update in queue");
}
2. Eureka Client: JsonParseException & Cannot Execute Request

Root Cause

This error usually stems from one of three issues:

  • The Eureka server isn't running, or your client is using the wrong server URL/credentials
  • A Jackson version mismatch between your client and Eureka server, causing JSON parsing failures
  • GZIP compression issues (the server sends compressed responses that the client can't parse correctly)

Step-by-Step Fixes

  1. Verify Eureka Server Access

    • First, confirm the Eureka server is running at http://localhost:8761/eureka/. Try accessing this URL in your browser—you should see the Eureka dashboard after entering admin/admin as credentials.
    • Double-check your client's Eureka configuration (in application.yml or application.properties):
      eureka:
        client:
          service-url:
            defaultZone: http://admin:admin@localhost:8761/eureka/
          fetch-registry: true
          register-with-eureka: true
        instance:
          prefer-ip-address: true
      
  2. Fix Jackson Version Conflicts

    • If you're using Maven/Gradle, ensure you're not manually specifying Jackson dependency versions. Spring Boot manages compatible versions automatically—remove any explicit jackson-databind or related dependencies from your build file.
  3. Disable GZIP Compression (Temporary Test)

    • If the JSON parse error persists, try disabling GZIP on the Eureka server to rule out compression issues. Add this to the server's application.yml:
      eureka:
        server:
          gzip:
            enabled: false
      
  4. Check Network/Proxy Issues

    • Ensure there's no firewall, proxy, or security group blocking your client from reaching localhost:8761.

内容的提问来源于stack exchange,提问作者Abdalrhman Alkraien

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 09:39:11