This component provides an infrastructure to publish and discover various resources, such as service
proxies, HTTP endpoints, data sources…​

These resources are called services. A service is a discoverable
functionality. It can be qualified by its type, metadata, and location. So a service can be a database, a
service proxy, a HTTP endpoint and any other resource you can imagine as soon as you can describe it, discover it
and interact with it. It does not have to be a vert.x entity, but can be anything. Each service is described by a
Record.

The service discovery implements the interactions defined in service-oriented computing. And to some extent,
also provides the dynamic service-oriented computing interactions. So, applications can react to arrival and
departure of services.

Consumer would 1) lookup a service record matching their need, 2) retrieve the
ServiceReference that give access to the service, 3) get a service object to access
the service, 4) release the service object once done.

The process can be simplified using service type where you can directly retrieve the service object if you know
from which type it is (JDBC client, Http client…​).

As stated above, the central piece of information shared by the providers and consumers are
records.

Providers and consumers must create their own ServiceDiscovery instance. These
instances are collaborating in the background (distributed structure) to keep the set of services in sync.

The service discovery supports bridges to import and export services from / to other discovery technologies.

Using the service discovery

To use the Vert.x service discovery, add the following dependency to the dependencies section of your build
descriptor:

Overall concepts

The discovery mechanism is based on a few concepts explained in this section.

Service records

A service Record is an object that describes a service published by a service
provider. It contains a name, some metadata, a location object (describing where is the service). This record is
the only object shared by the provider (having published it) and the consumer (retrieve it when doing a lookup).

The metadata and even the location format depend on the service type (see below).

A record is published when the provider is ready to be used, and withdrawn when the service provider is stopping.

Service Provider and publisher

A service provider is an entity providing a service. The publisher is responsible for publishing a record
describing the provider. It may be a single entity (a provider publishing itself) or a different entity.

Service Consumer

Service consumers search for services in the service discovery. Each lookup retrieves 0..nRecord. From these records, a consumer can retrieve a
ServiceReference, representing the binding between the consumer and the provider.
This reference allows the consumer to retrieve the service object (to use the service), and release the service.

It is important to release service references to cleanup the objects and update the service usages.

Service object

The service object is the object that gives access to a service. It can come in various forms, such as a proxy, a client,
and may even be non-existent for some service types. The nature of the service object depends on the service type.

Notice that because of the polyglot nature of Vert.x, the service object can differ if you retrieve it from Java,
Groovy or another language.

Service types

Services are just resources, and there are a lot of different kinds of services. They can be functional services,
databases, REST APIs, and so on. The Vert.x service discovery has the concept of service types to handle this
heterogeneity. Each type defines:

how the service is located (URI, event bus address, IP / DNS…​) - location

Some service types are implemented and provided by the service discovery component, but you can add
your own.

Service events

Every time a service provider is published or withdrawn, an event is fired on the event bus. This event contains
the record that has been modified.

In addition, in order to track who is using who, every time a reference is retrieved with
getReference or released with
release, events are emitted on the event bus to track the
service usages.

More details on these events below.

Backend

The service discovery uses a Vert.x distributed data structure to store the records. So, all members of the cluster
have access to all the records. This is the default backend implementation. You can implement your own by
implementing the ServiceDiscoveryBackend SPI. For instance, we provide an
implementation based on Redis.

Notice that the discovery does not require Vert.x clustering. In single-node mode, the structure is local. It can
be populated with ServiceImporter`s. Since 3.5.0, you can use a local
structure even in clustered mode by setting the system property `vertx-service-discovery-backend-local to true (or
the environment variable VERTX-SERVICE-DISCOVERY-BACKEND-LOCAL to true).

Creating a service discovery instance

Publishers and consumers must create their own ServiceDiscovery
instance to use the discovery infrastructure:

