1 | # Locators: a simple service locators
|
2 | There are three different locators packaged in this library: simple, request and zookeeper locators.
|
3 |
|
4 | ### simple locator
|
5 | simple locator takes a list of known servers and randomly returns one of the servers.
|
6 |
|
7 | #### example
|
8 | ```coffeescript
|
9 | simpleLocatorFactory = require('locators').simple
|
10 |
|
11 | locator = simpleLocatorFactory()({
|
12 | resource: "localhost;koalastothemax.com:80"
|
13 | defaultPort: 8181
|
14 | })
|
15 | locator.then((location) ->
|
16 | console.log location #either { host: "localhost", port: 8181 } or { host: "koalastothemax.com", port: "80" }
|
17 | )
|
18 | ```
|
19 |
|
20 | ### request locator
|
21 | request locator takes a remote http/https endpoint and retrieves a list of servers and randomly returns one of the servers.
|
22 |
|
23 | #### example
|
24 | ```coffeescript
|
25 | requestLocatorFactory = require('locators').request
|
26 |
|
27 | locator = requestLocatorFactory()({
|
28 | url: "http://www.test-endpoint.com:8080/list" # returns {"blah": [{"address": "localhost", "port": 8080}, {"address": "localhost", "port": 1234}]}
|
29 | dataExtractor: (data) ->
|
30 | location = JSON.parse(data).blah[1]
|
31 | return {
|
32 | host: location.address
|
33 | port: location.port
|
34 | }
|
35 | })
|
36 | locator.then((location) ->
|
37 | console.log location #returns { host: 'localhost', port: 1234 }
|
38 | )
|
39 | ```
|
40 |
|
41 | ### zookeeper locator
|
42 | zookeeper locator uses [zookeeper](http://zookeeper.apache.org) to find other services. It is different from other locators in that it takes a locator for zookeeper services as well. So that if you are using an [exhibitor](https://github.com/Netflix/exhibitor), you can make use of its list api.
|
43 |
|
44 | #### example
|
45 | ```
|
46 | zookeeperLocatorFactory = require('locators').zookeeper
|
47 |
|
48 | zookeeperLocator = zookeeperLocatorFactory({
|
49 | serverLocator: simpleLocatorFactory()('localhost:2181')
|
50 | path: '/discovery'
|
51 | locatorTimeout: 2000
|
52 | })
|
53 | myServiceLocator = zookeeperLocator('my:service')
|
54 | myServiceLocator.then((location) ->
|
55 | console.log location #returns host and port from zookeeper localhost:2181
|
56 | )
|
57 | ``` |
\ | No newline at end of file |