UNPKG

1.99 kBMarkdownView Raw
1# Locators: a simple service locators
2There are three different locators packaged in this library: simple, request and zookeeper locators.
3
4### simple locator
5simple locator takes a list of known servers and randomly returns one of the servers.
6
7#### example
8```coffeescript
9simpleLocatorFactory = require('locators').simple
10
11locator = simpleLocatorFactory()({
12 resource: "localhost;koalastothemax.com:80"
13 defaultPort: 8181
14})
15locator.then((location) ->
16 console.log location #either { host: "localhost", port: 8181 } or { host: "koalastothemax.com", port: "80" }
17)
18```
19
20### request locator
21request 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
25requestLocatorFactory = require('locators').request
26
27locator = 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})
36locator.then((location) ->
37 console.log location #returns { host: 'localhost', port: 1234 }
38)
39```
40
41### zookeeper locator
42zookeeper 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```
46zookeeperLocatorFactory = require('locators').zookeeper
47
48zookeeperLocator = zookeeperLocatorFactory({
49 serverLocator: simpleLocatorFactory()('localhost:2181')
50 path: '/discovery'
51 locatorTimeout: 2000
52})
53myServiceLocator = zookeeperLocator('my:service')
54myServiceLocator.then((location) ->
55 console.log location #returns host and port from zookeeper localhost:2181
56)
57```
\No newline at end of file