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
:
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:
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 Route Policy that register/deregister routes
to a given ServiceRegistry
according to route’s life-cycle:
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:
// 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:
// 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:
// 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.
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 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:
service:serviceName:delegateUri[?options]
Let’s explain this with an example:
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:
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.