1 Two modes of route configuration

  • Yml file configuration

    You can configure the YML file by referring to the official website. The following is an example of simplified configuration:

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - Cookie=mycookie,mycookievalue
    Copy the code

    A complete example:

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - name: Cookie
              args:
                name: mycookie
                regexp: mycookievalue
    Copy the code

    Configuration of the Spring Cloud Gateway official website

  • code

    @SpringBootApplication
    public class DemogatewayApplication {
    	@Bean
    	public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    		return builder.routes()
    			.route("path_route", r -> r.path("/get")
    				.uri("http://httpbin.org"))
    			.route("host_route", r -> r.host("*.myhost.org")
    				.uri("http://httpbin.org"))
    			.route("rewrite_route", r -> r.host("*.rewrite.org")
    				.filters(f -> f.rewritePath("/foo/(? 
            
             .*)"
            ."/${segment}"))
    				.uri("http://httpbin.org"))
    			.route("hystrix_route", r -> r.host("*.hystrix.org")
    				.filters(f -> f.hystrix(c -> c.setName("slowcmd")))
    				.uri("http://httpbin.org"))
    			.route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
    				.filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
    				.uri("http://httpbin.org"))
    			.route("limit_route", r -> r
    				.host("*.limited.org").and().path("/anything/**")
    				.filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
    				.uri("http://httpbin.org")) .build(); }}Copy the code

    Configure programmatically.

In both cases, routes are written to death without dynamic loading. Let’s see how to add a router dynamically.

2 Dynamic Routing

Spring Cloud Gateway source code version 2.2.6.RELEASE

Dynamic routes can be configured to load dynamic routes. First, analyze the initialization process of the route through the source code.

The default is to load from the configuration file. This is also the configuration of the official website. The definition of the route is finally encapsulated by the RouteDefinition class. All routes are loaded and assembled and stored in memory when the project starts:

The added data is stored in memory. Load from file configuration and load from memory without user customization. There is always no routing defined in the memory of the gateway project just after startup.

As long as the implementation RouteDefinitionRepository interface can load custom storage location.

How to add? The interface is readily available on the Spring Cloud Gateway website.

The Actuator API contains dynamic adding and querying interfaces for the Gateway:

Of course also can consult GatewayControllerEndpoint implementation

I am lazy to use ready-made. Combined with the storage location of the above implementation can be implemented to dynamically add the router.

Note: use interface/gateway/routes / {id_route_to_create} (POST) to add after routing, you immediately call/physical/gateway/routes / {id} (GET) is to GET less than just adding routing. The reason is that routes are fetched from the cache. Need to call after adding/physical/gateway/refresh (POST) to clear the cache. This reloads the cache to retrieve the new router information.