我使用弹性搜索来搜索我的项目活动,在这些活动中,我通过spring实用程序与后端ES集群通信。
弹簧数据弹性搜索
下面是webapp的spring存储库描述。
<?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 say (192.xx.xx.xx)建立了完整的功能集群。因此,我的问题是,我们已经在生产服务器中的/etc/project/es.yml文件中的yml文件中指定了集群主机。因此,我需要调整我的应用程序,以便从这个自定义位置获取集群配置。
由于上面由spring容器初始化的spring存储库xml,我们无法覆盖这种行为。有什么方法可以用弹簧数据弹性搜索来实现吗?
发布于 2014-02-15 08:11:35
最后,我解决了我的问题,并在这里分享它,以便它可能是有用的其他人。
将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>其中在PropertySourcePlaceHolder中采用了Spring 3.1+的修改机制。
因此,它将在/etc/project/es.props中查找es.host。本地测试人员可以通过使用-Des.host=定制-cluser-host启动服务器来覆盖此属性。
实际上,Mohsin(Spring弹性搜索开发人员)给出了实现该解决方案的一些提示。谢谢莫辛。
https://stackoverflow.com/questions/21360696
复制相似问题