blob: dbca0882d033029c541a3fc6ad703f972b9e9b6f [file] [log] [blame]
= Service Registry
Service registration is a key part of service discovery which Camel leverages through
the _Service Call EIP_ and support to ease the process to expose routes in a
cloud environment and consume them with minimal configuration.
== Service Registry Set-Up
A `ServiceRegistry` is just like any other camel service so set it up you only need
to register your implementations to the `CamelContext`:
[source,java]
----
ServiceRegistry service = new MyServiceRegistry();
context.addService(service);
----
The configuration of the _Service Registry_ depends on the implementation you have chosen.
Out of the box camel provides the following implementations:
[cols="1,1,2", options="header"]
|====
|Type |Module | Class
|consul |camel-consul | org.apache.camel.component.consul.cloud.ConsulServiceRegistry
|spring-cloud |camel-spring-cloud | org.apache.camel.component.spring.cloud.CamelSpringCloudServiceRegistry
|zookeeper |camel-zookeeper | org.apache.camel.component.zookeeper.cloud.ZooKeeperServiceRegistry
|====
== Service Registry Usage
The _Service Registry SPI_ is leveraged by the following new implementations:
=== ServiceRegistryRoutePolicy
This is an implementation of a xref:route-policy.adoc[Route Policy] that register/deregister routes
to a given `ServiceRegistry` according to route's life-cycle:
[source,java]
----
RoutePolicy policy = new ServiceRegistrationRoutePolicy()
// bind the policy to one or more routes
from("undertow:http://0.0.0.0:8080")
.routePolicy(policy)
.log("Route ${routeId} has been invoked");
----
To apply the same policy to all the routes a dedicated `RoutePolicyFactory` can be used:
[source,java]
----
// add the service registry route policy factory to context
context.addRoutePolicyFactory(new ServiceRegistrationRoutePolicyFactory()));
----
To configure how the service is exposed you can add route specific properties like:
[source,java]
----
// bind the policy to one or more routes
from("undertow:http://0.0.0.0:8080")
.routePolicy(policy)
.routeProperty(ServiceDefinition.SERVICE_META_NAME, "my-service")
.routeProperty(ServiceDefinition.SERVICE_META_ID, "my-id")
.routeProperty(ServiceDefinition.SERVICE_META_PORT, "8080")
.log("Route ${routeId} has been invoked");
----
Service name and service id can also be provided by _routeId_ and _routeGroup_:
[source,java]
----
// bind the policy to one or more routes
from("undertow:http://0.0.0.0:8080")
.routePolicy(policy)
.routeGroup("my-service")
.routeId("my-id")
.routeProperty(ServiceDefinition.SERVICE_META_PORT, "8080")
.log("Route ${routeId} has been invoked");
----
Any property prefixed with _service._ is automatically added to the service's metadata.
TIP: Some component such has camel-undertow and those based on camel-http-common implement _DiscoverableService_ and they can automatically provide the metadata needed for service registration.
=== Service Component
The xref:components::service-component.adoc[Service] component is similar to a `ServiceRegistrationRoutePolicyFactory`
but is capable of tagging routes that need to be registered to the `ServiceRegistry`
by prefixing the related endpoints with `service:name` according to the following syntax:
[source,text]
----
service:serviceName:delegateUri[?options]
----
Let's explain this with an example:
[source,java]
----
from("service:my-service:undertow:http://0.0.0.0:8080")
.log("Route ${routeId} has been invoked");
----
To configure how the service is exposed you can add service specific endpoint options such as:
[source,java]
----
from("service:my-service:undertow:http://0.0.0.0:8080?service.id=my-service-id")
.log("Route ${routeId} has been invoked");
----
Any option prefixed with _service._ is automatically added to the service's metadata.