spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

Using @RefreshScope on configuration with RestHighLevelClient leads to null restClient

Open axelczk opened this issue 4 years ago • 8 comments

Describe the bug With spring data elasticsearch, when we used @RefreshScope on the configuration, the restClient is null and thus we can't perform operation on the cluster. Without this, it work perfectly fine.

Spring version: spring boot starter parent 2.3.5 spring cloud config : 2.2.4 spring boot data elasticsearch 4.0.5

Sample

@Configuration
@EnableElasticsearchRepositories
public class ElasticConfig {

    @Bean
    @RefreshScope
    public ElasticProperties elasticProperties() {
        return new ElasticProperties();
    }

    @Bean
    @RefreshScope
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(elasticProperties().getClusterNodes()).build();
        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    @RefreshScope
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}

Thanks

axelczk avatar Nov 19 '20 15:11 axelczk

Furthermore due to this issue, when healthcheck with actuator on the elasticsearch cluster, it uses the default localhost:9200 instead of the configured host.

axelczk avatar Dec 10 '20 12:12 axelczk

Describe the bug With spring data elasticsearch, when we used @RefreshScope on the configuration, the restClient is null and thus we can't perform operation on the cluster. Without this, it work perfectly fine.

Spring version: spring boot starter parent 2.3.5 spring cloud config : 2.2.4 spring boot data elasticsearch 4.0.5

Sample

@Configuration
@EnableElasticsearchRepositories
public class ElasticConfig {

    @Bean
    @RefreshScope
    public ElasticProperties elasticProperties() {
        return new ElasticProperties();
    }

    @Bean
    @RefreshScope
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(elasticProperties().getClusterNodes()).build();
        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    @RefreshScope
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}

Thanks

Hi, did you manage to find a workaround?

maximzmushko avatar Feb 08 '21 10:02 maximzmushko

No, I remove the @RefreshScope annotation for the time being.

axelczk avatar Feb 08 '21 11:02 axelczk

Have you tried passing the dependencies as method parameters rather than method calls?

spencergibb avatar Feb 17 '21 21:02 spencergibb

This doesn't fix the problem with the null client.

axelczk avatar Feb 18 '21 10:02 axelczk

I'm unable to reproduce this. Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file.

spencergibb avatar Mar 30 '21 21:03 spencergibb

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Apr 06 '21 21:04 spring-cloud-issues

Here is a full exemple. Launch the application and you will see in the startup logs:

WARN 8352 --- [           main] .d.e.r.s.AbstractElasticsearchRepository : Cannot create index: null

This mean the restHighLevelClient used to create the index is null.

TestSpring.zip

axelczk avatar Apr 07 '21 09:04 axelczk