【问题标题】:Service callout with dynamic host name具有动态主机名的服务调出
【发布时间】:2014-11-18 19:21:40
【问题描述】:

我在 Apigee 中有一个代理,它使用服务调出到同一环境中的另一个代理。我想为标注设置 URL 主机以匹配初始请求的主机。

例如,如果在开发环境中发出请求:

https://example-dev.apigee.com/awesome-proxy

我需要打电话给:

https://example-dev.apigee.com/support-proxy

在测试环境中,第一个调用是:

https://example-test.apigee.com/awesome-proxy

支持电话需要转到:

https://example-test.apigee.com/support-proxy

这是我想定义服务调用策略的方式:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="serviceCallout">
    <DisplayName>serviceCallout</DisplayName>
    <FaultRules/>
    <Properties/>
    <Request clearPayload="true" variable="example.request">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>example.response</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>{client.host}/support-proxy</URL>
    </HTTPTargetConnection>
</ServiceCallout>

这不会保存并抱怨没有协议。帮助表明这必须是硬编码的:

<HTTPTargetConnection>/<URL> element
The URL to the service being called. While the hostname portion of URL must be hard-coded, you can supply the remainder of the URL dynamically with a variable.

我找到了一个变量来定义服务调用的 URL:

servicecallout.{policy-name}.target.url

我尝试使用分配消息策略来动态设置变量,如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="assignCalloutURL">
    <DisplayName>assignCalloutURL</DisplayName>
    <FaultRules/>
    <Properties/>
    <AssignVariable>
        <Name>servicecallout.serviceCallout.target.url</Name>
        <Value>{client.host}</Value>
        <Ref/>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

这会将 URL 设置为文字文本 {client.host}

我以类似的方式将分配消息策略用于其他目的,它实际上解决了列出的变量。我不确定这里发生了什么。

【问题讨论】:

    标签: apigee


    【解决方案1】:

    client.host 不是要使用的正确变量,它返回一个 IP 地址192.168...

    我尝试了其他一些变量:

    proxy.url 返回一个奇怪的主机,它看起来像是带有端口的内部 Apigee 机器名称。直接命中proxy.url主机超时。

    我最终使用了virtualhost.aliasesproxy.pathsuffix。这是解决它的完整 JavaScript:

    var base = context.getVariable("proxy.basepath");
    var aliases = context.getVariable("virtualhost.aliases");
    
    var url = "https://" + aliases[0] + base + "/support-proxy";
    
    context.setVariable("servicecallout.serviceCallout.target.url", url);
    

    【讨论】:

      【解决方案2】:

      在处理确保 URL 具有有效值的 linter 时,修改目标 URL 会变得有点愚蠢。

      这不会保存并抱怨没有协议。

      这是因为您在 URL 之前缺少方案(https://http://)(client.host 不包括方案)。

      这会将 URL 设置为文字文本 {client.host}

      该位无法正常工作,因为您需要使用 ref 标记来检索现有变量:

      <AssignVariable>
          <Name>servicecallout.serviceCallout.target.url</Name>
          <Ref>client.host</Ref>
      </AssignVariable>
      

      现在,这可能适用于服务调用,但可能不适用于设置目标 URL。

      我最终创建了 JavaScript 策略来处理 target.url,因为 AssignMessage 对我来说是个问题:

      var scheme = context.getVariable("client.scheme");
      var host = context.getVariable("client.host");
      var pathsuffix = context.getVariable("proxy.pathsuffix");
      var newUrl = scheme + host + pathsuffix;
      
      context.setVariable("target.url", newUrl);
      

      【讨论】:

        【解决方案3】:

        看看this - 它对我有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-04-27
          • 1970-01-01
          • 2023-03-08
          • 1970-01-01
          • 2012-07-02
          • 1970-01-01
          • 2014-03-29
          相关资源
          最近更新 更多