How can you use Azure API Management policy to loa

2019-08-17 04:26发布

问题:

How can you use policy to load balance calls to a pair of backend services? (in this case a pair of Logic Apps in different regions)

I've read through the API Management policies and can see something around the control flow but I can't work out how to (a) test the back-end service is available, and then (b) change the call to the backup service if the primary is unavailable

<backend>  
     <forward-request/>  
</backend> 

回答1:

One more way to achieve this could be that you can use the retry policy with set backend service or send-request

with something like

<backend>
    <retry condition="@(context.Response.StatusCode == 400 || context.Response.StatusCode >= 500)" count="10" interval="10" max-interval="100" delta="10" first-fast-retry="false">
        <choose>
            <when condition="@(context.Response != null && (context.Response.StatusCode == 400 || context.Response.StatusCode >= 500)">
                <set-backend-service base-url="http://echoapibackup.cloudapp.net/api" />
            </when>
            <otherwise>
                <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
            </otherwise>
        </choose>
        <forward-request />
    </retry>
</backend>

This will in case your primary backend returns an error, will keep retrying on your backup backend.



回答2:

Look into using send-request policy. With it (and wait policy) you could make parallel calls to a couple of web services and return result from one that completes first. That would mean that you need to skip forward-request altogether as you'll be getting result data from these policies.

Or you could use send-request to test if certain backend is available and then use set-backend-service and/or rewrite-uri policies to change destination backend. In that case you'll be keeping forward-request.