Skip to content

Commit

Permalink
Merge pull request #100 from jquatier/support-multiple-vips
Browse files Browse the repository at this point in the history
Support multiple vips
  • Loading branch information
jquatier committed Dec 15, 2016
2 parents 3b32aa7 + 6bb4ba1 commit 987d28f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -27,3 +27,5 @@ build/Release
node_modules

lib/

.DS_Store
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "eureka-js-client",
"version": "4.1.2",
"version": "4.1.3",
"description": "A JavaScript implementation the Netflix OSS service registry, Eureka.",
"main": "lib/index.js",
"scripts": {
Expand Down
23 changes: 18 additions & 5 deletions src/EurekaClient.js
Expand Up @@ -372,14 +372,20 @@ export default class Eureka extends EventEmitter {
const instances = app.instance.filter((instance) => (this.validateInstance(instance)));
cache.app[app.name.toUpperCase()] = instances;
instances.forEach((inst) => {
if (!cache.vip[inst.vipAddress]) {
cache.vip[inst.vipAddress] = [];
}
cache.vip[inst.vipAddress].push(inst);
const vipAddresses = this.splitVipAddress(inst.vipAddress);
vipAddresses.forEach((vipAddress) => {
if (!cache.vip[vipAddress]) {
cache.vip[vipAddress] = [];
}
cache.vip[vipAddress].push(inst);
});
});
} else if (this.validateInstance(app.instance)) {
const instances = [app.instance];
cache.vip[app.instance.vipAddress] = instances;
const vipAddresses = this.splitVipAddress(app.instance.vipAddress);
vipAddresses.forEach((vipAddress) => {
cache.vip[vipAddress] = instances;
});
cache.app[app.name.toUpperCase()] = instances;
}
}
Expand All @@ -391,6 +397,13 @@ export default class Eureka extends EventEmitter {
return (!this.config.eureka.filterUpInstances || instance.status === 'UP');
}

/*
Returns an array of vipAddresses from string vipAddress given by eureka
*/
splitVipAddress(vipAddress) { // eslint-disable-line
return vipAddress.split(',');
}

/*
Fetches the metadata using the built-in client and updates the instance
configuration with the hostname and IP address. If the value of the config
Expand Down
18 changes: 16 additions & 2 deletions test/EurekaClient.test.js
Expand Up @@ -746,17 +746,21 @@ describe('Eureka client', () => {
let app;
let instance1;
let instance2;
let instance3;
let downInstance;
let theVip;
let multiVip;
let cache;
beforeEach(() => {
config = makeConfig({
instance: { dataCenterInfo: { name: 'Amazon' } },
});
client = new Eureka(config);
theVip = 'theVip';
multiVip = 'fooVip,barVip';
instance1 = { host: '127.0.0.1', port: 1000, vipAddress: theVip, status: 'UP' };
instance2 = { host: '127.0.0.2', port: 2000, vipAddress: theVip, status: 'UP' };
instance3 = { host: '127.0.0.2', port: 2000, vipAddress: multiVip, status: 'UP' };
downInstance = { host: '127.0.0.2', port: 2000, vipAddress: theVip, status: 'DOWN' };
app = { name: 'theapp' };
cache = { app: {}, vip: {} };
Expand All @@ -769,11 +773,21 @@ describe('Eureka client', () => {
expect(cache.vip[theVip].length).to.equal(1);
});

it('should transform an app with one instance that has a comma separated vipAddress', () => {
app.instance = instance3;
client.transformApp(app, cache);
expect(cache.app[app.name.toUpperCase()].length).to.equal(1);
expect(cache.vip[multiVip.split(',')[0]].length).to.equal(1);
expect(cache.vip[multiVip.split(',')[1]].length).to.equal(1);
});

it('should transform an app with two or more instances', () => {
app.instance = [instance1, instance2];
app.instance = [instance1, instance2, instance3];
client.transformApp(app, cache);
expect(cache.app[app.name.toUpperCase()].length).to.equal(2);
expect(cache.app[app.name.toUpperCase()].length).to.equal(3);
expect(cache.vip[theVip].length).to.equal(2);
expect(cache.vip[multiVip.split(',')[0]].length).to.equal(1);
expect(cache.vip[multiVip.split(',')[1]].length).to.equal(1);
});

it('should filter UP instances by default', () => {
Expand Down

0 comments on commit 987d28f

Please sign in to comment.