Skip to content

Commit

Permalink
feat: support set unitName (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer committed May 8, 2021
1 parent 4d512fb commit 483b4fc
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
sudo: false
language: node_js
node_js:
- '8'
- '10'
- '12'
- '14'
- '16'
before_install:
- npm i npminstall -g
install:
Expand Down
9 changes: 8 additions & 1 deletion lib/client_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const defaultOptions = {
rebalanceInterval: 10 * 1000,
clientIP: address.ip(),
unitMode: false,
unitName: null,
// 阿里云自创建实例,需要 ns 前缀
namespace: '',
// 公有云生产环境:http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
Expand Down Expand Up @@ -41,7 +42,9 @@ class ClientConfig extends Base {
}

get clientId() {
return `${this.options.clientIP}@${this.instanceName}`;
return this.unitName ?
`${this.options.clientIP}@${this.instanceName}@${this.unitName}` :
`${this.options.clientIP}@${this.instanceName}`;
}

get pollNameServerInteval() {
Expand All @@ -64,6 +67,10 @@ class ClientConfig extends Base {
return this.options.unitMode;
}

get unitName() {
return this.options.unitName;
}

get namespace() {
return this.options.namespace;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/consumer/mq_push_consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class MQPushConsumer extends ClientConfig {
const _self = this;
(async () => {
while (!_self._isClosed) {
const pullRequest = _self._processQueueTable.get(messageQueue.key)
const pullRequest = _self._processQueueTable.get(messageQueue.key);
if (!pullRequest) {
break;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/remoting_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class RemotingClient extends Base {
return this.options.responseTimeout;
}

get unitName() {
return this.options.unitName;
}

/**
* start the client
* @return {void}
Expand Down Expand Up @@ -118,7 +122,11 @@ class RemotingClient extends Base {
this._namesrvAddrList = Array.isArray(this.options.nameSrv) ? this.options.nameSrv : [ this.options.nameSrv ];
return;
}
const ret = await this.httpclient.request(this.options.onsAddr, {
let url = this.options.onsAddr;
if (this.unitName) {
url = url + '-' + this.unitName + '?nofix=1';
}
const ret = await this.httpclient.request(url, {
timeout: this.options.connectTimeout || 10000,
});
if (ret.status === 200) {
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@
"homepage": "https://github.com/ali-sdk/ali-ons#readme",
"dependencies": {
"JSON2": "^0.1.0",
"address": "^1.1.0",
"address": "^1.1.2",
"binary-search-insert": "^1.0.3",
"byte": "^2.0.0",
"bytes": "^3.1.0",
"co-gather": "^1.0.1",
"debug": "^4.1.1",
"debug": "^4.3.1",
"is-type-of": "^1.2.1",
"long": "^4.0.0",
"mkdirp": "^0.5.1",
"mkdirp": "^1.0.4",
"mz-modules": "^2.1.0",
"osenv": "^0.1.5",
"sdk-base": "^3.6.0",
"tcp-base": "^3.1.0",
"utility": "^1.16.1"
"utility": "^1.17.0"
},
"devDependencies": {
"autod": "^3.1.0",
"autod": "^3.1.1",
"contributors": "^0.5.1",
"egg-bin": "^4.13.0",
"eslint": "^5.16.0",
"eslint-config-egg": "^7.4.1",
"mm": "^2.5.0",
"urllib": "^2.34.0"
"egg-bin": "^4.16.1",
"eslint": "^7.25.0",
"eslint-config-egg": "^9.0.0",
"mm": "^3.2.0",
"urllib": "^2.37.1"
},
"engines": {
"node": ">= 8.0.0"
Expand Down
2 changes: 1 addition & 1 deletion test/mq_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('test/mq_client.test.js', () => {
try {
await client.unregisterConsumer('xxx');
} catch (err) {
assert(err.message.includes('resource xxx not created'));
assert(err.message.includes('xxx not created'));
}
await client.unregisterProducer('xxx');
});
Expand Down
17 changes: 17 additions & 0 deletions test/remoting_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,21 @@ describe('test/remoting_client.test.js', function() {
channel.close();
assert(client.getAndCreateChannel(address).clientId !== channel.clientId);
});

it('should support unitName', async () => {
mm(httpclient, 'request', async url => {
assert(url.includes('-xxx?nofix=1'));
return {
status: 200,
data: '127.0.0.1:9876;127.0.0.2:9876',
};
});
const client = new RemotingClient(Object.assign({ httpclient, unitName: 'xxx' }, config));
await client.ready();

assert(client._namesrvAddrList, [
'127.0.0.1:9876',
'127.0.0.2:9876',
]);
});
});

0 comments on commit 483b4fc

Please sign in to comment.