当前位置: 首页 > 知识库问答 >
问题:

Apache Camel Spring DSL,引用环境变量HOSTNAME

公良光熙
2023-03-14

我正在使用Apache Camel 2.16.0和Spring DSL我有一个Spring上下文XML,我在其中定义了一个Property PlaceHolder来从各种文件中读取属性,如下所示-

    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="false"/>
    <property name="locations">
        <list>
            <value>classpath:/properties/versioning.properties</value>
            <value>classpath:/properties/#{inetAddress.hostName}.properties</value>
        </list>
    </property>
</bean>
<bean id="inetAddress" class="java.net.InetAddress" factory-method="getLocalHost"/>

属性值用于构造其他bean,例如-

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    <property name="url" value="${${LIVE_}DATASOURCE_URL}"/>
    <property name="username" value="${${LIVE_}DATASOURCE_USERNAME}"/>
    <property name="password" value="${${LIVE_}DATASOURCE_PASSWORD}"/>
</bean>

这工作正常,我可以看到正在创建的bean。我在同一个应用程序中还有另一个Spring Context XML,它具有骆驼上下文,我想使用相同属性文件中定义的其他一些属性。我知道camel支持Spring Property占位符,请参阅下面的上下文摘录-

<camelContext id="charge-process-context" xmlns="http://camel.apache.org/schema/spring">
    <propertyPlaceholder id="properties" 
        location="classpath:/properties/versioning.properties,
                             properties/${env:HOSTNAME}.properties"
        xmlns="http://camel.apache.org/schema/spring" />
    .....
    .....
</camelContext>

作为上下文的一部分,我有一个使用https组件的路由,该组件使用属性文件中的值,如下所示-

<to uri="https4:{{LIVE_AUTH_RESPONSE_HOST}}:{{LIVE_AUTH_RESPONSE_PORT}}/{{LIVE_AUTH_RESPONSE_CONTEXT_PATH}}"/>

此路由不启动并引发以下异常-

Caused by: java.lang.IllegalArgumentException: Cannot find system environment with key: HOSTNAME
    at org.apache.camel.util.FilePathResolver.resolvePath(FilePathResolver.java:54)
    at org.apache.camel.component.properties.PropertiesComponent.parseLocations(PropertiesComponent.java:434)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:163)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:148)
    at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:2261)
    at org.apache.camel.model.ProcessorDefinitionHelper.resolvePropertyPlaceholders(ProcessorDefinitionHelper.java:730)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:427)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:413)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:165)
    at org.apache.camel.model.ExpressionNode.createFilterProcessor(ExpressionNode.java:109)
    at org.apache.camel.model.WhenDefinition.createProcessor(WhenDefinition.java:74)
    at org.apache.camel.model.WhenDefinition.createProcessor(WhenDefinition.java:32)
    at org.apache.camel.model.ProcessorDefinition.createProcessor(ProcessorDefinition.java:483)
    at org.apache.camel.model.ChoiceDefinition.createProcessor(ChoiceDefinition.java:135)
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:534)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:495)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:219)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1069)

请注意:我正在将我的应用程序作为war文件部署到AWS实例上的Tomcat 8上。我在Windows 10上有一个开发环境,我发现它可以在Windows操作系统上运行。我还看到了文件FilePathResolver。Apache Camel 2.16中的java使用系统。getenv(键)以获取值,即系统。getenv(“主机名”),它在AWS实例上返回空值,在Windows 10上返回正确值。我也尝试过使用env:hostname(unix的小写字母),但还是没有成功。。。

共有1个答案

养星汉
2023-03-14

我在http://camel.apache.org/using-propertyplaceholder.html的Bridging Spring和Camel Property占位符找到了解决方案

它提到以下内容-

Spring框架不允许第三方框架(如Apache Camel)无缝地连接到Spring属性占位符机制中。然而,通过声明类型为org的Springbean,您可以轻松地将Spring和Camel连接起来。阿帕奇。骆驼Springspi。BridgePropertyPlaceHolderConfigure,这是一个Spring组织。springframework。豆。工厂配置。PropertyPlaceHolderConfiguration类型。

 类似资料:
  • Git 总是在一个 bash shell 中运行,并借助一些 shell 环境变量来决定它的运行方式。 有时候,知道它们是什么以及它们如何让 Git 按照你想要的方式去运行会很有用。 这里不会列出所有的 Git 环境变量,但我们会涉及最有的那部分。 全局行为 像通常的程序一样,Git 的常规行为依赖于环境变量。 GIT_EXEC_PATH 决定 Git 到哪找它的子程序 (像 git-commit

  • Puppeteer 寻找某些环境变量来帮助其操作。 如果 puppeteer 在环境中没有找到它们,这些变量的小写变体将从 npm 配置 中使用。 HTTP_PROXY, HTTPS_PROXY, NO_PROXY - 定义用于下载和运行 Chromium 的 HTTP 代理设置。 PUPPETEER_SKIP_CHROMIUM_DOWNLOAD - 请勿在安装步骤中下载绑定的 Chromium。

  • 环境变量 vue-element-admin 4.0 之后是基于 vue-cli来进行构建,所以所有的环境变量配置都是基于vue-cli来实现和控制的。 官方文档 .env # 在所有的环境中被载入 .env.[mode] # 只在指定的模式中被载入 一个环境文件只包含环境变量的“键=值”对: FOO=bar VUE_APP_SECRET=secre

  • 一些 Electron 的行为受到环境变量的控制,因为他们的初始化比命令行和应用代码更早. POSIX shells 的例子: $ export ELECTRON_ENABLE_LOGGING=true $ electron Windows 控制台: > set ELECTRON_ENABLE_LOGGING=true > electron ELECTRON_RUN_AS_NODE 类似nod

  • 设置变量,可以不必更改代码的控制应用程序配置和行为。 某些Electron行为由环境变量控制,因为它们比命令行和代码更早地初始化。 POSIX shell示例: 1 $ export ELECTRON_ENABLE_LOGGING=true 2 $ electron Copied! Windows控制台示例: 1 > set ELECTRON_ENABLE_LOGGING=true 2 > ele

  • 在构建或者代码在端上运行中需要一些跟区分于环境的变量,用于配置构建流程或者运行时过程,这时候我们可以配置环境变量。 设置环境变量 执行命令时添加 比如: # OS X, Linux PORT=3000 fes dev # Windows (cmd.exe) set PORT=3000 && fes dev 如果要同时考虑 OS X 和 Windows,可借助三方工具 cross-en