Java Api Consumer 連線啟用Kerberos認證的Kafka

weixin_33912246發表於2018-08-23

java程式連線到一個需要Kerberos認證的kafka叢集上,消費生產者生產的資訊,kafka版本是2.10-0.10.0.1;

Java程式以maven構建,(怎麼構建maven工程,可去問下度娘:“maven工程入門示例”)

先上pom.xml檔案

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ht</groupId>
  <artifactId>kafkaTest</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka-clients</artifactId>
      <version>0.10.0.1</version>
    </dependency>
  </dependencies>
   <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.7</source>
                     <target>1.7</target>
                 </configuration>
             </plugin>
         </plugins>
    </build>
</project>


然後是Jave程式碼,先上圖,一一解釋圖中標識:

 

註釋:

    1:可以將所需的配置檔案載入到程式;(參見:度娘--“JDK 執行引數 JAVA -Dxxx與System.setProperty()的關係”)

    2:新版本的Producter和Consumer都可以直接連線brocker,不用再配置zookeeper的相關資訊,所以這裡是要連線的kafka的主機ip和埠號

    3:設定的topic的組Id

    4:設定偏移量

    5:設定認證配置

    6:設定所要讀取的主題Topic

 

import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class ConsumerTest {
    public static void main(String[] args) {
        
//        System.setProperty("java.security.auth.login.config", "/home/kafka/kafka_client_jaas.conf"); 
//        System.setProperty("java.security.krb5.conf", "/home/kafka/krb5.conf"); 
        // 環境變數新增,需要輸入配置檔案的路徑System.out.println("===================配置檔案地址"+fsPath+"\\conf\\cons_client_jaas.conf"); 
        Properties props = new Properties(); 
        props.put("bootstrap.servers", "192.168.132.130:9092"); 
        props.put("group.id", "group-1111"); 
        props.put("enable.auto.commit", "false"); 
        props.put("auto.commit.interval.ms", "1000"); 
        props.put("auto.offset.reset", "earliest"); 
        props.put("session.timeout.ms", "30000"); 
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); 
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); 
 
        props.put("security.protocol", "SASL_PLAINTEXT"); 
        props.put("sasl.mechanism", "GSSAPI"); 
        props.put("sasl.kerberos.service.name", "kafka");
        
        KafkaConsumer kafkaConsumer = new KafkaConsumer<>(props); 
        kafkaConsumer.subscribe(Arrays.asList("cust_info"));
        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());
                
        }
    }
}

 

以上就是所有配置,將工程通過匯出為Runnable JAR file 匯出為jar檔案

 

直接執行   java -jar jar包名.jar  即可;

如果程式裡沒有設定1相關的配置檔案,也可以執行下列命令:

java -Djava.security.auth.login.config=/home/kafka/kafka_client_jaas.conf   -Djava.security.krb5.conf=/home/kafka/krb5.conf  -jar  jar包名.jar 

相關文章