opticyclic
opticyclic

Reputation: 8106

How Do I Configure API Management With Multiple Back Ends?

I am hosting a service for clients where we have the same application deployed per client in Azure.

It is essentially this per client:

Application Gateway ->
App Service Environment ->
API Management ->
VM with Application back end ->
Remote Azure SQl Data

The first 3 layers increase the costs quite significantly.

Is it possible to configure it like this?

Application Gateway ->
App Service Environment ->
API Management ->
    - Client 1 
        VM1 with Application back end ->
        Remote Azure SQl Data1
    - Client 2 
        VM2 with Application back end ->
        Remote Azure SQl Data2
    - Client 3 
        VM3 with Application back end ->
        Remote Azure SQl Data3

i.e. the web layers route to the appropriate back end somehow

e.g. Maybe each client would access the web layer with a different URL.

http://client1.rest-application.azure.com

http://client2.rest-application.azure.com

but they all go through the same application gateway.

Upvotes: 0

Views: 5101

Answers (1)

msrini-MSIT
msrini-MSIT

Reputation: 1502

You need to use APIM policy to route the traffic to different backend pool based on the HTTP header information.

Here is the sample policy which routes the traffic based on the request body size.

The policy defined in this file demonstrates how to route requests based on the size of the message body. Content-Length header contains the size of the message body. 256 KB, a limitation on message size in the Azure Service Bus. he snippet checks if the message is smaller than 256000 bytes. If it's larger, request is routed somewhere else. Copy the following snippet into the inbound section.

<policies>
<inbound>
    <base/>
        <set-variable name="bodySize" value="@(context.Request.Headers["Content-Length"][0])"/>
        <choose>
            <when condition="@(int.Parse(context.Variables.GetValueOrDefault<string>("bodySize"))<256000)">
                <!-- let it pass through by doing nothing -->
            </when>
            <otherwise>
                <rewrite-uri template="{{alternate-path-and-query}}"/>
                <set-backend-service base-url="{{alternate-host}}"/>
            </otherwise>
        </choose>

        <!-- In rare cases where Content-Length header is not present we'll have to read the body to get its length. -->
        <!--
        <choose>
            <when condition="@(context.Request.Body.As<string>(preserveContent: true).Length<256000)">

            </when>
            <otherwise>
                <rewrite-uri template=""/>
                <set-backend-service base-url=""/>
            </otherwise>
        </choose>
        -->
</inbound>
<backend>
    <base/>
</backend>
<outbound>
    <base/>
</outbound>
<on-error>
    <base/>
</on-error>

Upvotes: 1

Related Questions