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

Spring Boot Kafka生产者配置后仍尝试连接localhost:9092,无法连接远程集群节点问题求助

Troubleshooting Two Kafka Producer Connection Issues

Hey there, let's work through the two Kafka producer connection problems you're dealing with. I'll break down each issue and share practical fixes based on the broker config you provided.


1. Java Native Kafka Producer Can't Connect to Remote Cluster

First, let's rule out common culprits for this issue:

  • Verify Network Connectivity
    Make sure your local machine can reach the remote broker's port (9094). Run this command to test:

    nc -zv 192.168.10.10 9094
    # Or use telnet if nc isn't available
    telnet 192.168.10.10 9094
    

    If this fails, check the remote server's firewall/security group—ensure port 9094 is open for inbound traffic from your client IP.

  • Double-Check Producer Bootstrap Config
    Ensure your Java producer code sets the correct bootstrap.servers property. It should explicitly point to your remote broker:

    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.10.10:9094");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    
    KafkaProducer<String, String> producer = new KafkaProducer<>(props);
    

    A typo in the IP or port is one of the most common causes here.

  • Confirm Broker Config is Applied
    Your broker's listeners and advertised.listeners are correctly set to PLAINTEXT://192.168.10.10:9094, but make sure you restarted the Kafka broker after updating server.properties. Check the broker startup logs for a line like:

    INFO [kafka.server.KafkaServer] [BrokerId=1] Started listening for connections on PLAINTEXT://192.168.10.10:9094.
    INFO [kafka.server.KafkaServer] [BrokerId=1] Advertised listener: PLAINTEXT://192.168.10.10:9094
    

    If these lines don't show up, your config changes weren't loaded properly.

  • Check for Network Routing/VPN Restrictions
    If you're on a VPN or behind a corporate network, ensure there's no routing rule blocking traffic to the remote broker's IP/port.


2. Spring Boot Kafka Producer Always Connects to localhost:9092

This almost always boils down to misconfigured properties or overridden settings. Here's how to fix it:

  • Validate Your Spring Boot Config
    Ensure you're using the correct property key in your application.properties or application.yml:

    • For .properties format:
      spring.kafka.producer.bootstrap-servers=192.168.10.10:9094
      # Optional: Add other producer configs if needed
      spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
      spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
      
    • For .yml format:
      spring:
        kafka:
          producer:
            bootstrap-servers: 192.168.10.10:9094
            key-serializer: org.apache.kafka.common.serialization.StringSerializer
            value-serializer: org.apache.kafka.common.serialization.StringSerializer
      

    Important: Use bootstrap-servers (hyphen-separated), not camelCase bootstrapServers—Spring Boot uses hyphenated keys for externalized config.

  • Check Custom Producer Factory Beans
    If you're manually defining a ProducerFactory bean in your code, make sure you didn't hardcode localhost:9092:

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        // Replace localhost:9092 with your remote broker address
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.10.10:9094");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        return new DefaultKafkaProducerFactory<>(configProps);
    }
    

    Hardcoded values will override any properties from your config file.

  • Verify Config Loading
    Check your Spring Boot startup logs for lines containing bootstrap-servers—this will show the actual value the application is using. If it still shows localhost:9092, check for:

    • Environment variables overriding the config (e.g., SPRING_KAFKA_PRODUCER_BOOTSTRAP_SERVERS=localhost:9092)
    • Config files placed in the wrong directory (ensure it's in src/main/resources)
    • Profile-specific configs that might be overriding your default settings

Final Quick Checks

  • Confirm the Kafka broker is running and can connect to its ZooKeeper cluster (your zookeeper.connect is set to 192.168.10.11:2181—check broker logs for ZooKeeper connection errors)
  • If you're using older Kafka versions, ensure there's no compatibility issue between your client and broker versions (stick to the same major version if possible)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 14:32:32