天天看點

kafka叢集及與springboot內建

大家好,又見面了,我是你們的朋友全棧君。

linux搭建,kafkao3節點虛拟機為CentOS6,ip為192.168.1.128,192.168.1.129和192.168.1.130,域名分别為master,worker1,worker2

1. 叢集
#192.168.1.128
  [root@master local]# cd /home/gilbert/app/rar/
  [root@master rar]# tar zxvf kafka_2.10-0.10.2.0.tgz
  [root@master rar]# mv kafka_2.10-0.10.2.0 /home/gilbert/app/kafka
  ​           

複制

配置檔案路徑:kafka/config/server.properties

kafka叢集及與springboot內建

修改配置檔案

[root@master config]# more server.properties 
  # Licensed to the Apache Software Foundation (ASF) under one or more
  # contributor license agreements.  See the NOTICE file distributed with
  # this work for additional information regarding copyright ownership.
  # The ASF licenses this file to You under the Apache License, Version 2.0
  # (the "License"); you may not use this file except in compliance with
  # the License.  You may obtain a copy of the License at
  #
  #    http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ​
  # see kafka.server.KafkaConfig for additional details and defaults
  ​
  ############################# Server Basics #############################
  ​
  # The id of the broker. This must be set to a unique integer for each broker.
  ##每一個broker在叢集中的唯一标示,要求是正數。在改變IP位址,不改變broker.id的話不會影響consumers
  broker.id=0
  ​
  # Switch to enable topic deletion or not, default value is false
  ## 是否允許自動建立topic ,若是false,就需要通過指令建立topic
  delete.topic.enable=true
  ​
  ############################# Socket Server Settings #############################
  ​
  # The address the socket server listens on. It will get the value returned from 
  # java.net.InetAddress.getCanonicalHostName() if not configured.
  #   FORMAT:
  #     listeners = listener_name://host_name:port
  #   EXAMPLE:
  #     listeners = PLAINTEXT://your.host.name:9092
  #listeners=PLAINTEXT://:9092
  ##提供給用戶端響應的端口
  port=9092
  host.name=192.168.1.128
  ​
  # Hostname and port the broker will advertise to producers and consumers. If not set, 
  # it uses the value for "listeners" if configured.  Otherwise, it will use the value
  # returned from java.net.InetAddress.getCanonicalHostName().
  #advertised.listeners=PLAINTEXT://your.host.name:9092
  ​
  # Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
  #listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
  ​
  # The number of threads handling network requests
  ## broker 處理消息的最大線程數,一般情況下不需要去修改
  num.network.threads=3
  ​
  # The number of threads doing disk I/O
  ## broker處理磁盤IO 的線程數 ,數值應該大于你的硬碟數
  num.io.threads=8
  ​
  # The send buffer (SO_SNDBUF) used by the socket server
  ## socket的發送緩沖區,socket的調優參數SO_SNDBUFF
  socket.send.buffer.bytes=102400
  ​
  # The receive buffer (SO_RCVBUF) used by the socket server
  ## socket的接受緩沖區,socket的調優參數SO_RCVBUFF
  socket.receive.buffer.bytes=102400
  ​
  # The maximum size of a request that the socket server will accept (protection against OOM)
  ## socket請求的最大數值,防止serverOOM,message.max.bytes必然要小于socket.request.max.bytes,會被topic建立時的指定參數覆寫
  socket.request.max.bytes=104857600
  ​
  ​
  ############################# Log Basics #############################
  ​
  # A comma seperated list of directories under which to store log files
  ##kafka資料的存放位址,多個位址的話用逗号分割/data/kafka-logs-1,/data/kafka-logs-2
  log.dirs=/tmp/kafka-logs
  ​
  # The default number of log partitions per topic. More partitions allow greater
  # parallelism for consumption, but this will also result in more files across
  # the brokers.
  ##每個topic的分區個數,若是在topic建立時候沒有指定的話會被topic建立時的指定參數覆寫
  num.partitions=1
  ​
  # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
  # This value is recommended to be increased for installations with data dirs located in RAID array.
  ##我們知道segment檔案預設會被保留7天的時間,逾時的話就
  ##會被清理,那麼清理這件事情就需要有一些線程來做。這裡就是
  ##用來設定恢複和清理data下資料的線程數量
  num.recovery.threads.per.data.dir=1
  ​
  ############################# Log Flush Policy #############################
  ​
  # Messages are immediately written to the filesystem but by default we only fsync() to sync
  # the OS cache lazily. The following configurations control the flush of data to disk.
  # There are a few important trade-offs here:
  #    1. Durability: Unflushed data may be lost if you are not using replication.
  #    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
  #    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.
  # The settings below allow one to configure the flush policy to flush data after a period of time or
  # every N messages (or both). This can be done globally and overridden on a per-topic basis.
  ​
  # The number of messages to accept before forcing a flush of data to disk
  #log.flush.interval.messages=10000
  ​
  # The maximum amount of time a message can sit in a log before we force a flush
  #log.flush.interval.ms=1000
  ​
  ############################# Log Retention Policy #############################
  ​
  # The following configurations control the disposal of log segments. The policy can
  # be set to delete segments after a period of time, or after a given size has accumulated.
  # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
  # from the end of the log.
  ​
  # The minimum age of a log file to be eligible for deletion due to age
  ##segment檔案保留的最長時間,預設保留7天(168小時),
  ##逾時将被删除,也就是說7天之前的資料将被清理掉。
  log.retention.hours=168
  ​
  # A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
  # segments don't drop below log.retention.bytes. Functions independently of log.retention.hours.
  #log.retention.bytes=1073741824
  ​
  # The maximum size of a log segment file. When this size is reached a new log segment will be created.
  ###日志檔案中每個segment的大小,預設為1G
  log.segment.bytes=1073741824
  ​
  # The interval at which log segments are checked to see if they can be deleted according
  # to the retention policies
  ##上面的參數設定了每一個segment檔案的大小是1G,那麼
  ##就需要有一個東西去定期檢查segment檔案有沒有達到1G,
  ##多長時間去檢查一次,就需要設定一個周期性檢查檔案大小
  ##的時間(機關是毫秒)。
  log.retention.check.interval.ms=300000
  ​
  ############################# Zookeeper #############################
  ​
  # Zookeeper connection string (see zookeeper docs for details).
  # This is a comma separated host:port pairs, each corresponding to a zk
  # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
  # You can also append an optional chroot string to the urls to specify the
  # root directory for all kafka znodes.
  #zookeeper.connect=localhost:2181
  ##消費者叢集通過連接配接Zookeeper來找到broker。
  ##zookeeper連接配接伺服器位址
  zookeeper.connect=master:2181,worker1:2181,worker2:2181
  ​
  # Timeout in ms for connecting to zookeeper
  zookeeper.connection.timeout.ms=6000           

