-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo.js
236 lines (207 loc) · 6.09 KB
/
demo.js
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
229
230
231
232
233
234
235
236
'use strict';
const path = require('path');
const Client = require('./lib/client');
const Consumer = require('./lib/consumer');
const Message = require('./lib/message');
const Producer = require('./lib/producer');
const Subscription = require('./lib/subscription');
const createEventsProxy = require('eventsproxy');
const {
CLIENT_READY,
PRODUCER_READY,
CONSUMER_READY,
SUBSCRIPTION_READY,
TOPICS_READY,
CONSUMER_CONNECT,
PRINT_LOGGER,
PRINT_ERROR_LOGGER,
} = require('./lib/definition');
const ep = createEventsProxy();
const app = {
config: {},
};
app.config.baseDir = __dirname;
app.config.kafkaNode = {
kafkaHost: '127.0.0.1:9092',
clientOption: {},
consumerOption: [{
groupId: 'group1',
topics: [ 'testTopic1' ],
options: {
fetchMaxWaitMs: 100,
fetchMinBytes: 1,
fetchMaxBytes: 1024 * 1024,
}, // 每个消费组对应的相关 consumerGroup 配置
}, {
groupId: 'group2',
topics: [ 'testTopic2' ],
options: {},
}, {
groupId: 'group3',
topics: [ 'testTopic3' ],
}],
producerOption: {
requireAcks: 1,
ackTimeoutMs: 100,
partitionerType: 2,
autoCreateTopic: true,
topics: [ 'testTopic1', 'testTopic2', 'testTopic3' ],
},
messageOption: {
partition: 0,
attributes: 0,
},
};
const ClientOp = (kafkaHost, clientOption = {}) => {
const option = Object.assign({}, {
kafkaHost,
}, clientOption);
const client = Client(option);
ep.emit(CLIENT_READY, client);
ep.emit(PRINT_LOGGER, '[egg-kafka-node] client ready!');
};
const ProducerOp = (client, producerOption) => {
const producer = Producer(client);
const {
autoCreateTopic,
topics,
} = producerOption;
producer.on('ready', () => {
ep.emit(PRODUCER_READY, producer);
ep.emit(PRINT_LOGGER, '[egg-kafka-node] producer ready!');
if (autoCreateTopic && topics) {
producer.createTopics(topics, false, err => {
if (err) {
ep.emit(PRINT_ERROR_LOGGER, '[egg-kafka-node] create topics error!');
ep.emit(PRINT_ERROR_LOGGER, err);
return;
}
ep.emit(TOPICS_READY, topics);
ep.emit(PRINT_LOGGER, '[egg-kafka-node] create topics success!');
});
}
});
producer.on('error', error => {
ep.emit(PRINT_ERROR_LOGGER, '[egg-kafka-node] producer connect error!');
ep.emit(PRINT_ERROR_LOGGER, error);
});
};
const SubscriptionOp = (baseReadDir, consumerOption) => {
const topicSubscription = Subscription(baseReadDir, consumerOption);
ep.emit(SUBSCRIPTION_READY, topicSubscription);
ep.emit(PRINT_LOGGER, '[egg-kafka-node] load consumer subscription success');
};
const ConsumerOp = (config, topicSubscription) => {
const consumerMemory = Consumer(config);
// 等待所有consumer连接
const unregister = ep.wait(CONSUMER_CONNECT, () => {
ep.emit(PRINT_LOGGER, '[egg-kafka-node] all consumer conncect success!');
unregister && unregister();
}, consumerMemory.length);
consumerMemory.forEach(consumer => {
consumer.on('connect', () => {
ep.emit(CONSUMER_CONNECT);
});
consumer.on('message', message => {
const {
topic,
key,
} = message;
consumer.pause();
const Subscriber = topicSubscription.get(`${topic}:${key}`);
if (!Subscriber) return consumer.resume();
const subcriber = new Subscriber();
subcriber
.subscribe(message)
.then(() => {
consumer.commit(err => {
consumer.resume();
if (err) {
ep.emit(PRINT_ERROR_LOGGER, '[egg-kafka-node] commit offset error!');
ep.emit(PRINT_ERROR_LOGGER, err);
}
});
})
.catch(() => {
consumer.resume();
ep.emit(PRINT_ERROR_LOGGER, '[egg-kafka-node] consume message fail!');
});
});
consumer.on('error', error => {
ep.emit(PRINT_ERROR_LOGGER, '[egg-kafka-node] consumer connect error!');
ep.emit(PRINT_ERROR_LOGGER, error);
});
});
ep.emit(CONSUMER_READY, consumerMemory);
};
const config = app.config.kafkaNode;
const {
kafkaHost,
producerOption = {},
clientOption = {},
consumerOption = [],
} = config;
const baseReadDir = path.join(app.config.baseDir, './app/kafka/');
ep.once(CLIENT_READY, client => {
ProducerOp(client, producerOption);
SubscriptionOp(baseReadDir, consumerOption);
});
ep.once([ PRODUCER_READY, TOPICS_READY, SUBSCRIPTION_READY ], (producer, topics, topicSubscription) => {
ConsumerOp(config, topicSubscription);
});
ep.once(CONSUMER_READY, consumerMemory => {
process.once('SIGINT', () => {
consumerMemory.forEach(consumer => {
ep.emit(PRINT_LOGGER, '[egg-kafka-node] consumer close connect');
consumer.close(true, function(error) {
if (error) {
ep.emit(PRINT_ERROR_LOGGER, '[egg-kafka-node] consumer close connect fail!');
ep.emit(PRINT_ERROR_LOGGER, error);
}
});
});
});
});
ep.on(PRINT_LOGGER, info => {
console.info(info);
});
ep.on(PRINT_ERROR_LOGGER, error => {
console.error(error);
});
ep.once(PRODUCER_READY, producer => {
const createMessage = Message(config);
const topic1Message = new Array(100);
const topic2Message = new Array(100);
const topic3Message = new Array(100);
for (let i = 0; i < 100; i++) {
topic1Message[i] = `this is a message ${new Date()} ${Math.random()}`;
}
for (let i = 0; i < 100; i++) {
producer.send([ createMessage('testTopic1', 'Some', topic1Message) ], err => {
if (err) {
console.log(err);
}
});
}
for (let i = 0; i < 100; i++) {
topic2Message[i] = `this is a message ${new Date()} ${Math.random()}`;
}
for (let i = 0; i < 100; i++) {
producer.send([ createMessage('testTopic2', 'Every', topic2Message) ], err => {
if (err) {
console.log(err);
}
});
}
for (let i = 0; i < 100; i++) {
topic3Message[i] = `this is a message ${new Date()} ${Math.random()}`;
}
for (let i = 0; i < 100; i++) {
producer.send([ createMessage('testTopic3', 'New', topic3Message) ], err => {
if (err) {
console.log(err);
}
});
}
});
ClientOp(kafkaHost, clientOption);