Reputation: 99
I am trying to get to know Kafka but I am having some problems. I installed Kafka on a Centos8 VPS, and everything works when I am connected to the VPS.
For the record, I opened the 9092 port for remote access.
The problem is that when I try to create a Message using Spring Boot, the Producer tries to connect to localhost instead of using the server ip address.
This is the configuration Bean :
@Bean
public ProducerFactory<String, String> producerStringFactory() {
Map<String, Object> config = new HashMap<>();
// the server name
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "152.225.215.185:9092");
// the key type
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
// the value type
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<String, String>(config);
}
I use a simple method for producing Messages, with a hard coded topic name:
// code
private static final String TOPIC = "testTopic1";
//code
@GetMapping("/publish/{message}")
public String postString(@PathVariable String message) {
kafkaStringTemplate.send(TOPIC, "This is a message sent to the " + TOPIC + " topic");
return "Published: " + message;
}
When I launch a request I get the ip adress declared in the configuration bean :
2021-04-17 12:20:01.986 INFO 2520 --- [nio-8080-exec-1] o.a.k.clients.producer.ProducerConfig : ProducerConfig values:
acks = 1
batch.size = 16384
bootstrap.servers = [152.255.215.185:9092]
But then Spring tries to connect to localhost, I suppose because Kafka is set to automatically use localhost
2021-04-17 12:20:06.591 WARN 2520 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node 0 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
I suppose the answer is somewhere the Kafka configuration files.
Thank you for your answers. For the record, I started learning Apache Kafka yesterday, so please excuse me for misusing technical terms.
Upvotes: 1
Views: 3383
Reputation: 99
I found the solution thanks to a video published by SelfTuts on Youtube:
https://www.youtube.com/watch?v=jY5fzVCkABg
I had to modify the server.properties file :
nano /usr/local/kafka/config/server.properties
Inside the file I added the server hosting Kafka Ip's address for the advertised.listeners and to the zookeeper.connect properties.
advertised.listeners=PLAINTEXT://152.255.215.185:9092
zookeeper.connect=152.255.215.185:2181
Upvotes: 3