2

I have implemented a ReactiveDiscoveryClient discovery class in a Spring Cloud Gateway. How do I get it to refresh?

In the POC code below it only logs once inside the Flux.defer statement (in the real code this will call another service to get active clients). I want it do update at a regular interval...

@Component
public class DemoReactiveDiscoveryClient implements ReactiveDiscoveryClient {
    private static Logger log = LoggerFactory.getLogger(DemoReactiveDiscoveryClient.class);

    @Override
    public String description() {
        return "DemoReactiveDiscoveryClient";
    }

    @Override
    public Flux<ServiceInstance> getInstances(String serviceId) {
        return Flux.defer(() -> {

            log.info("DemoReactiveDiscoveryClient getInstances"); // this log line only is called once
            var serviceInstances = List.of(
                    new DefaultServiceInstance("say-hello" + "1", serviceId, "localhost", 8000, false),
                    new DefaultServiceInstance("say-hello"+ "2", serviceId, "localhost", 8001, false)
            );
            return Flux.fromIterable(serviceInstances);
        });
    }

    @Override
    public Flux<String> getServices() {
        return Flux.fromIterable(List.of("" +
                "say-hello"));

    }
}

AppConfig.java:

@Configuration
public class AppConfig {

    @Bean
    ReactiveDiscoveryClient reactiveDiscoveryClient() {
        return new DemoReactiveDiscoveryClient();
    }

    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
            ConfigurableApplicationContext context) {
        return ServiceInstanceListSupplier.builder()
                .withDiscoveryClient()
                .withZonePreference()
                .withCaching()
                .build(context);
    }
}

application.yml file:

server:
  port: 8888
loadbalancer:
  client:
    name: say-hello
spring:
  application:
    name: gw-s1
  cloud:
    enabled: true
    failFast: true
    loadbalancer:
      ribbon:
        enabled: false
      health-check:
        default:
          path: /health
        say-hello:
          path: /health
        retry-on-all-operations:
      retry:
        avoid-previous-instance:

    gateway:
      routes:
        - id: say-hello
          uri: lb://service
          predicates:
            - Path=/service/**
          filters:
            - StripPrefix=1
    refresh:
      enabled:
logging:
  level:
    root: DEBUG

Also I tried implementing ServiceInstanceListSupplier as well and this does update regularly but it doesn't have health-check and perhaps other things.

Any ideas appreciated :)

Liam
  • 100
  • 2
  • 6

0 Answers0