/
ComKafka.php
228 lines (171 loc) · 7.84 KB
/
ComKafka.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
class ComKafka{
CONST PRODUCER_PARTITION_MODE_CONSISTENT = -1;
CONST PRODUCER_PARTITION_MODE_RANDOM = -2;
CONST PRODUCER_REQUEST_ACK = 1;//-1所有replicas同步完成,0不发送响应,1only leader
CONST CONSUMER_MESSAGE_MAX_BYTES = 1048576;
CONST OPERATE_TIMEOUT_MS = 1000;
private $offset_path = "/Users/jinglinhu/Desktop/kafka-offset-data/";
private $topic_name;
private $all_partitions;
private $producer;
private $producer_topic;
private $consumer;
private $queue;
private $consumer_topic;
function __construct($topic_name,$identity,$host_name="kafka"){
$conf = YII::app()->params[$host_name];
if($conf == '' || $topic_name == '' || $identity == ''){
echo "error : no init param";
exit;
}
if(!in_array($identity,array('producer','consumer'))){
echo "error : wrong identity";
exit;
}
$this->topic_name = $topic_name;
//初始化全局配置
$rd_conf = new RdKafka\Conf();
$rd_conf -> set('metadata.broker.list',$conf['host']);
#$rd_conf -> set('socket.keepalive.enable',true);
#$rd_conf -> set('log_level',LOG_DEBUG);//Logging level (syslog(3) levels)
//print_r($rd_conf->dump());exit;
switch ($identity) {
case 'producer':
$rd_conf -> set('compression.codec','none');//Compression codec to use for compressing message sets: none, gzip or snappy;default none
$this->producer = new RdKafka\Producer($rd_conf);
break;
case 'consumer':
$rd_conf -> set('fetch.message.max.bytes',self::CONSUMER_MESSAGE_MAX_BYTES);
$this->consumer = new RdKafka\Consumer($rd_conf);
$rd_topic_conf = new RdKafka\TopicConf();
$back=debug_backtrace();
$back=$back[1];
$group = $this->topic_name."_".$back['class']."_".$back['function'];
$rd_topic_conf -> set('group.id',$group);
$rd_topic_conf -> set('auto.commit.interval.ms',1000);
$rd_topic_conf -> set("offset.store.path", $this->offset_path);
$rd_topic_conf -> set('auto.offset.reset','smallest');
//$rd_topic_conf -> set('offset.store.method','broker');//flie(offset.store.path),broker
//$rd_topic_conf -> set('offset.store.sync.interval.ms',60000);//fsync() interval for the offset file, in milliseconds. Use -1 to disable syncing, and 0 for immediate sync after each write.
$this->consumer_topic = $this->consumer->newTopic($topic_name,$rd_topic_conf);
break;
}
}
//-----------------------------------produce start-----------------------------------
public function produce($message, $partition = self::PRODUCER_PARTITION_MODE_RANDOM , $key = null){
if(!isset($this->producer) || trim($message) == '' || !is_numeric($partition) || $partition < -2 ){
echo "error : produce wrong param";
return false;
}
try {
switch ($partition) {
//一致性hash发送消息
case self::PRODUCER_PARTITION_MODE_CONSISTENT:
if(isset($key) && $key !=''){
$this->setProducerTopic('consistent');
$this->producer_topic-> produce(RD_KAFKA_PARTITION_UA, 0, $message,$key);
}else{
echo "error : produce no key";
exit;
}
break;
//随机发送
case self::PRODUCER_PARTITION_MODE_RANDOM:
$this->setProducerTopic();
$this->producer_topic-> produce(RD_KAFKA_PARTITION_UA, 0, $message);
break;
//指定partition发送
default:
$this->setProducerTopic();
$this->setPartitions($this->producer,$this->producer_topic);
if(!in_array($partition,$this->all_partitions,true)){
echo "error : produce no partition ".$partition." in ".$this->topic_name." (".implode(",",$this->all_partitions).") ";
exit;
}
$this->producer_topic -> produce($partition, 0, $message);
break;
}
} catch (Exception $e) {
echo $e->getMessage();
return false;
}
return true;
}
private function setProducerTopic($mode = ''){
if(!$this->producer_topic){
if(!isset($this->producer)){
echo "error : produce wrong identity";
exit;
}
$rd_topic_conf = new RdKafka\TopicConf();
$rd_topic_conf->set("request.required.acks", self::PRODUCER_REQUEST_ACK);
if($mode == 'consistent'){
$rd_topic_conf -> setPartitioner(RD_KAFKA_MSG_PARTITIONER_CONSISTENT);
}
//print_r($rd_topic_conf->dump());exit;
$this->producer_topic = $this->producer->newTopic($this->topic_name,$rd_topic_conf);
}
}
//-----------------------------------produce end-----------------------------------
//-----------------------------------consume start-----------------------------------
//@todo 存储方式:zk
public function consume($partition=null){
if(!isset($this->consumer_topic) || !isset($this->consumer)){
echo "error : consume wrong";
return false;
}
$this->setQueue($partition);
$msg = $this->queue->consume(self::OPERATE_TIMEOUT_MS);
if(isset($msg)){
if ($msg->err != 0 ) {
echo "topic[$msg->topic_name] - partition[$msg->partition] - offset[$msg->offset] : msg-".$msg->errstr()."\n";
return false;
}
echo "topic[$msg->topic_name] - partition[$msg->partition] - offset[$msg->offset] : msg-";
return $msg->payload;
}
}
private function setQueue($partition){
if(!$this->queue){
$this->queue = $this->consumer -> newQueue();
$this->setPartitions($this->consumer,$this->consumer_topic);
if(is_numeric($partition) && $partition >= 0){
$partition = array($partition);
}
if(is_array($partition)){
foreach ($partition as $p) {
if(!in_array($p,$this->all_partitions,true)){
echo "error : consume no partition ".$partition." in ".$this->topic_name." (".implode(",",$this->all_partitions).") ";
exit;
}
}
$consume_partitions = $partition;
}else{
$consume_partitions = $this->all_partitions;
}
foreach ($consume_partitions as $p) {
$this->consumer_topic -> consumeQueueStart($p, RD_KAFKA_OFFSET_STORED,$this->queue);//RD_KAFKA_OFFSET_STORED
}
}
}
//-----------------------------------consume end-----------------------------------
//-----------------------------------public start-----------------------------------
private function setPartitions($kd_obj,$topic_obj){
if(!$this->all_partitions){
if(!isset($topic_obj) || !isset($kd_obj)){
echo "error : get partitions wrong";
exit;
}
$metadata = $kd_obj->metadata(false,$topic_obj,self::OPERATE_TIMEOUT_MS)->getTopics();
$this->all_partitions =array();
foreach ($metadata as $topic) {
foreach ($topic->getPartitions() as $k => $p) {
$this->all_partitions[] = $p->getId();
}
}
}
}
//-----------------------------------public end-----------------------------------
}
?>