264.26. 使用开箱即用的功能
可从 Camel 2.14.1 开始
Properties 组件包括以下功能开箱即用
-
env
- 从操作系统环境变量中查找属性的功能 -
sys
- 从 Java JVM 系统属性查找属性的功能 -
service
- 使用服务命名 idiom 从 OS 环境变量查找属性的功能 -
service.name
- Camel 2.16.1: 使用服务命名 idiom 仅返回主机名部分,从 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 环境变量定义的服务,以使用 主机名 : 端口
引用服务位置。
- NAME_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>
如果尚未定义该服务,可以使用默认值,例如在本地主机上调用服务,可能用于单元测试等
<camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="direct:start"/> <to uri="http://{`{service:FOO:localhost:8080}`}/myapp"/> </route> </camelContext>