windows搭建Kafka集群并整合springboot测试

一:搭建kafka集群

1. 创建zookeeper在某一个network:

docker run -d --name zookeeper -p 2181:2181 --network docker_network --ip 172.0.0.10 wurstmeister/zookeeper

2.创建Kafka集群与zookeeper在同一网络:(XXX为本机电脑ip)

docker run -d --name kafka0 --publish 9092:9092 --network docker_network --ip 172.0.0.11 -e KAFKA_BROKER_ID=0 --link zookeeper --env KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://XXX:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 wurstmeister/kafka
docker run -d --name kafka1 --publish 9093:9093 --network docker_network --ip 172.0.0.12 -e KAFKA_BROKER_ID=1 --link zookeeper --env KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://XXX:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 wurstmeister/kafka
docker run -d --name kafka2 --publish 9094:9094 --network docker_network --ip 172.0.0.13 -e KAFKA_BROKER_ID=2 --link zookeeper --env KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://XXX:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 wurstmeister/kafka

3.进入某一个kafka:

docker exec -it kafka0 /bin/bash

4.创建主题:

/opt/kafka/bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 3 --partitions 3 --topic TestTopic

5.查看主题:

/opt/kafka/bin/kafka-topics.sh --describe --zookeeper zookeeper:2181 --topic TestTopic

6.测试集群是否有效:

生产端发送消息
docker exec -it kafka0 /bin/bash
/opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic TestTopic
消费端接收消息(新开端口 或者用offsetexplorer查看)
docker exec -it kafka2 /bin/bash
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9094 --topic TestTopic

删除主题:进入zookeeper,启动bin/zkCli.sh

执行:delete /config/topics/TestTopic
rmr /brokers/topics/TestTopic

查看配置文件

cat /opt/kafka/config/server.properties
如果有问题 可复制粘贴配置文件 修改 docker cp d:/home/test/server.properties kafka0:/opt/kafka_2.13-2.8.1/config/server.properties

二:springboot整合kafka集群

1 pom文件中添加maven引用

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

2 yml文件中增加kafka生产者的配置

server:
  port: 8080
spring:
  kafka:
    bootstrap-servers: 192.168.242.243:9092,192.168.242.243:9093,192.168.242.243:9094
    producer: # 生产者
      retries: 0 # 设置大于0的值,则客户端会将发送失败的记录重新发送
      batch-size: 16384 # 每次发送时多少一批次 这里设置的是16kb
      buffer-memory: 33554432 # 设置内存缓存区32Mb
      acks: all # leader收到消息后就返回ack
      # 指定消息key和消息体的编解码方式
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
      # 连接超时时间
      properties:
        request.timeout.ms: 30000
    consumer:
      group-id: test-consumer-group # 组内单播,组间广播
      enable-auto-commit: false # 关闭消费自动提交
      auto-offset-reset: latest # 新消费组启动会从头信息消费
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      max-poll

你可能感兴趣的:(kafka,windows,spring,boot,docker)