Skip to content

Commit

Permalink
Filtering UP instances by default. Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jquatier committed May 1, 2016
1 parent 84ac4ec commit 0695aa0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 3.0.0
- Populate registry cache with instances that have a status of `UP`, `filterUpInstances` can be set to `false` to disable.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ If your have multiple availability zones and your DNS entries set up according t

This will cause the client to perform a DNS lookup using `config.eureka.host` and `config.eureka.ec2Region`. The naming convention for the DNS TXT records required for this to function is also described in the Wiki article above.

## Configuration Options
option | default value | description
---- | --- | ---
`logger` | console logging | logger implementation for the client to use
`eureka.heartbeatInterval` | `30000` | milliseconds to wait between heartbeats
`eureka.registryFetchInterval` | `30000` | milliseconds to wait between registry fetches
`eureka.fetchRegistry` | `true` | enable/disable registry fetching
`eureka.filterUpInstances` | `true` | enable/disable filtering of instances with status === `UP`
`eureka.servicePath` | `/eureka/v2/apps/` | path to eureka REST service
`eureka.ssl` | `false` | enable SSL communication with Eureka server
`eureka.useDns` | `false` | look up Eureka server using DNS, see [Looking up Eureka Servers in AWS using DNS](#looking-up-eureka-servers-in-aws-using-dns)
`eureka.fetchMetadata` | `true` | fetch AWS metadata when in AWS environment, see [Configuring for AWS environments](#configuring-for-aws-environments)
`eureka.useLocalMetadata` | `false` | use local IP and local hostname from metadata when in an AWS environment.

## Debugging

Expand Down
16 changes: 12 additions & 4 deletions src/EurekaClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,27 @@ export default class Eureka {

/*
Transforms the given application and places in client cache. If an application
has a single instance, the instance is placed into the cache as an array of one
has a single instance, the instance is placed into the cache as an array of one
*/
transformApp(app, cache) {
if (app.instance.length) {
cache.app[app.name.toUpperCase()] = app.instance;
cache.vip[app.instance[0].vipAddress] = app.instance;
} else {
const instances = app.instance.filter((instance) => (this.validateInstance(instance)));
cache.app[app.name.toUpperCase()] = instances;
cache.vip[app.instance[0].vipAddress] = instances;
} else if (this.validateInstance(app.instance)) {
const instances = [app.instance];
cache.vip[app.instance.vipAddress] = instances;
cache.app[app.name.toUpperCase()] = instances;
}
}

/*
Returns true if instance filtering is disabled, or if the instance is UP
*/
validateInstance(instance) {
return (!this.config.eureka.filterUpInstances || instance.status === 'UP');
}

/*
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
1 change: 1 addition & 0 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default {
heartbeatInterval: 30000,
registryFetchInterval: 30000,
fetchRegistry: true,
filterUpInstances: true,
servicePath: '/eureka/v2/apps/',
ssl: false,
useDns: false,
Expand Down
31 changes: 26 additions & 5 deletions test/EurekaClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@ describe('Eureka client', () => {
registry = {
applications: { application: {} },
};
instance1 = { host: '127.0.0.1', port: 1000, vipAddress: 'vip1' };
instance2 = { host: '127.0.0.2', port: 2000, vipAddress: 'vip2' };
instance3 = { host: '127.0.0.2', port: 2000, vipAddress: 'vip2' };
instance1 = { host: '127.0.0.1', port: 1000, vipAddress: 'vip1', status: 'UP' };
instance2 = { host: '127.0.0.2', port: 2000, vipAddress: 'vip2', status: 'UP' };
instance3 = { host: '127.0.0.2', port: 2000, vipAddress: 'vip2', status: 'UP' };
app1 = { name: 'theapp', instance: instance1 };
app2 = { name: 'theapptwo', instance: [instance2, instance3] };
client = new Eureka(config);
Expand Down Expand Up @@ -548,6 +548,7 @@ describe('Eureka client', () => {
let app;
let instance1;
let instance2;
let downInstance;
let theVip;
let cache;
beforeEach(() => {
Expand All @@ -556,8 +557,9 @@ describe('Eureka client', () => {
});
client = new Eureka(config);
theVip = 'theVip';
instance1 = { host: '127.0.0.1', port: 1000, vipAddress: theVip };
instance2 = { host: '127.0.0.2', port: 2000, vipAddress: theVip };
instance1 = { host: '127.0.0.1', port: 1000, vipAddress: theVip, status: 'UP' };
instance2 = { host: '127.0.0.2', port: 2000, vipAddress: theVip, status: 'UP' };
downInstance = { host: '127.0.0.2', port: 2000, vipAddress: theVip, status: 'DOWN' };
app = { name: 'theapp' };
cache = { app: {}, vip: {} };
});
Expand All @@ -575,6 +577,25 @@ describe('Eureka client', () => {
expect(cache.app[app.name.toUpperCase()].length).to.equal(2);
expect(cache.vip[theVip].length).to.equal(2);
});

it('should filter UP instances by default', () => {
app.instance = [instance1, instance2, downInstance];
client.transformApp(app, cache);
expect(cache.app[app.name.toUpperCase()].length).to.equal(2);
expect(cache.vip[theVip].length).to.equal(2);
});

it('should not filter UP instances when filterUpInstances === false', () => {
config = makeConfig({
instance: { dataCenterInfo: { name: 'Amazon' } },
eureka: { filterUpInstances: false },
});
client = new Eureka(config);
app.instance = [instance1, instance2, downInstance];
client.transformApp(app, cache);
expect(cache.app[app.name.toUpperCase()].length).to.equal(3);
expect(cache.vip[theVip].length).to.equal(3);
});
});

describe('addInstanceMetadata()', () => {
Expand Down

0 comments on commit 0695aa0

Please sign in to comment.