覆盖Spring Data Elastic Search集群节点配置

时间:2014-01-26 07:03:10

标签: spring-mvc jpa elasticsearch spring-data

我正在使用Elastic Search进行项目活动,我通过spring实用程序与后端ES集群进行通信

  

弹簧数据弹性搜索

以下是webapp的spring-repository描述。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
    xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" />

    <bean name="elasticsearchTemplate"
        class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>

    <elasticsearch:repositories
        base-package="com.customer.repositories" />
</beans>

在这里,我已将群集节点配置指定为 cluster-nodes =“localhost:9300”,并且在我的本地测试中工作正常。

在生产服务器中,我们有一个完整的功能集群设置,主机IP说(192.xx.xx.xx)。所以我的问题是,我们在生产服务器的/etc/project/es.yml文件中的yml文件中指定了集群主机。所以我需要调整我的应用程序以从此自定义位置获取群集配置。

由于弹簧容器初始化了上面的spring-repository xml,我们无法覆盖该行为。有没有办法用spring-data-elastic-search实现呢?

1 个答案:

答案 0 :(得分:1)

最后,我解决了我的问题并在此处分享,以便对其他人有用。

将YML想法更改为属性文件(es.props)

spring存储库描述应该如下

<?xml version="1.0" encoding="UTF-8"?>co
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
    xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch
                        http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:property-placeholder location="file:/etc/project/es.props" />

    <elasticsearch:transport-client id="client" cluster-nodes="${es-host}:9300""/>

    <bean name="elasticsearchTemplate"
        class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>

    <elasticsearch:repositories
        base-package="com.customer.repositories" />

</beans>

在3.1 +中使用了Spring PropertySourcePlaceHolder修改机制。

所以它会在/etc/project/es.props中查找es.host。本地测试人员可以通过使用-Des.host = custom-cluser-host

启动服务器来覆盖此属性

实际上Mohsin(Spring-data-elastic-search developer)提供了一些提示来解决这个问题。谢谢Mohsin。

相关问题