0

We using custom gateway filter for spring cloud gateway. The configuration looks like below: Filter class:

@Component
public class CustomFilter extends AbstractGatewayFilterFactory<CustomFilter.Config> {

    public static class Config {
        private String type;
        private LinkedMultiValueMap<String, String> values;
        //Getters & Setters
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            //some filter function
        };
    }
}
spring:
  cloud:
    gateway:
      routes:
        - id: my-service
          uri: https://myservice.host.com
          predicates:
            - Path=/api/{version}/api-name
          filters:
            - name: CustomFilter
              args:
                type: type1
                values:
                  key1: value1

We want to add additional key value pair under values, like:

                values:
                  key1: value1
                  key2: value2

I want to add additional key value pair and update configuration dynamically. I tried both /actuator/refresh and /actuator/gateway/refresh to update configuration but neither of them worked. Can someone help on how to update these values dynamically?

1 Answer 1

0

Option 1: change properties programatically

You can access properties in runtime, change them and publish RefreshRoutesEvent to make Gateway reload properties set in CachingRouteLocator

@Autowired GatewayProperties gatewayProperties;
@Autowired ApplicationEventPublisher eventPublisher;

public void someMethod() {
    List<FilterDefinition> filters = this.gatewayProperties.getRoutes().get(0).getFilters();
    // update filters however
    
    // this will make RouteLocator reload properties
    this.eventPublisher.publishEvent(new RefreshRoutesEvent(this));
}

Option 2: reload properties file and recreate beans

If you definetely want to do it in yaml it will be harder, since reading of the properties / yaml files is done once at startup. You can create a scheduler to reload the properties as shown here but you still need to figure out the way to make GatewayProperties to reread / rebind values and still will endup having to trigger the RefreshRoutesEvent.


Option 3: Implement your own RouteLocator

An alternative solution could be to provide your own solution of RouteLocator and make it reread properties each time

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.