【问题标题】:One API proxy calling two different target endpoints一个 API 代理调用两个不同的目标端点
【发布时间】:2014-05-12 02:45:36
【问题描述】:

我刚刚开始使用 Apigee。 我想创建一个 API 代理,它将根据“if”条件调用两个目标端点。 我已经创建了一个 API 并为其添加了资源,但问题是在这种情况下我得到了两个 API。 如果 thetype='abc' 目标点应该是 target1 如果 thetype='xyz' 目标点应该是 target2 谁能告诉我如何继续它?

【问题讨论】:

    标签: apigee


    【解决方案1】:

    查看this question 的答案。那里列出了查找 RouteRules 的详细信息。 ProxyEndpoint documentation 也会很有帮助。

    您可以使用此代码完成您正在尝试的事情:

    <RouteRule name="routeToTarget1">
        <Condition>thetype == "abc"</Condition>
        <TargetEndpoint>target1</TargetEndpoint>
    </RouteRule>
    <RouteRule name="routeToTarget2">
        <Condition>thetype == "xyz"</Condition>
        <TargetEndpoint>target2</TargetEndpoint>
    </RouteRule>
    

    这些 RouteRules 将按顺序进行评估。

    请注意,您可能希望底部的 RouteRule 没有条件,这意味着它将始终匹配。当类型不等于“abc”或“xyz”时会发生什么?假设 target1 是默认值,您的代码将如下所示:

    <RouteRule name="routeToTarget2">
        <Condition>thetype == "xyz"</Condition>
        <TargetEndpoint>target2</TargetEndpoint>
    </RouteRule>
    <RouteRule name="routeToTarget1">
        <TargetEndpoint>target1</TargetEndpoint>
    </RouteRule>
    

    【讨论】:

    • 谢谢迈克。我尝试使用您发送的代码,但它给我一个错误:: 上传 API vmife 文件时出错。消息传递.config.beans.InvalidBundle。错误:[实体:代理,未找到目标:url../../;]
    • 'thetype' 这里是 table 中的一列。在这里,我正在为两个目标端点创建一个代理 API。现在这些目标端点是两个不同的 API。我有一个 API 的过滤条件,它在浏览器中运行良好 (url?$filter=thtype eq 123) 现在在我的条件下,我说 thetype==123 但它不起作用。跨度>
    • 如何填充“thetype”变量?我相信你知道,但是 request.queryparam.$filter(可以通过使用 context.getVariable("request.queryparam.$filter") 在 JavaScript 策略中访问)将具有值“thtype eq 123”(使用你在上面评论中的例子)。您需要解析该字符串以获取值 123。
    【解决方案2】:

    如果您使用的是 API 代理编辑器 UI,那么您可以执行以下操作:

    (1) 从 API 代理编辑器工具栏中选择 New / New Resource

    然后您将看到:

    (2) 对于输入字段,可选目标 URL,输入与该资源对应的目标 URL。

    然后,此工具将为该资源生成条件流,您可以选择附加资源特定的策略。

    此工具还将添加所需的路由规则,您生成的 XML 将如下所示:

    <ProxyEndpoint name="default">
        <RouteRule name="Resource-1">
            <Condition>(proxy.pathsuffix MatchesPath &quot;/someResource&quot;) and (request.verb = &quot;GET&quot;)</Condition>
            <HTTPTargetConnection>
                <URL>http://myAlternateEndpoint</URL>
            </HTTPTargetConnection>
        </RouteRule>
        ....
    

    【讨论】:

    • 你好 Randy,这个方法会给我两个 API,但我只需要一个 API 用于两个目标端点。我不想要两个资源。
    猜你喜欢
    • 2014-05-20
    • 2014-04-25
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2020-06-18
    • 1970-01-01
    • 2014-10-21
    相关资源
    最近更新 更多