首页 > 编程语言 >Java代码kafka通过Kerberos认证消费数据

Java代码kafka通过Kerberos认证消费数据

时间:2023-01-11 10:22:56浏览次数:60  
标签:Java java Kerberos kafka new setProperty import properties

引入依赖

<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>

kafka生产

import java.io.PrintStream;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

public class KafkaProducerExample
{
  public static void main(String[] args)
  {
    System.setProperty("java.security.krb5.conf", "kerberos/krb5.conf");
    Properties properties = new Properties();
    properties.setProperty("bootstrap.servers", "aaaa:9092,bbbb:9092,cccc:9092");
    
    properties.setProperty("sasl.jaas.config", "com.sun.security.auth.module.Krb5LoginModule required \n        useKeyTab=true \n        storeKey=true  \n        keyTab=\"kerberos/rcp.keytab\" \n        principal=\"[email protected]\";");
    
    properties.setProperty("sasl.kerberos.service.name", "kafka");
    properties.setProperty("sasl.mechanism", "GSSAPI");
    properties.setProperty("security.protocol", "SASL_PLAINTEXT");
    
    Properties properties1 = ProducerConfig.addSerializerToConfig(properties, new StringSerializer(), new StringSerializer());
    
    KafkaProducer<String, String> stringStringKafkaProducer = new KafkaProducer(properties1);
    Random random = new Random();
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext())
    {
      String next = scanner.next();
      ProducerRecord<String, String> rcp_test_001 = new ProducerRecord("rcp_test_002", random.nextInt() + "", next);
      stringStringKafkaProducer.send(rcp_test_001);
      stringStringKafkaProducer.flush();
      
    }
  }
}

kafka消费

import java.io.PrintStream;
import java.time.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

public class KafkaConsumerExample
{
  public static void main(String[] args)
  {
    System.setProperty("java.security.krb5.conf", "kerberos/krb5.conf");
    Properties properties = new Properties();
    properties.setProperty("bootstrap.servers", "aaaa:9092,bbbb:9092,ccc:9092");
    
    properties.setProperty("sasl.jaas.config", "com.sun.security.auth.module.Krb5LoginModule required \n        useKeyTab=true \n        storeKey=true  \n        keyTab=\"kerberos/rcp.keytab\" \n        principal=\"[email protected]\";");
    
    properties.setProperty("sasl.kerberos.service.name", "kafka");
    properties.setProperty("sasl.mechanism", "GSSAPI");
    properties.setProperty("security.protocol", "SASL_PLAINTEXT");
    properties.setProperty("auto.offset.reset", "earliest");
    properties.setProperty("group.id", "test_003");
    
    Properties properties1 = ConsumerConfig.addDeserializerToConfig(properties, new StringDeserializer(), new StringDeserializer());
    
    KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer(properties1);
    kafkaConsumer.subscribe(Collections.singleton("rcp_test_002"));
    for (;;)
    {
      ConsumerRecords<String, String> poll = kafkaConsumer.poll(Duration.ofMillis(1000L));
      if (!poll.isEmpty())
      {
        Iterator<ConsumerRecord<String, String>> iterator = poll.iterator();
        while (iterator.hasNext())
        {
          ConsumerRecord<String, String> next = (ConsumerRecord)iterator.next();
          System.out.println((String)next.value());
        }
      }
    }
  }
}

https://www.pudn.com/news/631d3ae00a3381336819ced4.html

标签:Java,java,Kerberos,kafka,new,setProperty,import,properties
From: https://www.cnblogs.com/eternityz/p/17042992.html

相关文章

  • JavaWeb综合案例
    JavaWeb综合案例笔记目录:(https://www.cnblogs.com/wenjie2000/p/16378441.html)视频教程(P154~P163)功能列表环境搭建执行提供的下面的tb_brand.sql脚本--......
  • 关于Net和Java的一些看法
    两个都是美好的语言,都有良好的生态支持。微软一直想让开发变得简单,导致封装的过度,C#程序员被爱护过头了,导致中级C#程序员只能对应初级Java程序员的,希望随着NET的持续更新,......
  • 【javascript】关于charCodeAt()方法
    在做算法题目leetcode2283时,看见某些答案会使用charCodeAt(),因为自己没用过,所以作此纪录描述在JavaScript中,charCodeAt()是一种字符串方法,用于检索字符串中特定位置的......
  • ERROR: JAVA_HOME is not set and could not be found.
    问题描述:今天在启动Hadoop时发现显示报错:JAVA_HOMEisnotsetandcouldnotbefound。但我昨天明明还是启动成功的,那这不就是见鬼了???  问题解决:......
  • java 重写equals方法 最佳实践 Java@Override equals():当this.getClass()!=o.getClass()
    java重写equals方法最佳实践Java@Overrideequals():当this.getClass()!=o.getClass()失败https://www.5axxw.com/questions/content/dn5jai我在MyClass中有一个@Overrid......
  • 【Java源码解析】如何严谨地重写 equals 方法、getClass 方法与 instanceof 关键词用
    【Java源码解析】如何严谨地重写equals方法、getClass方法与instanceof关键词用法比较https://blog.csdn.net/m0_46360532/article/details/123118780文章目......
  • Java学习路线总结
    认清现实,放弃幻想。(特此感谢动力节点老杜分享出来的学习路线)学习阶段阶段地址第一阶段:JavaSE基础入https://www.cnblogs.com/NorthPoet/p/1702......
  • JavaScript所有内部属性列表 [[Configurable]] 等
    简介据MDN文档所说内部属性是由[[···]]包裹的内容,于是我们去复制ECMA-262标准的所有文字部分,然后用正则统计[[···]]的出现并打印效果代码经老大提醒,这里......
  • Java课程设计之——Web前端设计
    0、团队课程设计1、主要使用的技术HTML/CSSBootstrap5javascript/jQuery/jQuery-UIJSP2、前期调查与设计搜索引擎主要分为两个界面,主界面和搜索结果界面主界面......
  • Java中集合函数--ArrayList和HashMap
    ArrayList集合框架:ArrayList是一个数组队列,提供了相关的添加、删除、修改、遍历等功能先初始化对象:ArrayList<String>list=newArrayList<>();添加元素使用add()方法......