spring-cloud-commons
spring-cloud-commons copied to clipboard
Using @RefreshScope on configuration with RestHighLevelClient leads to null restClient
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
Furthermore due to this issue, when healthcheck with actuator on the elasticsearch cluster, it uses the default localhost:9200 instead of the configured host.
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?
No, I remove the @RefreshScope annotation for the time being.
Have you tried passing the dependencies as method parameters rather than method calls?
This doesn't fix the problem with the null client.
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.
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.
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.