Java Spring Boot Elasticsearch 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32837507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:11:02  来源:igfitidea点击:

Spring Boot Elasticsearch Configuration

javaspring-bootelasticsearch

提问by Joe Reymann

I've got a working Spring Boot Elasticsearch Application which uses one of two profiles: application.dev.properties or application.prod.properties. That part works fine. I am having issue with getting the external elasticsearch to read from the application.xxx.properties.

我有一个可用的 Spring Boot Elasticsearch 应用程序,它使用两个配置文件之一:application.dev.properties 或 application.prod.properties。那部分工作正常。我在从 application.xxx.properties 读取外部 elasticsearch 时遇到问题。

This works:

这有效:

@Configuration
@PropertySource(value = "classpath:config/elasticsearch.properties")
public class ElasticsearchConfiguration {

    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        TransportClient client = new TransportClient();
        TransportAddress address = new InetSocketTransportAddress(
                environment.getProperty("elasticsearch.host"), 
                Integer.parseInt(environment.getProperty("elasticsearch.port"))
        );
        client.addTransportAddress(address);        
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

but obviously doesn't solve my multi-environment issue.

但显然不能解决我的多环境问题。

I've also tried @Value annotations for host and port variables without success.

我还尝试了主机和端口变量的 @Value 注释,但没有成功。

How can I convert the above to read its values from the application properties file or choose a different @PropertySource file based on whichever profile I want to run?

如何将上述内容转换为从应用程序属性文件中读取其值或根据我要运行的配置文件选择不同的 @PropertySource 文件?

spring.data.elasticsearch.properties.host = 10.10.1.10
spring.data.elasticsearch.properties.port = 9300

Thanks

谢谢

采纳答案by M. Deinum

Remove your configuration class and properties.

删除您的配置类和属性。

Add the following dependency

添加以下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Just add the spring.data.elasticsearchproperties to an application-prod.propertiesand application-dev.propertiesand change for the desired environment. This is described in the ElasticSearch sectionof the Spring Boot guide.

只需将spring.data.elasticsearch属性添加到application-prod.propertiesandapplication-dev.properties并更改所需的环境。这在 Spring Boot 指南的ElasticSearch 部分中有描述。

spring.data.elasticsearch.cluster-nodes=10.10.1.10:9300

The value in either file will of course differ (or put the default in the application.propertiesand simply override with an application-dev.properties.

在任一文件中的值当然会有所不同(或把默认的application.properties,只是与覆盖application-dev.properties

Spring Boot will based on the spring.profiles.activeload the desired propertiesfile.

Spring Boot 将根据spring.profiles.active加载所需的属性文件。

There is no need to hack around yourself.

没有必要破解自己。

回答by ignacio.suay

I agree with Deinum, if you are using Spring boot it will get the properties from the active profile active.

我同意 Deinum,如果您使用 Spring boot,它将从活动配置文件中获取活动的属性。

I have different profiles in my project and this is my elasticsearch configuration:

我的项目中有不同的配置文件,这是我的 elasticsearch 配置:

@Configuration
public class ElasticSearchConfiguration {
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;
    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    @Bean
    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
            String server = clusterNodes.split(":")[0];
            Integer port = Integer.parseInt(clusterNodes.split(":")[1]);
            Settings settings = Settings.settingsBuilder()
                .put("cluster.name", clusterName).build();
            client = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(server), port));
            return new ElasticsearchTemplate(client);

    }