-
Notifications
You must be signed in to change notification settings - Fork 43
Service Discovery
A ServiceDiscovery is a function that receives the desired service name and must return a string value (or a Promise<string>
) to inform the target destination for this proxy.
The serviceDiscovery middleware is initialized receiving a config
parameter that will receive any options declared in your middleware configuration plus two configurations:
Property | Type | Description | Required |
---|---|---|---|
clientAgent | any | The client created by the serviceDiscovery provider middleware. | true |
ssl | boolean | True if the requests to the target service must use SSL (https). | false |
Each serviceDiscovery middleware must be defined on its own .js file.
Example:
/**
* Where config contains a reference by the clientAgent created by the serviceDiscovery provider and a ssl flag
*/
module.exports = function (config) {
return (serviceName) => {
return (config.ssl?'https://':'http://') +
config.clientAgent.get(serviceName).address + ':' +
config.clientAgent.get(serviceName).port;
};
};
or, using Promises:
module.exports = function (config) {
return (serviceName) => {
return new Promise((resolve, reject) => {
config.clientAgent.get(serviceName)
.then(service => {
resolve((config.ssl?'https://':'http://') + service.address + ': ' + service.port)
}).catch(reject);
};
}
};
You must remeber to configure a serviceDiscovery provider middleware to initialize the clientAgent, used to interact with the service registry.
You can configure a serviceDiscovery middleware through:
- Admin Rest API:
POST /midleware/servicediscovery
- SDK:
sdk.middleware.addServiceDiscovery(name, fileName);
- CLI:
treeGatewayConfig middleware serviceDiscovery -a <name> ./filename.js
Tree Gateway provide some service discovery middlewares for common tasks already included in its distribution. Check the list here.