複制

啟動kafka                   

複制

複制

[root@master kafka]# ./bin/kafka-server-start.sh config/server.properties &
  [2018-06-25 02:31:21,931] INFO KafkaConfig values: 
          advertised.host.name = null
          advertised.listeners = null
          advertised.port = null
          authorizer.class.name = 
          auto.create.topics.enable = true
          auto.leader.rebalance.enable = true
          background.threads = 10
          broker.id = 0
          broker.id.generation.enable = true
          broker.rack = null
          compression.type = producer
          connections.max.idle.ms = 600000
          controlled.shutdown.enable = true
          controlled.shutdown.max.retries = 3
          controlled.shutdown.retry.backoff.ms = 5000
          controller.socket.timeout.ms = 30000           

複制

建立topic

#建立topic topic名字為gilbert
  [root@master kafka]# ./bin/kafka-topics.sh --create --zookeeper master:2181,worker1:2181,worker2:2181 --replication-factor 3 --partitions 3 --topic gilbert
  Created topic "gilbert".           

複制

檢視topic

[root@master kafka]# ./bin/kafka-topics.sh --describe --zookeeper master:2181,worker1:2181,worker2:2181 --topic gilbert
  Topic:gilbert   PartitionCount:3        ReplicationFactor:3     Configs:
          Topic: gilbert  Partition: 0    Leader: 2       Replicas: 2,0,1 Isr: 2,0,1
          Topic: gilbert  Partition: 1    Leader: 0       Replicas: 0,1,2 Isr: 0,1,2
          Topic: gilbert  Partition: 2    Leader: 1       Replicas: 1,2,0 Isr: 1,2,0
            
  [root@master kafka]# ./bin/kafka-topics.sh --list --zookeeper master:2181,worker1:2181,worker2:2181
  gilbert
  test           

複制

建立producer

./bin/kafka-console-producer.sh --broker-list master:9092 -topic gilbert           

複制

建立consumer,分别在3台伺服器上執行建立消費者

