1 producer直接上代码:
public class JavaKafkaWithKerberos {
public static void main(String[] args) throws Exception{
System.setProperty("java.security.auth.login.config", "d:/myconf/jaas.conf");
System.setProperty("java.security.krb5.conf", "d:/myconf/krb5.conf");
// System.setProperty("java.security.auth.login.config", "/tmp/myconf/jaas.conf");
// System.setProperty("java.security.krb5.conf", "/tmp/myconf/krb5.conf");
Properties props = new Properties();
props.put("bootstrap.servers", "1000000000000:9092,000000000000:9092,0000000000000:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
//props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "GSSAPI");
props.put("sasl.kerberos.service.name", "kafka");
KafkaProducer<String,String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 10000; i++){
String s = UUID.randomUUID().toString() +" " + i + " Test Date: " + new Date();
System.out.println(s);
producer.send(new ProducerRecord<>("sink-topic",s ));// 仅V, K为null
Thread.sleep(1000);
}
}
}
2 consumer直接上代码
public class JavaKafkaConsumedrWithKerberos { public static void main(String[] args) throws Exception{ System.setProperty("java.security.auth.login.config", "d:/myconf/jaas.conf"); System.setProperty("java.security.krb5.conf", "d:/myconf/krb5.conf"); // System.setProperty("java.security.auth.login.config", "/tmp/myconf/jaas.conf"); // System.setProperty("java.security.krb5.conf", "/tmp/myconf/krb5.conf"); Properties props = new Properties(); props.put("bootstrap.servers", "000000000000:9092,000000000000:9092,000000000000:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); //props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); //props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); props.put("group.id", "test"); //kerberos安全认证 props.put("security.protocol", "SASL_PLAINTEXT"); props.put("sasl.mechanism", "GSSAPI"); props.put("sasl.kerberos.service.name", "kafka"); KafkaProducer<String,String> producer = new KafkaProducer<>(props); KafkaConsumer kafkaConsumer = new KafkaConsumer<>(props); //kafkaConsumer.subscribe(Arrays.asList("cust_info"));(Collections.singletonList("consumerCodeTopic") kafkaConsumer.subscribe(Collections.singletonList("test")); while (true) { ConsumerRecords<String, String> records = kafkaConsumer.poll(1); for (ConsumerRecord<String, String> record : records) System.out.println("Partition: " + record.partition() + " Offset: " + record.offset() + " Value: " + record.value() + " ThreadID: " + Thread.currentThread().getId()); } } }
3 配置文件:jaas.conf
KafkaClient {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="d:/myconf/hadoop.keytab"
storeKey=true
useTicketCache=false
serviceName="kafka"
principal="hadoop/cdh@HADOOP.COM";
};
Client {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
storeKey=true
useTicketCache=false
serviceName="kafka"
keyTab="d:/myconf/hadoop.keytab"
principal="hadoop/cdh@HADOOP.COM";
};
注:上述代码可以不用在job运行时,执行kinit -kt keytab认证。
如果没有指定keytab路径,在运行job前,执行kinit -kt /home/hadoop/hadoop.keytab也可以达到目的。