Originally, I was going to try to play with kafka server by myself, but I didn't expect to hit nails when I came up, so I couldn't toss around for a long time.
Environmental Science:
kafka 2.9.2 - 0.0.2.2
Phenomenon:
Create themes and produce data within the server
No problem receiving with the same server
Call kafka with java on another machine and produce data error reporting
Failed to send messages after 3 tries
After consulting all kinds of data, the solution is given.
1) Check whether the server port is open
telnet ip port
2) Check whether the configuration files advertised.host.name and advertised.port are annotated under service.properties
Open and configure the current server ip and port number
Restart kafka after change
Test code:
import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class MyProducer {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("metadata.broker.list","ip:port");
props.setProperty("serializer.class","kafka.serializer.StringEncoder");
props.put("request.required.acks","1");
ProducerConfig config = new ProducerConfig(props);
//Creating Production Object
Producer<String, String> producer = new Producer<String, String>(config);
//Generate message
KeyedMessage<String, String> data = new KeyedMessage<String, String>("mykafka","test-kafka");
try {
int i =1;
while(i < 100){
//send message
producer.send(data);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
producer.close();
}
}
Consumption of the theme on the server at the same time: mykafka
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic mykafka --from-beginning
Reference and thanks
http://www.mamicode.com/info-detail-1008759.html