javaspringspring-webfluxspring-webclientreactor-netty

Spring WebClient how to reduce DNS cache TTL?


I'm using WebClient (from Spring-Weblux) and currently it caches DNS for 30 mins.

I don't see any information how to reduce this DNS cache. How can I reduce it to 30sec?

HttpClient client = HttpClient.create().responseTimeout(Duration.ofMillis(connectionTimeout))
                           .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout);


WebClient serviceWebClient = WebClient.builder().clientConnector(neweactorClientHttpConnector(client))
                           .baseUrl(deployedServiceHost).build();

Solution

  • Everything that is related to DNS can be configured with .resolver()

    there is method cacheMaxTimeToLive() in NameResolverSpec, from java doc:

    Sets the max time to live of the cached DNS resource records (resolution: seconds). If the time to live of the DNS resource record returned by the DNS server is greater than this max time to live, this resolver will ignore the time to live from the DNS server and will use this max time to live. Default to NameResolverProvider.Build.DEFAULT_CACHE_MAX_TIME_TO_LIVE.

    So, try this:

    HttpClient.create()
            .resolver(nameResolverSpec -> nameResolverSpec.cacheMaxTimeToLive(Duration.ofSeconds(...)));