Custom route

Updated at:

To add custom routing logic, implement the com.alipay.cloud.sofa.router.SofaRouter interface.

The com.alipay.cloud.sofa.router.SofaRouter interface provides two methods:

  • List<InstanceInfo> route(SofaRouterContext context, List<InstanceInfo> instanceInfos)

    When you implement routing logic in this method:

    • The SofaRouterContext request parameter contains key information about the current request. You can use it to retrieve data, such as request message headers, cookies, and query strings, to identify the request.

    • The List<InstanceInfo> request parameter contains all available providers. InstanceInfo provides the metadata and address information of a provider from the service registry.

      The List<InstanceInfo> passed to this method has already been filtered by previous routing algorithms.

    This method returns the filtered list of providers.

  • int getOrder()

    This method defines the routing priority. A smaller return value indicates a higher priority. For built-in routing, the grayscale route has the highest priority with an order value of 0.

    After you implement the interface, load it as a Spring Bean into the Spring container to activate it.