【问题标题】:Mule PlaceHolder Documentation is wrong?Mule PlaceHolder 文档有误?
【发布时间】:2017-07-11 05:16:34
【问题描述】:

我希望使用正确的属性方式在多个环境中部署......但该示例不起作用: 网址:https://docs.mulesoft.com/mule-user-guide/v/3.8/deploying-to-multiple-environments

我创建了一个配置了 http 的项目,如示例中所示:

<http:listener-config name="HttpListenerConfiguration"
                    doc:name="HTTP Listener Configuration"
                    host="${mule.env.host}"
                    port="${mule.env.port}"
                    basePath="${mule.env.basePath}" />

他们说的地方

这个例子使用了 Spring 的属性占位符解析机制。变量位清晰可见:基本路径、主机和端口可能因部署此连接器的每个环境而异。部署应用程序时,mule.env 可以动态替换为您要部署到的特定环境,例如作为 qa 或 prod。

我在 java/main/resources 中创建了一个名为 config.properties 的文件:

dev.basePath=test/products
dev.host=localhost
dev.port=8082
prod.basePath=products
prod.host=www.acme.com
prod.port=8081

并设置占位符:

<context:property-placeholder location="config.properties"/>

我的 mule-app.properties:

mule.env=dev

当我在 AS 中运行它时:

[Could not resolve placeholder 'mule.env.host' in string value "<http:listener-config name="ejemplop....]

如果我从以下位置更改值:

 ${mule.env.host} to ${${mule.env}.host} 

并且主机属性有效,但如果我对端口做同样的事情

(${${mule.env}.port} 

给我一​​个错误

[..${${mule.env}.port}' is not a valid value of union type 'substitutableInt'....]

但如果我添加到我的属性文件中

env.port=${${mule.env}.port}

并将连接器更改为:

<http:listener-config name="HttpListenerConfiguration"
                    doc:name="HTTP Listener Configuration"
                    host="${${mule.env}.host}"
                    port="${env.port}"
                    basePath="${mule.env.basePath}" />

它的作品。 有什么花哨或正确的方法吗?

【问题讨论】:

    标签: java spring deployment mule placeholder


    【解决方案1】:

    出于实际目的,我认为您可以忽略提及 Spring。在幕后,我认为全球占位符使用该技术。以下是对我有用的简单步骤:

    1. 在 mule-project.xml 中,创建一个 mule.env 变量,该变量具有一个值或 dev 或 qa,例如。
    2. 选择您的项目 xml,转到 Global Elements 选项卡,然后创建一个全局属性占位符。

    1. 将位置设置为:

      ${mule.env}.properties

    2. 在 /src/main/app 中创建一个 qa.properties 文件并定义属性:

      mule.env.host=localhost

      mule.env.port=8082

    3. 在 src/main/app 中创建一个 dev.properties 文件并定义属性:

      mule.env.host=localhost

      mule.env.port=8081

    4. 在HTTP监听配置中,设置主机和端口如下:

      ${mule.env.host}

      ${mule.env.port}

    当您使用步骤 1 中的 qa 环境变量运行应用程序时,您会从端口 8081 启动应用程序。将环境变量更改为 dev 并重新部署应用程序。现在您从端口 8082 启动应用程序。

    这是示例 XML:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:spring="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd">
        <context:property-placeholder location="${mule.env}.properties"/>
        <http:listener-config name="HTTP_Listener_Configuration" host="${mule.env.host}" port="${mule.env.port}" doc:name="HTTP Listener Configuration"/>
        <http:request-config name="HTTP_Request_Configuration" host="jsonplaceholder.typicode.com" port="80" doc:name="HTTP Request Configuration"/>
        <flow name="myprojectFlow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
            <http:request config-ref="HTTP_Request_Configuration" path="/users" method="GET" doc:name="HTTP"/>
        </flow>
    </mule>
    

    【讨论】:

    • 非常好,这就是我现在在其他项目中使用它的方式。我的错误是认为“spring or Mule”将在 ${mule.env.host} 中仅替换“${mule.env”部分
    【解决方案2】:

    CHo,您正朝着多环境方向前进,但还没有完全实现。如果您需要多个属性文件,我会建议什么,每个属性文件具有相同的属性,具有相同的名称但特定于环境的值。

    在你的应用程序中,你会有类似的东西:

    <context:property-placeholder location="config_${mule.env}.properties"/>
    

    那么在您的流程中,您将拥有:

    <http:listener-config name="HttpListenerConfiguration"
                        doc:name="HTTP Listener Configuration"
                        host="${host}"
                        port="${port}"
                        basePath="${basePath}" />
    

    然后您将拥有一个名为 config_dev.properties 的属性文件,其中您将拥有:

    basePath=test/products
    host=localhost
    port=8082
    

    您将拥有另一个名为 config_prod.properties 的属性文件:

    basePath=products
    host=www.acme.com
    port=8081
    

    当 mule.env 设置为 dev 时,将使用 config_dev 值。当设置为 prod 时,使用 config_prod 值。

    【讨论】:

    • 非常好,这就是我现在在其他项目中使用它的方式。我的错误是认为“spring or Mule”将在 ${mule.env.host} 中仅替换“${mule.env”部分
    • @CHo 没问题。您尝试的问题在于,解码需要多次传递,并且可能需要不同的语法来告诉 VM 执行操作的顺序。即使有效的方法也需要多次传递,但逻辑足够简单,解释器可以解码它并首先进行占位符替换,而不是您尝试的嵌套替换。在处理此类问题时要记住一件事,mule 与 spring:Mule 是建立在 spring 之上的。 mule 可能存在一些细微的语法差异,但它正在实现 spring 上下文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2023-03-19
    • 2016-05-26
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多