KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.1.20:9092
KAFKA_ADVERTISED_PORT: 9092
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_PORT: 9092
KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "false"
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_HEAP_OPTS: "-Xmx512M -Xmx512M"
kafka-ui:
image: provectuslabs/kafka-ui:latest
container_name: kafka-ui
restart: always
ports:
- 10010:8080
environment:
- DYNAMIC_CONFIG_ENABLED=true
- SERVER_SERVLET_CONTEXT_PATH=/ui-kafka
- KAFKA_CLUSTERS_0_NAME=local
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9092
- KAFKA_CLUSTERS_0_PROPERTIES_SECURITY_PROTOCOL=PLAINTEXT
depends_on:
- zookeeper
- kafka
### kafka-ui
地址:[http://localhost:10010/ui-kafka/]( )
![](https://img-blog.csdnimg.cn/direct/0a798ceb92e64ce2aad64c9f33c2b49e.png)
### java生产者
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.9.0</version>
</dependency>
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;
import java.util.Properties;
public class KafkaProducerTest {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
// KEY: 是kafka用于做消息投递计算具体投递到对应的主题的哪一个partition而需要的
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// VALUE: 实际发送消息的内容
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// 2.创建kafka生产者对象 传递properties属性参数集合
KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
for (int i = 0; i < 100; i++) {
ProducerRecord<String, String> record = new ProducerRecord<String, String>("topic_test", i + " : testx123测试");
// 4.发送消息
producer.send(record);
System.out.println("发送成功: " + i);
}
// 5.关闭生产者
producer.close();
}
}
### java消费者
public class KafkaConsumerTest {
public static void main(String[] args) {
Properties props = new Properties();
// bootstrap.servers:kafka服务器地址,多个用逗号隔开
props.put("bootstrap.servers", "127.0.0.1:9092");
props.put("group.id", "topic-test-group"); // 消费组groupId
props.put("auto.offset.reset", "earliest");
// 序列化方式
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("topic_test")); // 订阅的topic
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000L));
for (ConsumerRecord<String, String> record : records) {
System.out.printf("主题 = %s, 分区 = %d, 位移 = %d, " + "消息键 = %s, 消息值 = %s\n",
record.topic(), record.partition(), record.offset(), record.key(), record.value());
}
if (!records.isEmpty()) {
try {
// 提交消费位移
consumer.commitSync();
} catch (CommitFailedException exception) {
System.out.println("commit failed....");
}
}
}
}
}
## 二.SASL模式部署kafka
解释: SASL(Simple Authentication and Security Layer)的配置
目录结构如下:C:/docker/kafka1/
![](https://img-blog.csdnimg.cn/direct/df4f0e6b32474feba855edeefbc3f3a7.png)
### server\_jaas.conf配置
需要新建文件:server\_jaas.conf,前两个是zk配置,后两个是kafka配置
Client {
org.apache.zookeeper.server.auth.DigestLoginModule required
username=“admin”
password=“123456”;
};
Server {
org.apache.zookeeper.server.auth.DigestLoginModule required
username=“admin”
password=“123456”
user_super=“123456”
user_admin=“123456”;
};
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=“admin”
password=“123456”
user_admin=“123456”;
};
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=“admin”
password=“123456”;
};
### zoo.cfg配置
其他的没改变,就是最后添加的四行
The number of milliseconds of each tick
tickTime=2000
The number of ticks that the initial
synchronization phase can take
initLimit=10
The number of ticks that can pass between
sending a request and getting an acknowledgement
syncLimit=5
the directory where the snapshot is stored.
do not use /tmp for storage, /tmp here is just
example sakes.
dataDir=/opt/zookeeper-3.4.13/data
the port at which the clients will connect
clientPort=2181
the maximum number of client connections.
increase this if you need to handle more clients
#maxClientCnxns=60
Be sure to read the maintenance section of the
administrator guide before turning on autopurge.
http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
The number of snapshots to retain in dataDir
autopurge.snapRetainCount=3
Purge task interval in hours
Set to “0” to disable auto purge feature
autopurge.purgeInterval=1
开启SASl关键配置
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
requireClientAuthScheme=sasl
jaasLoginRenew=3600000
zookeeper.sasl.client=true
### docker-compose.yml
版本根据你的docker版本来的,目前主流应该都是3.几的版本
version: ‘3.8’
services:
zookeeper:
image: wurstmeister/zookeeper
volumes:
- C:/docker/kafka1/secrets/:/opt/secrets/
- C:/docker/kafka1/zookeeper/zoo.cfg:/opt/zookeeper-3.4.13/conf/zoo.cfg
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
SERVER_JVMFLAGS: -Djava.security.auth.login.config=/opt/secrets/server_jaas.conf
ports:
- 2181:2181
restart: always
kafka:
image: wurstmeister/kafka
container_name: kafka
depends_on:
- zookeeper
ports:
- 9092:9092
volumes:
- C:/docker/kafka1/secrets/:/opt/secrets/
environment:
KAFKA_BROKER_ID: 0
KAFKA_ADVERTISED_LISTENERS: SASL_PLAINTEXT://192.168.1.20:9092
KAFKA_ADVERTISED_PORT: 9092
KAFKA_LISTENERS: SASL_PLAINTEXT://0.0.0.0:9092
KAFKA_SECURITY_INTER_BROKER_PROTOCOL: SASL_PLAINTEXT
KAFKA_PORT: 9092
KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL: PLAIN
KAFKA_SASL_ENABLED_MECHANISMS: PLAIN
KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.auth.SimpleAclAuthorizer
KAFKA_SUPER_USERS: User:admin
KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: “true” #设置为true,ACL机制为黑名单机制,只有黑名单中的用户无法访问,默认为false,ACL机制为白名单机制,只有白名单中的用户可以访问
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_HEAP_OPTS: “-Xmx512M -Xms16M”
KAFKA_OPTS: -Djava.security.auth.login.config=/opt/secrets/server_jaas.conf
kafka-ui:
image: provectuslabs/kafka-ui:latest
container_name: kafka-ui
restart: always
ports:
- 10010:8080
environment:
- DYNAMIC_CONFIG_ENABLED=true
- SERVER_SERVLET_CONTEXT_PATH=/kafka-ui
- KAFKA_CLUSTERS_0_NAME=local
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9092
- KAFKA_CLUSTERS_0_PROPERTIES_SECURITY_PROTOCOL=SASL_PLAINTEXT
- KAFKA_CLUSTERS_0_PROPERTIES_SASL_MECHANISM=PLAIN
- KAFKA_CLUSTERS_0_PROPERTIES_SASL_JAAS_CONFIG=org.apache.kafka.common.security.scram.ScramLoginModule required username=“admin” password=“123456”;
depends_on:
- zookeeper
- kafka
###
这是一个Docker Compose文件,用于定义和运行多个Docker容器的应用。我会为你详细解释这个文件的内容:
1. **版本**:
* `version: '3.8'`: 指定了Docker Compose的版本为3.8。
2. **services**:
* 定义了两个服务,分别是`zookeeper`和`kafka`。
3. **zookeeper**:
* 使用`wurstmeister/zookeeper`镜像来创建容器。
* 挂载了两个卷:一个是本地的`C:/docker/kafka1/secrets/`目录到容器内的`/opt/secrets/`目录,另一个是本地的`C:/docker/kafka1/zookeeper/zoo.cfg`文件到容器内的`/opt/zookeeper-3.4.13/conf/zoo.cfg`文件。
* 设置容器的名称为`zookeeper`。
* 设置环境变量:如`ZOOKEEPER_CLIENT_PORT`, `ZOOKEEPER_TICK_TIME`, `SERVER_JVMFLAGS`等。
* 映射容器的2181端口到主机的2181端口。
* 设置容器在退出后总是重启。
4. **kafka**:
* 使用`wurstmeister/kafka`镜像来创建容器。
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/a37bc53a3c926173068009556e2b47a8.png)
![img](https://img-blog.csdnimg.cn/img_convert/0feb5eb9b632d7fbc24feab502df3a75.png)
![img](https://img-blog.csdnimg.cn/img_convert/b35361d95fc0d17fa1615d4c5b2504da.png)
![img](https://img-blog.csdnimg.cn/img_convert/38788f7094fbe7bf9eb2205d28a7a792.png)
![img](https://img-blog.csdnimg.cn/img_convert/daee6b71d7ac000f95d298f7dd2fc9bf.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)**
![img](https://img-blog.csdnimg.cn/img_convert/07e7c498e503062368c54490842f7190.png)
**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)**
[外链图片转存中...(img-2aOF3gF4-1712956241490)]
**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**