diff --git a/lib/mq_client_api.js b/lib/mq_client_api.js index 82e368c..894d121 100644 --- a/lib/mq_client_api.js +++ b/lib/mq_client_api.js @@ -168,7 +168,16 @@ class MQClientAPI extends RemotingClient { const request = RemotingCommand.createRequestCommand(RequestCode.GET_ROUTEINTO_BY_TOPIC, { topic, }); - const response = await this.invoke(null, request, timeoutMillis); + let response = {}; + let count = this._namesrvAddrList.length; + while (count--) { + try { + response = await this.invoke(null, request, timeoutMillis); + break; + } catch (err) { + this.logger.warn(err); + } + } switch (response.code) { case ResponseCode.SUCCESS: { diff --git a/lib/remoting_client.js b/lib/remoting_client.js index b2394b0..be61eca 100644 --- a/lib/remoting_client.js +++ b/lib/remoting_client.js @@ -24,7 +24,7 @@ class RemotingClient extends Base { assert(options.onsAddr, '[RemotingClient] options.onsAddr is required'); assert(options.httpclient, '[RemotingClient] options.httpclient is required'); super(Object.assign({ initMethod: 'init' }, defaultOptions, options)); - + this._index = 0; this._inited = false; this._namesrvAddrList = []; this._channels = new Map(); @@ -162,8 +162,8 @@ class RemotingClient extends Base { */ getAndCreateChannel(addr) { if (!addr) { - const index = Math.floor(Math.random() * this._namesrvAddrList.length); - addr = this._namesrvAddrList[index]; + this._index = ++this._index % this._namesrvAddrList.length; + addr = this._namesrvAddrList[this._index]; } let channel = this._channels.get(addr); if (channel && channel.isOK) { diff --git a/test/mq_client_api.test.js b/test/mq_client_api.test.js index 12525f3..ba4ddd6 100644 --- a/test/mq_client_api.test.js +++ b/test/mq_client_api.test.js @@ -33,4 +33,29 @@ describe('test/mq_client_api.test.js', function() { } assert(isError); }); + + it('should getTopicRouteInfoFromNameServer ok', async () => { + const res = await client.getTopicRouteInfoFromNameServer('TEST_TOPIC', 3000); + assert(res); + }); + + it('should getTopicRouteInfoFromNameServer ok if old one is closed', async () => { + client._namesrvAddrList.unshift('1.2.3.4:80', '2.3.4.5:80'); + client._namesrvAddrList.push('6.7.8.9:80'); + const res = await client.getTopicRouteInfoFromNameServer('TEST_TOPIC', 3000); + assert(res); + }); + + it('should getTopicRouteInfoFromNameServer ok if old one is closed empty list', async () => { + client._namesrvAddrList = []; + let isError = false; + try { + await client.getTopicRouteInfoFromNameServer('TEST_TOPIC', 3000); + } catch (err) { + isError = true; + assert(err.name === 'MQClientException'); + } + assert(isError); + }); + });