Spring Boot Kafka生产者配置后仍尝试连接localhost:9092,无法连接远程集群节点问题求助
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 9094If 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 correctbootstrap.serversproperty. 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'slistenersandadvertised.listenersare correctly set toPLAINTEXT://192.168.10.10:9094, but make sure you restarted the Kafka broker after updatingserver.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:9094If 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 yourapplication.propertiesorapplication.yml:- For
.propertiesformat: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
.ymlformat: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 camelCasebootstrapServers—Spring Boot uses hyphenated keys for externalized config.- For
Check Custom Producer Factory Beans
If you're manually defining aProducerFactorybean in your code, make sure you didn't hardcodelocalhost: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 containingbootstrap-servers—this will show the actual value the application is using. If it still showslocalhost: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
- Environment variables overriding the config (e.g.,
Final Quick Checks
- Confirm the Kafka broker is running and can connect to its ZooKeeper cluster (your
zookeeper.connectis set to192.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

