简体   繁体   English

Spring Boot-在不删除代码或不更改POM的情况下禁用嵌入式ElasticSearch

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

I'm looking for a way to prevent ElasticSearch for starting (embedded or separate server) in a Spring Boot project. 我正在寻找一种方法来防止ElasticSearch在Spring Boot项目中启动(嵌入式或单独的服务器)。 ES is currently not in use, but will be at a later stage in the project. ES当前未使用,但将在项目的后期使用。

If I remove the lines from the POM, my code needs major updates, because all annotations to ES cannot be found anymore. 如果我从POM中删除这些行,则我的代码需要进行重大更新,因为无法再找到ES的所有注释。

Is there a way I can keep my project intact, but prevent ES from launching (embedded)? 有什么方法可以使我的项目保持完整,但是可以阻止ES启动(嵌入)?

My goal is to speed up restarts for the time being, when ES is not in use. 我的目标是暂时不使用ES时加快重启速度。

Or course, I could also run ES as a separate server, but I don't want to spend the time. 或者,当然,我也可以将ES作为单独的服务器运行,但是我不想浪费时间。

Thanks 谢谢

Add the following exclude to your @SpringBootApplication 将以下exclude添加到您的@SpringBootApplication

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

@SpringBootApplication(exclude = ElasticsearchAutoConfiguration.class)

In your local application.properties 在您本地的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 您将需要使ElasticsearchTemplate @Lazy或将其放在@Profile("production")类下的概率

In case your are using Java High Level REST Client, then the solution is as the following: 如果您使用的是Java High Level REST Client,则解决方案如下:

@EnableAutoConfiguration(exclude = RestClientAutoConfiguration.class ) @EnableAutoConfiguration(exclude = RestClientAutoConfiguration.class

I found this way to disable/enable Elasticsearch in Spring Boot 我发现这种方法可以在Spring Boot中禁用/启用Elasticsearch

in application.properties: 在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();
}

You should exclude this two Spring Boot Auto Configurations: 您应该排除这两个Spring Boot Auto Configurations:

ElasticsearchAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class

Also if you have @Configuration class for your Elastic or @Bean for the ElasticsearchTemplate be sure to comment them out. 另外,如果您的Elastic拥有@Configuration类,或者ElasticsearchTemplate @Bean ,请确保将其注释掉。

I have just disabled embedded elasticsearch in spring boot 1.5.8 by adding the following exclude to my @SpringBootApplication. 通过在我的@SpringBootApplication中添加以下排除,我刚刚在Spring Boot 1.5.8中禁用了嵌入式Elasticsearch。

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})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不改变原有pom的情况下,在新的spring-boot项目中添加spring-boot jar作为依赖 - How to add a spring-boot jar as a dependency in a new spring-boot project without changing the original pom 在Spring Boot测试中关闭嵌入式Elasticsearch - Turn off embedded Elasticsearch in Spring Boot test 自定义 Spring 开机报错响应码不改默认正文 - Customize Spring Boot error response code without changing the default body 在不更改代码的情况下更改 Spring Boot 应用程序的端口 - Change the port of a Spring boot application without changing the code 没有 pom.xml 的 Java Spring Boot 项目 - Java Spring Boot Project without pom.xml 将Spring Boot中的嵌入式数据库从H2更改为MySQL - Changing embedded database in Spring Boot from H2 to MySQL Spring 引导:通过更改标志在嵌入式和 postgresql 数据源之间切换 - Spring Boot: switch between embedded and postgresql datasources by changing a flag 如何在不删除依赖项的情况下从 spring-boot-2 中的 yaml/properties 文件禁用所有与 Kafka 相关的自动配置? - How to disable all Kafka related auto configuration from yaml/properties file in spring-boot-2 without removing dependencies? Spring Boot:禁用状态异常代码的安全性 - Spring Boot: disable security for status exception code 在不更改错误响应正文或状态代码的情况下记录 Spring 引导 controller 异常 - Log Spring Boot controller exceptions without changing the error response body or status code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM