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.
服务注册是服务发现的关键部分,Camel 通过服务调用 EIP 和支持来利用它,以便在云环境中轻松暴露路由并以最小配置消费它们。
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
就像任何其他的骆驼服务一样,所以要设置它,你只需要将你的实现注册到 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:
服务注册表的配置取决于您选择的实现方式。Camel 提供了以下几种开箱即用的实现方式:
Type 类型 | Module 模块 | Class 班级 |
---|---|---|
consul 领事 | camel-consul 骆驼-领事 | org.apache.camel.component.consul.cloud.ConsulServiceRegistry |
spring-cloud 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:
服务注册 SPI 被以下新实现所利用:
ServiceRegistryRoutePolicy
服务注册路由策略
This is an implementation of a Route Policy that register/deregister routes to a given ServiceRegistry
according to route’s life-cycle:
这是一个根据路由的生命周期向给定的 ServiceRegistry
注册/注销路由的路由策略的实现
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:
为了将相同的政策应用于所有路由,可以使用专用的 RoutePolicyFactory
// 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:
服务名称和服务 ID 也可以通过 routeId 和 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.
任何以 service.为前缀的属性都会自动添加到服务的元数据中。
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. 一些组件,如 camel-undertow 和基于 camel-http-common 的组件,实现了 DiscoverableService,并且它们可以自动提供服务注册所需的元数据。 |
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:
服务组件类似于 ServiceRegistrationRoutePolicyFactory
,但能够通过在相关端点前加上 service:name
来标记需要注册到 ServiceRegistry
的路由,语法如下:
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.
任何以 service.为前缀的选项都会自动添加到服务的元数据中。