使用 camel http4 的系统属性代理设置

use system properties proxy settings for camel http4

我尝试使用系统属性的 http4 组件代理设置无济于事。

documentation给出了这个例子:

<camelContext>
    <properties>
        <property key="http.proxyHost" value="172.168.18.9"/>
        <property key="http.proxyPort" value="8080"/>
    </properties>
</camelContext>

但这只是使用硬编码值。

有没有办法在 camelContext 属性中使用占位符?

首先,您需要 PropertiesComponent 来解析 <camelContext> 中的属性:

<bean id="propertiesComponent" class="org.apache.camel.component.properties.PropertiesComponent" />

如果您只需要支持以下其中一项,则无需指定位置:

现在您可以在 camelContext 属性中使用占位符:

<camelContext>
    <properties>
        <property key="http.proxyHost" value="{{http.proxyHost}}"/>
        <property key="http.proxyPort" value="{{http.proxyPort}}"/>
    </properties>
</camelContext>

另一件需要注意的事情是,如果系统 属性 未设置,这将失败。您可以(并且可能应该)在冒号

之后指定一个默认值
<property key="http.proxyHost" value="{{http.proxyHost:}}"/>
<property key="http.proxyPort" value="{{http.proxyPort:}}"/>

以确保它在这两种情况下都有效。