#192.168.1.128伺服器 
  [root@master kafka]# ./bin/kafka-console-consumer.sh --zookeeper master:2181,worker1:2181,worker2:2181 -topic gilbert --from-beginning
  Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
    
  #192.168.1.129伺服器 
  [root@worker1 kafka_2.10-0.10.2.0]# ./bin/kafka-console-consumer.sh --zookeeper master:2181,worker1:2181,worker2:2181 -topic gilbert --from-beginning
  Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
  ​
  #192.168.1.130伺服器 
  [root@worker2 kafka_2.10-0.10.2.0]#  ./bin/kafka-console-consumer.sh --zookeeper master:2181,worker1:2181,worker2:2181 -topic gilbert --from-beginning
  Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
  ​
  ​           

複制

在#192.168.1.128伺服器上生産者控制台輸入:hello kafka進行測試

kafka叢集及與springboot內建

在3台伺服器上的消費者都正常接收到消息

kafka叢集及與springboot內建
kafka叢集及與springboot內建
kafka叢集及與springboot內建

删除topic

[root@master kafka]# ./bin/kafka-topics.sh --delete --zookeeper master:2181,worker1:2181,worker2:2181 --topic test
  Topic test is marked for deletion.
  Note: This will have no impact if delete.topic.enable is not set to true           

複制

springboot內建kafka

1.生産者kafka-producer
kafka叢集及與springboot內建

a) pom檔案

<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>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>           

複制

b) yml配置檔案,本例為kafka3節點叢集

spring:
    kafka:
      bootstrap-servers: http://master:9092,http://worker1:9092,http://worker2:9092
      producer:
        retries: 0
        batch-size: 16384
        buffer-memory: 33554432
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.apache.kafka.common.serialization.StringSerializer           

複制

c) message消息實體類

@Data
  public class Message {
 
 
      private Long id;    //id
      private String msg; //消息
      private Date sendTime;  //時間戳
  }           

複制

d) 生産者

@Component
  @Slf4j
  public class KafkaProducer {
 
 
  ​
      @Autowired
      private KafkaTemplate<String, String> kafkaTemplate;
  ​
      private Gson gson = new GsonBuilder().create();
  ​
      //發送消息方法
      public void send() {
 
 
          Message message = new Message();
          message.setId(System.currentTimeMillis());
          message.setMsg(UUID.randomUUID().toString());
          message.setSendTime(new Date());
          log.info("+++++++++++++++++++++  message = {}", gson.toJson(message));
          //topic-ideal為主題
          kafkaTemplate.send("topic-ideal", gson.toJson(message));
      }
  }           

複制

e) 測試類,運作kafkaProducer方法即可

@RunWith(SpringRunner.class)
  @SpringBootTest
  public class KafkaProducerApplicationTests {
 
 
  ​
      @Autowired
      private KafkaProducer kafkaProducer;
  ​
      @Test
      public void kafkaProducer(){
 
 
          this.kafkaProducer.send();
      }
  ​
      @Test
      public void contextLoads() {
 
 
      }
  ​
  }           

複制

2. 消費者kafka-consumer
kafka叢集及與springboot內建

a) pom檔案

<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>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>           

複制

b) yml配置檔案

server:
    port: 9999
  spring:
    kafka:
      bootstrap-servers: http://master:9092,http://worker1:9092,http://worker2:9092
      consumer:
        group-id: ideal-consumer-group
        auto-offset-reset: earliest
        enable-auto-commit: true
        auto-commit-interval: 20000
        key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
        value-deserializer: org.apache.kafka.common.serialization.StringDeserializer           

複制

c) 消費者類

@Component
  @Slf4j
  public class KafkaConsumer {
 
 
  ​
      @KafkaListener(topics = {
 
 "topic-ideal"})
      public void consumer(ConsumerRecord<?, ?> record){
 
 
          Optional<?> kafkaMessage = Optional.ofNullable(record.value());
          if (kafkaMessage.isPresent()) {
 
 
              Object message = kafkaMessage.get();
              log.info("----------------- record =" + record);
              log.info("------------------ message =" + message);
          }
      }
  }           

複制

運作消費者kafka-consumer,再運作kafka-producer工程測試類KafkaProducerApplicationTests中kafkaProducer()方法,可以看到消費者背景正常接收消息

kafka叢集及與springboot內建
kafka叢集及與springboot內建

釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/129939.html原文連結:https://javaforall.cn