Skip to content

Service Discovery in Martini

When using Martini with a cluster manager, clients of a service can use Martini's service registry Functions) to determine the location of a service instance to which to send requests. This enables you to programmatically ask for a host that's serving a particular API. This is very useful when using Martini in conjuction with Kubernetes or Consul.

Gloop APIs in a cluster

By default, Martini APIs are automatically registered as services[^1] against the cluster manager in use, and the namespace is used as their lookup names.

The service registry API includes methods that will perform the lookup for you, and rewrite the URL you give it as well, depending on the protocol. This means at runtime, you can query the service registry using an API namespace as the host (complete with URI segments), and it will return a fully-formed URL that points to a server that can serve the request.

For example, if you have an API registered with the namespace my.api.HelloWorld, you can query the service registry using the URL srhttp://my.api.HelloWorld/foo/bar. With this, the API will return a rewritten URL, such as https://192.168.242.100:12345/foo/bar, which you can then use to execute your desired service. Notice that the URL passed to the API uses srhttp as the scheme - this is how Martini knows to look it up. If the scheme isn't srhttp, Martini won't perform a lookup and rewrite the URL, instead it will return with the URL it received.

Note that if Martini isn't running in a cluster, the service registry will rewrite URLs, and these URLs will be pointing to the already running Martini instance (in other words, Martini will rewrite the URLs to point to itself).