Dynamic routing

This policy dynamically determines the destination for each request.

Dynamic routing selects an endpoint at runtime by evaluating conditions based on the incoming request. The routing decision is made using values from request headers or context variables, allowing the API to direct traffic to different endpoints depending on the request data or execution context.

This approach lets you direct requests to different backend services depending on request data or environment-specific settings.

In IBM API Studio, this policy is identified by the kind Route.

Example of a Dynamic routing policy:

kind: Route
apiVersion: api.ibm.com/v1
metadata:
  name: dynamicRoutingContext
  version: 1.0
  namespace: dev
spec:
  default-endpoint:
    $ref: dev:default_endpoint:1.0
    
  dynamic-endpoints:
    useContext:
      $ref: dev:dynamic_endpoint_context:1.0

---
kind: HTTPEndpoint
apiVersion: api.ibm.com/v1
metadata:
  name: default_endpoint
  version: 1.0
  namespace: dev
spec:
  url: http://localhost:3000/
  method: GET
  connectTimeout: 300
  readTimeout: 200
  sslConfig:
    keyStoreAlias: ""
    keyAlias: ""
    trustStoreAlias: ""
  serviceRegistryParameters:
    - name: ""
      value: ""

---
kind: HTTPEndpoint
apiVersion: api.ibm.com/v1
metadata:
  name: dynamic_endpoint_context
  version: 1.0
  namespace: dev
spec:
  url: http://endpoint1/
  method: GET
  connectTimeout: 101
  readTimeout: 102
  sslConfig:
    keyStoreAlias: "DEFAULT_IS_KEYSTORE"
    keyAlias: "ssos"
    trustStoreAlias: "DEFAULT_IS_TRUSTSTORE"
  serviceRegistryParameters:
    - name: ""
      value: ""

To invoke the Dynamic routing policy, you can specify:

Type Description
spec

You can configure the following policy specifications:

The spec defines the default endpoint and the dynamic routing options. You can configure dynamic routing by selecting an endpoint based on values from the context or from a specific request header.
  • default-endpoint. Specifies the endpoint used when no dynamic routing condition is met. This endpoint is provided as $ref value in the format namespace:endpoint name:version.
  • dynamic-endpoints. Defines the routing rules that select endpoints at runtime.
  • useContext. References a dynamic routing context definition. The routing decision is based on a value stored in the execution context. This endpoint is provided as $ref value in the format namespace:endpoint name:version.
  • useHeader. Routes the request by reading the value of a specific request header. The routing uses both name and $ref:
    • name. Specifies the request header to evaluate.
    • $ref. Points to the routing configuration that maps the header value to an endpoint, in the format: namespace:endpoint name:version

To configure the default_endpoint and dynamic-endpoints, you can specify:

Type Description
spec This denotes various asset properties that you can configure for the endpoint.

These are the endpoint specifications that can be configured:

  • URL. Specifies the URL for the default endpoint.
  • method. Specifies the available routing methods.
  • connectTimeout. Specifies the number of seconds after which connection timeout is initiated.
  • readTimeout. Specifies the number of seconds after which read timeout is initiated.
  • sslConfig. Specifies the SSL configurations.
    You can configure the following SSL details:
    • keyStoreAlias. Specify the keystore alias to identify the API call. This value (along with the value of Client Certificate Alias) is used for conducting an SSL client authentication.
    • keyAlias. Specify the alias of the private key, which should reside in the keystore specified by the keystore alias.
    • trueStoreAlias. Specify the alias for the truststore containing CA certificates used by IBM API Studio to validate the native API’s SSL certificate.
  • serviceRegistryParameters. Specify values that are used for constructing the discovery service URI. Configure the name and value.