API Gateways with Dedicated Backends
We can extend Multiple Gateway environments feature by utilizing dynamic endpoint capabilities of WSO2 API Manager to have each gateway point to a different back-end endpoint. API Gateway is the actual runtime of the APIs to which are developed and Published from the API Publisher. WSO2 API Manager is capable of publishing APIs to different gateways were the API users connect to those API Gateways in order to do the actual API calls through the applications they are subscribed. Refer Maintaining Separate Production and Sandbox Gateways for more information on publishing to multiple gateways.
However, when the API publisher can only provide a single static Endpoint for an API in the implementation. Therefore, which ever the gateway that the API is deployed, the API call is directed to a single endpoint as depicted in the diagram below.
But in most situations, you would want to have each Gateway proxying a dedicated Back-End API. To provide that capability WSO2 API Manager provides ability to specify dynamic endpoint URLs at the time of specifying the API endpoint URL. This UEL will be resolved at the runtime with the details (host and port) we specify in the statup of each gateway. With that each gateway will point to dedicated backend APIs as depicted in the digram below.
Configuring Dynamic Endpoints
Follow the below step to Configure dynamic endpoint as the API endpoint.
- Start WSO2 API Manager Server which includes the API Publisher Component and create an API.
- Go to the implement stage of the API and replace Host and Port of the API endpoint with {uri.var.host} and {uri.var.port} respectively as shown below.
Save and Publish API.
Refer Create and Publish an API for more information on creating and Publishing the API.
Go to <AM_HOME>/repository/deployment/server/synapse-configs/sequences directory of each Gateway and create the following sequence.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In"> <property name="uri.var.host" expression="get-property('system','host')" /> <property name="uri.var.port" expression="get-property('system','port')" /> </sequence>
We use Java system properties which will be set at the server start-up process of each Gateway to resolve the variables which are defined as properties in this sequence.
Alternatively you can resolve this host and port using a class mediator. To do that follow the below steps as an alternative of step 4.
1. Create a java class extending the AbstractMediator class of org.synapse.core as below and create the jar file out of it.
import org.apache.synapse.MessageContext; import org.apache.synapse.mediators.AbstractMediator; public class EnvironmentResolver extends AbstractMediator { @Override public boolean mediate(MessageContext messageContext) { String host = System.getProperty("environment.host"); String port = System.getProperty("environment.port"); messageContext.setProperty("uri.var.host", host); messageContext.setProperty("uri.var.port", port); return true; } @Override public boolean isContentAware(){ return false; } }
2. Add the created jar into <AM_HOME>/repository/components/lib folder of each Gateway. You can download a sample jar file created from here.
3. Add following sequence to <AM_HOME>/repository/deployment/server/synapse-configs/sequences folder of each Gateway.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In"> <class name="org.wso2.carbon.env.EnvironmentResolver"/> </sequence>
org.wso2.carbon.env.EnvironmentResolver is the fully qualified name of the class that contains the code responsible for converting system variables into properties. It is a special class we created which needs to be extended from the org.apache.synapse.mediators.AbstractMediator class and requires overriding the 'mediate' function.
Execute the following command when starting up each Gateway to set the system variables at the server start up from within the <APIM_HOME>/bin directory by replacing the following values.
<ip_of_backend_environment> <port_of_backend_environment> host IP of the Gateway port where the Gateway is running in the dedicated machine or VM ./wso2server.sh -Dhost=<ip_of_backend_environment> -Dport=<port_of_backend_environment>
If you have used the class mediator to configure API Gateways in step 4, use the below commands instead of above described in this step.
./wso2server.sh -Denvironment.host=<ip_of_backend_environment> -Denvironment.port=<port_of_backend_environment>
Now the Gateways have started with the dedicated backend host/port combinations.
Invoke the API.
You will get the response from the API which has sent to the dedicated backend through the Gateway that this API is published.