[英]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.