8.12. service Call

概述

由 Camel 2.18 提供。

服务调用 模式允许您在分布式系统中调用远程服务。要调用的服务会在服务 registry 中查找,如 Kubernetes、Consul、etcd 或 Zookeeper。模式将服务 registry 的配置与调用服务分开。

Maven 用户必须为要使用的服务 registry 添加依赖项。可能性包括:

  • camel-consul
  • camel-etcd
  • camel-kubenetes
  • camel-ribbon

调用服务的语法

要调用一个服务,请参考服务的名称,如下所示:

from("direct:start")
    .serviceCall("foo")
    .to("mock:result");

以下示例显示了用于调用服务的 XML DSL:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <serviceCall name="foo"/>
    <to uri="mock:result"/>
  </route>
</camelContext>

在这些示例中,Camel 使用与服务 registry 集成的组件来查找服务名为 foo。lookup 返回一组 IP:PORT 对,指向托管远程服务的活跃服务器列表。然后,Camel 会随机选择该服务器,以使用该列表并使用所选 IPPORT 编号构建 Camel URI。

默认情况下,Camel 使用 HTTP 组件。在上例中,调用将解析为由动态 到D 端点调用的 Camel URI,如下所示:

toD("http://IP:PORT")
<toD uri="http:IP:port"/>

您可以使用 URI 参数来调用服务,如 beer=yes

serviceCall("foo?beer=yes")
<serviceCall name="foo?beer=yes"/>

您还可以提供上下文路径,例如:

serviceCall("foo/beverage?beer=yes")
<serviceCall name="foo/beverage?beer=yes"/>

将服务名称转换为 URI

如您所见,服务名称会解析为 Camel 端点 URI。以下是更多示例。 显示 Camel URI 的解析):

serviceCall("myService") -> http://hostname:port
serviceCall("myService/foo") -> http://hostname:port/foo
serviceCall("http:myService/foo") -> http:hostname:port/foo
<serviceCall name="myService"/> -> http://hostname:port
<serviceCall name="myService/foo"/> -> http://hostname:port/foo
<serviceCall name="http:myService/foo"/> -> http:hostname:port/foo

要完全控制已解析的 URI,请提供一个额外的 URI 参数,指定所需的 Camel URI。在指定的 URI 中,您可以使用服务名称,它解析为 IP:PORT。以下是一些示例:

serviceCall("myService", "http:myService.host:myService.port/foo") -> http:hostname:port/foo
serviceCall("myService", "netty4:tcp:myService?connectTimeout=1000") -> netty:tcp:hostname:port?connectTimeout=1000
<serviceCall name="myService" uri="http:myService.host:myService.port/foo"/> -> http:hostname:port/foo
<serviceCall name="myService" uri="netty4:tcp:myService?connectTimeout=1000"/> -> netty:tcp:hostname:port?connectTimeout=1000

上面的示例调用名为 myService 的服务。第二个参数控制已解析的 URI 的值。请注意,第一个示例使用 serviceName.hostserviceName.port 来指代 IP 或 PORT。如果您只指定 serviceName,则会解析为 IP:PORT

配置调用该服务的组件

默认情况下,Camel 使用 HTTP 组件调用该服务。您可以配置使用不同的组件(如 HTTP4 或 Netty4 HTTP)的使用,如下例所示:

KubernetesConfigurationDefinition config = new KubernetesConfigurationDefinition();
config.setComponent("netty4-http");

// Register the service call configuration:
context.setServiceCallConfiguration(config);

from("direct:start")
    .serviceCall("foo")
    .to("mock:result");

以下是 XML DSL 中的示例:

&lt;camelContext xmlns="http://camel.apache.org/schema/spring">
  &lt;kubernetesConfiguration id="kubernetes" component="netty4-http"/>
  &lt;route>
    &lt;from uri="direct:start"/>
    &lt;serviceCall name="foo"/>
    &lt;to uri="mock:result"/>
  &lt;/route>
&lt;/camelContext>

所有实现共享的选项

每个实现都可使用以下选项:

选项

默认值

描述

clientProperty

 

指定特定于您要使用的服务调用实现的属性。例如,如果您使用 ribbon 实现,则客户端属性在 com.netflix.client.config.config.CommonClientConfigKey 中定义。

component

http

设置用于调用远程服务的默认 Camel 组件。您可以配置使用组件,如 netty4-http、jetty、restlet 或一些其他组件。如果服务不使用 HTTP 协议,则必须使用另一个组件,如 mqtt、jms 和 amqp。如果您在 service 调用中指定 URI 参数,则会使用此参数中指定的组件而不是默认值。

loadBalancerRef

 

设置要使用的自定义 org.apache.camel.spi.ServiceCallLoadBalancer 的引用。

serverListStrategyRef

 

设置要使用的自定义 org.apache.camel.spi.ServiceCallServerListStrategy 的引用。

使用 Kubernetes 时的服务调用选项

Kubernetes 实现支持以下选项:

选项

默认值

描述

apiVersion

 

使用客户端查找时的 Kubernetes API 版本。

caCertData

 

使用客户端查找时设置证书颁发机构数据。

caCertFile

 

设置在使用客户端查找时从文件加载的证书颁发机构数据。

clientCertData

 

使用客户端查找时设置客户端证书数据。

clientCertFile

 

设置在使用客户端查找时从文件加载的客户端证书数据。

clientKeyAlgo

 

在使用客户端查找时设置客户端密钥存储算法,如 RSA。

clientKeyData

 

在使用客户端查找时设置客户端密钥存储数据。

clientKeyFile

 

使用客户端查找时,设置从文件加载的客户端密钥存储数据。

clientKeyPassphrase

 

在使用客户端查找时设置客户端密钥存储密码短语。

dnsDomain

 

设置 DNS 域,以用于 dns 查找。

lookup

环境

用于查找该服务的策略选择。lookup 策略包括:

  • 环境 abrt-admission 使用环境变量。
  • DNS 效果为 use DNS 域名。
  • 客户端 abrt-jaxb 使用 Java 客户端调用 Kubernetes 主机 API 并查询哪些服务器正在主动托管该服务。

masterUrl

 

使用客户端查找时 Kubernetes 主机的 URL。

namespace

 

要使用的 Kubernetes 命名空间。默认情况下,命名空间的名称从环境变量 KUBERNETES_MASTER 中获取。

oauthToken

 

使用客户端查找时为身份验证设置 OAUTH 令牌(而不是用户名/密码)。

password

 

在使用客户端查找时设置密码以进行身份验证。

trustCerts

false

设置是否在使用客户端查找时打开信任证书检查。

username

 

在使用客户端查找时设置用于身份验证的用户名。