【问题标题】:Spring Boot Elasticsearch ConfigurationSpring Boot Elasticsearch 配置
【发布时间】:2015-12-26 13:14:54
【问题描述】:

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

这行得通:

@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());
    }
}

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

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

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

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

谢谢

【问题讨论】:

  • 你为什么不只是使用 Spring Boot 而是试图解决它。 Spring boot 已经根据您选择的配置文件加载了一个属性文件。所以你基本上让它变得很难......而且Spring Boot已经为你配置了ElasticSearch,所以你为什么要自己再做一次......

标签: java spring-boot elasticsearch


【解决方案1】:

删除您的配置类和属性。

添加以下依赖

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

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

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

这两个文件中的值当然会有所不同(或者将默认值放在application.properties 中并简单地用application-dev.properties 覆盖。

Spring Boot 将基于 spring.profiles.active load the desired properties 文件。

没有必要在你自己周围乱砍。

【讨论】:

  • 感谢您的反馈。每当我尝试只使用没有自定义配置的属性时,它总是运行内存中的内部 Elasticsearch 而不是我的外部服务器。
  • 如果您指定集群节点属性,则不应该是这种情况(参考指南中也对此进行了说明)。
  • 我确实阅读了关于 ES 的参考指南。我正在做的其他事情必须与应用程序属性中的 spring.data.elasticsearch.cluster-nodes 行冲突。这就是我尝试手动创建配置的原因。
  • 我只使用您推荐的配置创建了一个全新的项目,它确实启动了传输节点。所以我需要将现有项目中的元素移过来,看看是什么破坏了它。再次感谢!
  • 看起来 spring.data.elasticsearch.cluster-name 已被弃用:-/ docs.spring.io/spring-data/elasticsearch/docs/current/reference/…
【解决方案2】:

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

我的项目中有不同的配置文件,这是我的 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);

    }

【讨论】:

    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 2017-07-18
    • 2020-01-07
    • 2019-10-30
    • 1970-01-01
    • 2023-02-07
    • 2019-02-23
    • 2019-10-28
    相关资源
    最近更新 更多