Spring Boot - Disable embedded ElasticSearch without removing code or changing POM

10,152

Solution 1

Add the following exclude to your @SpringBootApplication

import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;

@SpringBootApplication(exclude = ElasticsearchAutoConfiguration.class)

Solution 2

In your local application.properties

spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration

In production override to force the connection

spring.data.elasticsearch.cluster-nodes = someip:9300
spring.autoconfigure.exclude = none

Probability you will need to make ElasticsearchTemplate @Lazy or put it under a @Profile("production") class

Solution 3

In case your are using Java High Level REST Client, then the solution is as the following:

@EnableAutoConfiguration(exclude = RestClientAutoConfiguration.class)

Solution 4

I found this way to disable/enable Elasticsearch in Spring Boot

in application.properties:

elastic_enable=false

in elastic config file:

@Value("${elastic_enable}") boolean elastic_enable;

@Bean public Client a() throws UnknownHostException {
    if (elastic_enable) 
        return TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elastic_host), 9300));
    else 
        return TransportClient.builder().build();
}

Solution 5

I have just disabled embedded elasticsearch in spring boot 1.5.8 by adding the following exclude to my @SpringBootApplication.

Hope this can help.

import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;

@SpringBootApplication(exclude = {ElasticsearchAutoConfiguration.class,ElasticsearchDataAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class})
Share:
10,152
Wouter
Author by

Wouter

Senior Magento Developer

Updated on June 24, 2022

Comments

  • Wouter
    Wouter almost 2 years

    I'm looking for a way to prevent ElasticSearch for starting (embedded or separate server) in a Spring Boot project. ES is currently not in use, but will be at a later stage in the project.

    If I remove the lines from the POM, my code needs major updates, because all annotations to ES cannot be found anymore.

    Is there a way I can keep my project intact, but prevent ES from launching (embedded)?

    My goal is to speed up restarts for the time being, when ES is not in use.

    Or course, I could also run ES as a separate server, but I don't want to spend the time.

    Thanks

  • Wouter
    Wouter over 7 years
    I have added this, but I can still see ES being run. The console reports things like: 2016-12-19 13:22:13.110 WARN 56711 --- [anagement][T#1]] o.e.cluster.routing.allocation.decider : [Adaptoid] high disk watermark [90%] exceeded on [zc2jESkCR1OXqOoA6eri5g][Adaptoid][/.../data/elasticsearch/n‌​odes/0] free: 46.1gb[9.9%], shards will be relocated away from this node
  • Jobin
    Jobin over 7 years
    Try exclude = {ElasticsearchConfiguration.class}
  • Wouter
    Wouter over 7 years
    Nope. Doesn't work. An embedded ES is still started.