文章目录


  • 前言
  • 一、pandas是什么?
  • 二、使用步骤
  • 1.引入库
  • 2.读入数据
  • 总结


 


前言

  这篇主要讲如何安装kafka+配置SASL安全验证,之前网上一些文章要么没有配置SASL,要么本身存在一些问题,这里主要把正确的配置方案放上来, 后续会讲讲遇到的一些问题和该如何解决。

 

一、安装kafka

官网的quickstart有最新安装教程,想安装最新版可以去官网,目前我使用的是kafka 2.6.0。

这里的所有操作都是在linux服务器上进行的,如果没有linux服务器,自己用虚拟机也行

 

第一步: 下载 

wget https://mirrors.bfsu.edu.cn/apache/kafka/2.6.0/kafka_2.13-2.6.0.tgz

第二步: 解压    并进入文件夹

tar -xzf kafka_2.13-2.6.0.tgz

cd kafka_2.13-2.6.0

第三步 启动zookeeper  高版本kafka自带zookeeper

bin/zookeeper-server-start.sh config/zookeeper.properties

第四步 启动完成后 启动kafka

bin/kafka-server-start.sh config/server.properties

 第五步: 本地测试

1创建topic

bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

2生产者连接kafka

bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092

3消费者连接

bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092

   在生产者端输入任意字符回车,将在消费者端看到相同消息,至此,kafka启动+简单测试成功

 

 

 

这种方式启动会占用控制台,主要是为了方便调试,关闭控制台服务就关闭了。想要一直保持在后台运行,需要使用nohup启动

nohup sh bin/zookeeper-server-start.sh config/zookeeper.properties  >/dev/null 2>&1 &

nohup sh bin/kafka-server-start.sh config/server.properties  >/dev/null 2>&1 &

二、配置 

     走完上面四步,正常情况下kafka就已经启动成功了,不过这种只能在本地访问。想要做到外网访问,需要绑定公网ip 

 

 开始配置:  打开进入kafka目录的config文件夹下, 输入  vim server.properties   将下面配置添加至最下方

#这里一定要用本地ip 
listeners=SASL_PLAINTEXT://172.16.0.17:9092


#这里绑定外网地址
advertised.listeners=SASL_PLAINTEXT://你的公网ip:9092



# 使用的认证协议 
security.inter.broker.protocol=SASL_PLAINTEXT

#SASL机制
sasl.enabled.mechanisms=PLAIN

sasl.mechanism.inter.broker.protocol=PLAIN

# 完成身份验证的类 
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer 
# 如果没有找到ACL(访问控制列表)配置,则允许任何操作
allow.everyone.if.no.acl.found=true
super.users=User:admin

第二步: 配置jaas

#创建kafka jaas文件
touch kafka_server_jaas.conf

#创建zookeeper jaas文件
touch kafka_zookeeper_jaas.conf

将下面配置放入 kafka_server_jaas.conf, 其中username和password是配置zoookeeper内部节点间认证的用户,user_admin="密码";  实际上是创建了一个admin用户 密码为你填写的 “密码”,这个用户用来认证生产者和消费者是否能连接kafka

KafkaServer{
  org.apache.kafka.common.security.plain.PlainLoginModule required
  username="admin"
  password="你的密码"
  user_admin="你的密码";
};

 

将以下配置放入 kafka_zookeeper_jaas.conf, 注意用户名密码和上面配置的要一致

Server{

org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="和上面配置的密码相同"
user_admin="和上面配置的密码相同";
};

 

 

第三步: 启动加载jaas配置文件

进入kafka  bin文件夹    在kafka-server-start.sh 最上方添加    注意将下方的地址改成自己的文件地址!!!

export KAFKA_OPTS=" -Djava.security.auth.login.config=/usr/local/kafka/config/kafka_server_jaas.conf"

在 zookeeper-server-start.sh 最上方添加

export KAFKA_OPTS="-Djava.security.auth.login.config=/usr/local/kafka/config/kafka_zoo_jaas.conf"

 

最后 重启zookeeper和kafka    

 

 

使用java连接kafka

 

导入jar包

<dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>2.6.0</version>
    </dependency>

消息生产者

import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class Producer {
    public static String topic = "neoMessage";

    public static int partition = 1;


    private static Properties producerProperties = new Properties();


    public static void main(String[] args) throws InterruptedException {

        initProperties("ip:9092");

        KafkaProducer<String, String> producer = new KafkaProducer(producerProperties);

        int count = 0;
        for (;;) {
            String message = "my Message record" + count++;

            producer.send(new ProducerRecord(topic, String.valueOf(message.hashCode()), message));
            System.out.println("发送者---------发送消息: " + message);
            //隔1s发一次
            Thread.sleep(1000);
        }

    }

    private static void initProperties(String serverAddress) {
        producerProperties.setProperty("bootstrap.servers", serverAddress);
        producerProperties.put("acks", "all");
        producerProperties.put("retries", "3");
        producerProperties.put("key.serializer", StringSerializer.class);
        producerProperties.put("value.serializer", StringSerializer.class);

        //设置SASL连接
        producerProperties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
        producerProperties.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
        producerProperties.put("sasl.jaas.config",
                "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"你的密码\";");
    }
}

 

 

消费者代码

import org.apache.kafka.clients.CommonClientConfigs;
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.config.SaslConfigs;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;

public class Customer {
    public static String topic = "neoMessage";

    private static Properties customerProperties = new Properties();



    public static void main(String[] args) {
        initProperties("ip:9092");

        KafkaConsumer<String, String> consumer = new KafkaConsumer(customerProperties);
        consumer.subscribe(Collections.singleton(topic));



        while(true) {
            ConsumerRecords<String, String> msgs = consumer.poll(Duration.ofSeconds(1));
            Iterator iterator = msgs.iterator();

            while(iterator.hasNext()) {
                ConsumerRecord msg = (ConsumerRecord)iterator.next();

                System.out.println("消费者-------------收到消息: " + msg.value());

            }
        }
    }


    private static void initProperties(String serverAddress) {
        customerProperties.setProperty("bootstrap.servers", serverAddress);
        customerProperties.put("key.deserializer", StringDeserializer.class);
        customerProperties.put("value.deserializer", StringDeserializer.class);
        customerProperties.put("group.id", "group1");

        //设置SASL连接
        customerProperties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
        customerProperties.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
        customerProperties.put("sasl.jaas.config",
                "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"你的密码\";");

    }

}

先启动生产者   在启动消费者。   此demo仅适合演示  请勿用于生产