我正在尝试使用Kerberos为Kafka配置安全性。
它通过命令提示符正常工作。
但是当我通过java代码尝试时遇到问题。
将错误视为:
无法找到Set的领导者([test,0],[test,1])
显示java.lang.NullPointerException 在org.apache.kafka.common.utils.Utils.formatAddress(Utils.java:312)
这是我的代码,
final static String clientId = "SimpleConsumerDemoClient";
public final static String TOPIC = "test";
ConsumerConnector consumerConnector;
public static void main(String[] argv) throw
UnsupportedEncodingException {
System.setProperty("java.security.krb5.realm", "EXAMPLE.COM");
System.setProperty("java.security.krb5.kdc", "FQDN");
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("sun.security.spnego.debug", "true");
System.setProperty("java.security.krb5.conf","root/etc/krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
Consumer helloKafkaConsumer = new Consumer();
helloKafkaConsumer.start();
}
public Consumer(){
Properties properties = new Properties();
properties.put("zookeeper.connect","DOMAIN:2181");
properties.put("group.id","test-group");
properties.put("security.protocol", "PLAINTEXTSASL");
properties.put("auto.offset.reset","smallest");
ConsumerConfig consumerConfig = new ConsumerConfig(properties);
consumerConnector = kafka.consumer.Consumer.createJavaConsumerConnector(consumerConfig);
}
@Override
public void run() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(TOPIC, new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector.createMessageStreams(topicCountMap);
KafkaStream<byte[], byte[]> stream = consumerMap.get(TOPIC).get(0);
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while(it.hasNext()){
String str = new String(it.next().message(), StandardCharsets.UTF_8);
System.out.println(str+" The message consumed");
}
}
private static void printMessages(ByteBufferMessageSet messageSet) throws UnsupportedEncodingException {
for(MessageAndOffset messageAndOffset: messageSet) {
ByteBuffer payload = messageAndOffset.message().payload();
byte[] bytes = new byte[payload.limit()];
payload.get(bytes);
System.out.println(new String(bytes, "UTF-8"));
}
}
}
感谢任何帮助。