UNPKG

2.68 kBJavaScriptView Raw
1'use strict';
2
3const Base = require('sdk-base');
4const address = require('address');
5const MixAll = require('./mix_all');
6
7const defaultOptions = {
8 instanceName: 'DEFAULT',
9 pollNameServerInteval: 30 * 1000,
10 heartbeatBrokerInterval: 30 * 1000,
11 persistConsumerOffsetInterval: 5 * 1000,
12 rebalanceInterval: 10 * 1000,
13 clientIP: address.ip(),
14 unitMode: false,
15 // 阿里云自创建实例,需要 ns 前缀
16 namespace: '',
17 // 公有云生产环境:http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
18 // 公有云公测环境:http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet
19 // 杭州金融云环境:http://jbponsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
20 // 杭州深圳云环境:http://mq4finance-sz.addr.aliyun.com:8080/rocketmq/nsaddr4client-internal
21 onsAddr: 'http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet',
22 // https://help.aliyun.com/document_detail/102895.html 阿里云产品更新,支持实例化
23 // nameSrv: 'onsaddr.mq-internet-access.mq-internet.aliyuncs.com:80',
24 onsChannel: 'ALIYUN', // CLOUD, ALIYUN, ALL
25};
26
27class ClientConfig extends Base {
28
29 /**
30 * Producer 与 Consumer的公共配置
31 * @param {Object} options
32 * - {String} instanceName 示例名称
33 * - {Number} pollNameServerInteval name server 同步的间隔
34 * - {Number} heartbeatBrokerInterval 心跳的间隔
35 * - {Number} persistConsumerOffsetInterval 持久化消费进度的间隔
36 * - {Boolean} unitMode 是否为单元化的订阅组
37 */
38 constructor(options) {
39 super(Object.assign({}, defaultOptions, options));
40 this.instanceName = this.options.instanceName;
41 }
42
43 get clientId() {
44 return `${this.options.clientIP}@${this.instanceName}`;
45 }
46
47 get pollNameServerInteval() {
48 return this.options.pollNameServerInteval;
49 }
50
51 get heartbeatBrokerInterval() {
52 return this.options.heartbeatBrokerInterval;
53 }
54
55 get persistConsumerOffsetInterval() {
56 return this.options.persistConsumerOffsetInterval;
57 }
58
59 get rebalanceInterval() {
60 return this.options.rebalanceInterval;
61 }
62
63 get unitMode() {
64 return this.options.unitMode;
65 }
66
67 get namespace() {
68 return this.options.namespace;
69 }
70
71 formatTopic(topic) {
72 if (this.namespace && (!topic.startsWith(this.namespace) &&
73 !topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX))) {
74 topic = `${this.namespace}%${topic}`;
75 }
76 return topic;
77 }
78
79 /**
80 * 将实例名修改为进程号
81 */
82 changeInstanceNameToPID() {
83 if (this.instanceName === 'DEFAULT') {
84 this.instanceName = process.pid + '';
85 }
86 }
87}
88
89module.exports = ClientConfig;