introduce-kafka

来源:互联网 发布:lol网络不稳定断开连接 编辑:程序博客网 时间:2024/06/11 14:12

介绍:

a high-throughput distributed messaging system.

基本模型

基本模型

模型组成元素

  1. Kafka maintains feeds of messages in categories called topics.
  2. We’ll call processes that publish messages to a Kafka topic producers.
  3. We’ll call processes that subscribe to topics and process the feed of published messages consumers..
  4. Kafka is run as a cluster comprised of one or more servers each of which is called a broker.

partition

Kafka does it better. By having a notion of parallelism—the partition—within the topics, Kafka is able to provide both ordering guarantees and load balancing over a pool of consumer processes. This is achieved by assigning the partitions in the topic to the consumers in the consumer group so that each partition is consumed by exactly one consumer in the group. By doing this we ensure that the consumer is the only reader of that partition and consumes the data in order. Since there are many partitions this still balances the load over many consumer instances. Note however that there cannot be more consumer instances than partitions.

Kafka only provides a total order over messages within a partition, not between different partitions in a topic. Per-partition ordering combined with the ability to partition data by key is sufficient for most applications. However, if you require a total order over messages this can be achieved with a topic that has only one partition, though this will mean only one consumer process.

producer

$message相当于key,kafka会对\$message进行平衡处理,分配到partition上。
指定的两个参数,一个是key, 一个是topic

<?php$host = '';$port = '';$topic = 'test';$producer = new Kafka_Producer($host, $port, $topic);$message = fopen('php://stdin', 'r');$byte = $producer->send($message, $topic);?>

Consumer

pull模式,从服务器拿消息来进行处理。

拿消息的时候参数指定了:topic , partition, offset,可以看出,消息在partition存放的模式。

$host = '';$port = '';$topic = 'test';$maxSize = 1000;$sockTimeout = 5;$offset = 0;$partition = 0;$consumer = new Kafka_SimpleConsumer($host, $port, $sockTimeout, $maxSize);$fetchRequest = new Kafka_FetchRequest($topic, $partition, $offset, $maxSize);$partialOffset = 0;$message = $consumer->fetch($fetchRequest);

offset

参考的博客地址:

http://www.cnblogs.com/metoy/p/4452124.html
http://www.tuicool.com/articles/zIzyq2#0-tsina-1-48639-397232819ff9a47a7b7e80a40613cfe1

0 0