具有动态主机名的服务标注

时间:2014-11-18 19:21:41

标签: apigee

我在Apigee中有一个代理,它在同一环境中使用服务标注到另一个代理。我想设置callout的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

我尝试使用assign message策略来动态设置变量,如下所示:

<?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}

为了其他目的,我以类似的方式使用了assign message策略,它实际上解析了列出的变量。我不确定这里发生了什么。

3 个答案:

答案 0 :(得分:0)

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

  

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

这是因为您在URL(client.host不包含方案)之前错过了方案(https://http://)。

  

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

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

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

现在,这可能适用于服务标注,但它可能无法用于设置目标网址。

我最终创建了处理target.url的JavaScript策略,因为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);

答案 1 :(得分:0)

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 :(得分:0)

看看this - 它对我有用。

相关问题