By default, the announce address (the event bus address on which service events are sent is: vertx.discovery
.announce. You can also configure a name used for the service usage (see section about service usage).

When you don’t need the service discovery object anymore, don’t forget to close it. It closes the different
discovery importers and exporters you have configured and releases the service references.

You should avoid sharing the service discovery instance, so service usage would represent the right "usages".

Publishing services

Once you have a service discovery instance, you can publish services. The process is the following:

create a record for a specific service provider

publish this record

keep the published record that is used to un-publish a service or modify it.

To create records, you can either use the Record class, or use convenient methods
from the service types.

It is important to keep a reference on the returned records, as this record has been extended by a registration id.

Withdrawing services

To withdraw (un-publish) a record, use:

discovery.unpublish(record['registration']) { |ar_err,ar|
if (ar_err == nil)
# Okelse# cannot un-publish the service, may have already been removed, or the record is not publishedend
}

Looking for services

This section explains the low-level process to retrieve services, each service type provide convenient method to
aggregates the different steps.

On the consumer side, the first thing to do is to lookup for records. You can search for a single record or all
the matching ones. In the first case, the first matching record is returned.

Consumer can pass a filter to select the service. There are two ways to describe the filter:

A function taking a Record as parameter and returning a boolean (it’s a
predicate)

This filter is a JSON object. Each entry of the given filter is checked against the record. All entries must
exactly match the record. The entry can use the special * value to denote a requirement on the key, but not on
the value.

Let’s see an example of a JSON filter:

{ "name" = "a" } => matches records with name set to "a"
{ "color" = "*" } => matches records with "color" set
{ "color" = "red" } => only matches records with "color" set to "red"
{ "color" = "red", "name" = "a"} => only matches records with name set to "a", and color set to "red"

If the JSON filter is not set (null or empty), it accepts all records. When using functions, to accept all
records, you must return true regardless the record.

Here are some examples:

# Get any record
discovery.get_record(lambda { |r|
true
}) { |ar_err,ar|
if (ar_err == nil)
if (ar != nil)
# we have a recordelse# the lookup succeeded, but no matching serviceendelse# lookup failedend
}
discovery.get_record(nil) { |ar_err,ar|
if (ar_err == nil)
if (ar != nil)
# we have a recordelse# the lookup succeeded, but no matching serviceendelse# lookup failedend
}
# Get a record by name
discovery.get_record(lambda { |r|
r['name'].==("some-name")
}) { |ar_err,ar|
if (ar_err == nil)
if (ar != nil)
# we have a recordelse# the lookup succeeded, but no matching serviceendelse# lookup failedend
}
discovery.get_record({
'name' => "some-service"
}) { |ar_err,ar|
if (ar_err == nil)
if (ar != nil)
# we have a recordelse# the lookup succeeded, but no matching serviceendelse# lookup failedend
}
# Get all records matching the filter
discovery.get_records(lambda { |r|
"some-value".==(r['metadata']['some-label'])
}) { |ar_err,ar|
if (ar_err == nil)
results = ar
# If the list is not empty, we have matching record# Else, the lookup succeeded, but no matching serviceelse# lookup failedend
}
discovery.get_records({
'some-label' => "some-value"
}) { |ar_err,ar|
if (ar_err == nil)
results = ar
# If the list is not empty, we have matching record# Else, the lookup succeeded, but no matching serviceelse# lookup failedend
}

You can retrieve a single record or all matching records with
getRecords.
By default, record lookup does include only records with a status set to UP. This can be overridden:

when using JSON filter, just set status to the value you want (or * to accept all status)

when using function, set the includeOutOfService parameter to true in
getRecords
.

When retrieving a service reference you can pass a JsonObject used to configure the
service object. It can contain various data about the service object. Some service types do not need additional
configuration, some require configuration (as data sources):

require 'vertx-jdbc/jdbc_client'
reference = discovery.get_reference_with_configuration(record, conf)
# Then, gets the service object, the returned type depends on the service type:# For http endpoint:
client = reference.get_as(VertxJdbc::JDBCClient::class)
# Do something with the client...# When done with the service
reference.release()

In the previous examples, the code uses
getAs. The parameter is the type of
object you expect to get. If you are using Java, you can use
get. However in the other language you must pass the expected
type.

Types of services

A said above, the service discovery has the service type concept to manage the heterogeneity of the
different services.

These types are provided by default:

HttpEndpoint - for REST API’s, the service object is a
HttpClient configured on the host and port (the location is the url).

EventBusService - for service proxies, the service object is a proxy. Its
type is the proxies interface (the location is the address).

JDBCDataSource - for JDBC data sources, the service object is a
JDBCClient (the configuration of the client is computed from the location, metadata and
consumer configuration).

RedisDataSource - for Redis data sources, the service object is a
RedisClient (the configuration of the client is computed from the location, metadata and
consumer configuration).

MongoDataSource - for Mongo data sources, the service object is a
MongoClient (the configuration of the client is computed from the location, metadata and
consumer configuration).

This section gives details about service types in general and describes how to use the default service types.

Services with no type

Some records may have no type (ServiceType.UNKNOWN). It is not possible to
retrieve a reference for these records, but you can build the connection details from the location and
metadata of the Record.

Using these services does not fire service usage events.

HTTP endpoints

A HTTP endpoint represents a REST API or a service accessible using HTTP requests. The HTTP endpoint service
objects are HttpClient configured with the host, port and ssl.

Event bus services

Event bus services are service proxies. They implement async-RPC services on top of the event bus. When retrieving
a service object from an event bus service, you get a service proxy of the right type. You can access helper
methods from EventBusService.

Notice that service proxies (service implementations and service interfaces) are developed in Java.

As JDBC data sources can represent a high variety of databases, and their access is often different, the record is
rather unstructured. The location is a simple JSON object that should provide the fields to access the data
source (JDBC url, username…​). The set of fields may depend on the database but also on the connection pool used
in front.

Consuming a JDBC service

As stated in the previous section, how to access a data source depends on the data source itself. To build the
JDBCClient, you can merge configuration: the record location, the metadata and a json object provided by
the consumer:

The location is a simple JSON object that should provide the fields to access the Redis data
source (url, port…​).

Consuming a Redis service

As stated in the previous section, how to access a data source depends on the data source itself. To build the
RedisClient, you can merge configuration: the record location, the metadata and a json object provided by
the consumer:

The location is a simple JSON object that should provide the fields to access the Redis data
source (url, port…​).

Consuming a Mongo service

As stated in the previous section, how to access a data source depends on the data source itself. To build the
MongoClient, you can merge configuration: the record location, the metadata and a json object
provided by the consumer:

the id of the service discovery (either its name or the node id) in the id field

This id is configurable from the ServiceDiscoveryOptions. By default it’s "localhost" on
single node configuration and the id of the node in clustered mode.

You can disable the service usage support by setting the usage address to null with
usageAddress.

Service discovery bridges

Bridges let you import and export services from / to other discovery mechanism such as Docker, Kubernetes, Consul…​
Each bridge decides how the services are imported and exported. It does not have to be bi-directional.

The second parameter can provide an optional configuration for the bridge.

When the bridge is registered the

{@link io.vertx.servicediscovery.spi.ServiceImporter#start)}
method is called. It lets you configure the bridge. When the bridge is configured, ready and has imported /
exported the initial services, it must complete the given Future. If the bridge starts
method is blocking, it must use an
executeBlocking construct, and
complete the given future object.

When the service discovery is stopped, the bridge is stopped. The
close
method is called that provides the opportunity to cleanup resources, removed imported / exported services…​ This
method must complete the given Future to notify the caller of the completion.

Notice than in a cluster, only one member needs to register the bridge as the records are accessible by all members.

Additional bridges

In addition of the bridges supported by this library, Vert.x Service Discovery provides additional
bridges you can use in your application.

Consul bridge

This discovery bridge imports services from Consul into the Vert.x service discovery.

The bridge
connects to a Consul agent (server) and periodically scan for services:

new services are imported

services in maintenance mode or that has been removed from consul are removed

This bridge uses the HTTP API for Consul. It does not export to Consul and does not support service modification.

The service type is deduced from tags. If a tag matches a known service type, this service type will be used.
If not, the service is imported as unknown. Only http-endpoint is supported for now.

Using the bridge

To use this Vert.x discovery bridge, add the following dependency to the dependencies section of your build
descriptor:

For HTTP endpoints, the ssl (https) attribute is set to true if the service has the ssl label set to true.

Dynamics

The bridge imports all services on start and removes them on stop. In between it watches the Kubernetes
services and add the new ones and removes the deleted ones.

Unresolved directive in index.adoc - include::zookeeper-bridge.adoc[]

Docker Links bridge

This discovery bridge imports services from Docker Links into the Vert.x service discovery.

When you link a Docker
container to another Docker container, Docker injects a set of environment variables. This bridge analyzes these
environment variables and imports service record for each link. The service type is deduced from the service.type
label. If not set, the service is imported as unknown. Only http-endpoint are supported for now.

As the links are created when the container starts, the imported records are created when the bridge starts and
do not change afterwards.

Using the bridge

To use this Vert.x discovery bridge, add the following dependency to the dependencies section of your build
descriptor: