Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using @RefreshScope on configuration with RestHighLevelClient leads to null restClient #852

Open
gh-axel-czarniak opened this issue Nov 19, 2020 · 8 comments

Comments

@gh-axel-czarniak
Copy link

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

@gh-axel-czarniak
Copy link
Author

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

@maximzmushko
Copy link

maximzmushko commented Feb 8, 2021

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?

@gh-axel-czarniak
Copy link
Author

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

@spencergibb
Copy link
Member

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

@gh-axel-czarniak
Copy link
Author

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

@spencergibb
Copy link
Member

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.

@spring-cloud-issues
Copy link

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.

@gh-axel-czarniak
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants