Script Router

Script Router

How to use

Prerequisites

  • Docker and Docker Compose environment to deploy Nacos registry.
  • Nacos Version 2.x+
  • Go 1.23+

Run Nacos

Follow this instruction to install and start Nacos server.

Script router

Similar to the condition router, the script router enables traffic control using expressions. However, while it offers more powerful matching capabilities, this comes at the cost of higher resource consumption. Therefore, it should be used sparingly in production environments.

The example code for the script router is similar to that of the condition router, with slight differences in the Nacos configuration. Therefore, only the Nacos configuration is provided here.

scope: "application"
key: "script-server"
enabled: true
type: "javascript"
script: |
  (function(invokers, invocation, context) {
    if (!invokers || invokers.length === 0) return [];
    return invokers.filter(function(invoker) {
      var url = invoker.GetURL();
      return url && url.Port === "20000";
    });
  })(invokers, invocation, context);  

For the complete example, please see: Full Example Code.