• kafka伪集群部署,使用zookeeper模式


    1:拉去管理kafka界面UI镜像

    docker pull provectuslabs/kafka-ui
    
    • 1

    2:拉去管理kafka镜像

    docker pull bitnami/kafka
    
    • 1

    3:docker-compose.yml

    version: '3.8'
    services:
      zookeeper-1:
        container_name: zookeeper1
        image: bitnami/zookeeper
        ports:
          - "2181:2181"
        environment:
          - ALLOW_ANONYMOUS_LOGIN=yes
    
      kafka-1:
        container_name: kafka1
        image: bitnami/kafka	
        ports:
          - "19092:19092"
    
        environment:
          - KAFKA_CFG_ZOOKEEPER_CONNECT=192.168.11.50:2181
          - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.11.50:19092
          - KAFKA_CFG_LISTENERS=PLAINTEXT://:19092
          - ALLOW_PLAINTEXT_LISTENER=yes
          - KAFKA_BROKER_ID=1
          
      kafka-2:
        container_name: kafka2
        image: bitnami/kafka
        ports:
          - "29092:29092"
          
    
        environment:
          - KAFKA_CFG_ZOOKEEPER_CONNECT=192.168.11.50:2181
          - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.11.50:29092
          - KAFKA_CFG_LISTENERS=PLAINTEXT://:29092
          - ALLOW_PLAINTEXT_LISTENER=yes
          - KAFKA_BROKER_ID=2 
        depends_on:
          - kafka-1
          
      kafka-3:
        container_name: kafka3
        image: bitnami/kafka
        ports:
          - "39092:39092"
        
        environment:
          - KAFKA_CFG_ZOOKEEPER_CONNECT=192.168.11.50:2181
          - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.11.50:39092
          - KAFKA_CFG_LISTENERS=PLAINTEXT://:39092
          - ALLOW_PLAINTEXT_LISTENER=yes
          - KAFKA_BROKER_ID=3
          
        depends_on:
          - kafka-1
          - kafka-2
    
      kafka-ui:
        container_name: kafka-ui
        image: provectuslabs/kafka-ui
        ports:
          - "8080:8080"
        environment:
          - KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=192.168.11.50:19092,192.168.11.50:29092,192.168.11.50:39092
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    4:springboot项目发布和消费kafka

    4-1:application.yml

    server:
      port: 9088
    
    spring:
      kafka:
        consumer:
          bootstrap-servers: localhost:19092,localhost:29093,localhost:39092
          group-id: test-group
          auto-offset-reset: earliest
        producer:
          bootstrap-servers: localhost:19092,localhost:29092,localhost:39092
          key-serializer: org.apache.kafka.common.serialization.StringSerializer
          value-serializer: org.apache.kafka.common.serialization.StringSerializer
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4-2:消费者

    package com.example.kafkademo.config;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.kafka.annotation.KafkaListener;
    import org.springframework.stereotype.Service;
    
    /**
     * @Author xu
     * @create 2023/9/27 19
     */
    @Service
    public class KafkaConsumerService {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConsumerService.class);
    
        @KafkaListener(topics = "topic")
        public void receiveMessage(String message) {
            LOGGER.info("received message='{}'", message);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    4-3:生产者

    package com.example.kafkademo.config;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.kafka.core.KafkaTemplate;
    import org.springframework.stereotype.Service;
    
    /**
     * @Author xu
     * @create 2023/9/27 19
     */
    @Service
    public class KafkaProducerService {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(KafkaProducerService.class);
    
        @Autowired
        private KafkaTemplate<String, String> kafkaTemplate;
    
        public void sendMessage(String topic, String message) {
            LOGGER.info("sending message='{}' to topic='{}'", message, topic);
            kafkaTemplate.send(topic, message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4-4:controller

    package com.example.kafkademo.controller;
    
    import com.example.kafkademo.config.KafkaProducerService;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    /**
     * @Author xu
     * @create 2023/9/27 19
     */
    @RestController
    public class KafkaController {
        @Resource
        KafkaProducerService kafkaProducerService;
    
        @PostMapping("/publish")
        public String publish(String topic,String content){
            kafkaProducerService.sendMessage("topic",content);
            System.out.println("content");
            return content;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4-5:pom

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.16</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>kafkaDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>kafkaDemo</name>
        <description>kafkaDemo</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.kafka</groupId>
                <artifactId>spring-kafka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.kafka</groupId>
                <artifactId>spring-kafka-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    5:命令行方式启动kafka

    docker run -d --name kafka2 -p 19092:19092 -e KAFKA_CFG_ZOOKEEPER_CONNECT=192.168.11.50:2181 -e KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.11.50:19092  -e KAFKA_CFG_LISTENERS=PLAINTEXT://:19092 -e ALLOW_PLAINTEXT_LISTENER=yes -e KAFKA_BROKER_ID=2 bitnami/kafka
    
    • 1

    6:命令行方式启动kafka-ui

    docker run -d --name kafka-ui -p 8080:8080 -e KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=192.168.11.50:19092 provectuslabs/kafka-ui
    
    • 1

    7:命令行方式启动zookeeper

    docker run -d --name zookeeper -p 2181:2181 -e ALLOW_ANONYMOUS_LOGIN=yes bitnami/zookeeper
    
    • 1
  • 相关阅读:
    Git 不要只会 pull 和 push,学学这 5 条提高效率的命令(下)
    学习ros机器人导航从精读nav2导航launch文件开始
    武汉高性能计算大会2022举办,高性能计算生态发展再添新动力
    QT 气泡消息绘制
    Leetcode—2300.咒语和药水的成功对数【中等】
    深入分析Java的序列化与反序列化
    练习八-利用有限状态机进行时序逻辑的设计
    SpringBoot配置文件
    【Python】常用工具包系列1 —— Numpy
    逆向第一课---安装ADB工具,并使用夜神模拟器连接
  • 原文地址:https://blog.csdn.net/qq_19891197/article/details/133362998