264.26. 使用开箱即用的功能
从 Camel 2.14.1 开始提供
Properties 组件包括开箱即用的以下功能
-
env- 用于从 OS 环境变量查找属性的功能 -
sys- 从 Java JVM 系统属性查找属性的功能 -
service- 使用服务命名 idiom 从 OS 环境变量查找属性的功能 -
service.name- Camel 2.16.1 : 使用服务命名 idiom 返回 hostname 部分的从 OS 环境变量中查找属性的功能 -
service.port- Camel 2.16.1: 使用服务命名 idiom 仅返回端口部分的从 OS 环境变量中查找属性的功能
正如您所见,这些功能旨在方便从环境中查找值。在开箱即用的情况下,可以轻松使用它们,如下所示:
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="direct:start"/>
<to uri="{`{env:SOMENAME}`}"/>
<to uri="{`{sys:MyJvmPropertyName}`}"/>
</route>
</camelContext>
您还可以使用默认值,如果属性不存在,您可以定义一个默认值,如下所示,其中默认值是 log:foo 和 log:bar 值。
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="direct:start"/>
<to uri="{`{env:SOMENAME:log:foo}`}"/>
<to uri="{`{sys:MyJvmPropertyName:log:bar}`}"/>
</route>
</camelContext>
服务功能用于查找使用服务命名 idiom 的 OS 环境变量定义的服务,以使用 主机名 : 端口引用服务位置:
- 名称_SERVICE_HOST
- NAME_SERVICE_PORT
换句话说,服务使用 _SERVICE_HOST 和 _SERVICE_PORT 作为前缀。因此,如果服务命名为 FOO,则 OS 环境变量应设置为
export $FOO_SERVICE_HOST=myserver
export $FOO_SERVICE_PORT=8888
例如,如果 FOO 服务远程 HTTP 服务,则我们可以引用 Camel 端点 uri 中的服务,并使用 HTTP 组件进行 HTTP 调用:
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="direct:start"/>
<to uri="http://{`{service:FOO}`}/myapp"/>
</route>
</camelContext>
如果服务尚未定义,我们可以使用默认值,例如在 localhost 上调用服务,对于单元测试等,我们可以使用默认值。
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="direct:start"/>
<to uri="http://{`{service:FOO:localhost:8080}`}/myapp"/>
</route>
</camelContext>