gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:08:26 25 4
gpt4 key购买 nike

我将Elastic Search用于我的项目 Activity ,在其中我通过spring实用程序与后端ES集群进行通信

spring-data-elastic-search



以下是该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容器初始化的,因此我们无法覆盖其行为。有什么方法可以通过spring-data-elastic-search实现呢?

最佳答案

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

将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数据 Elasticsearch 开发人员)给出了实现此解决方案的提示。谢谢莫辛。

关于spring-mvc - 覆盖Spring Data Elastic Search集群节点配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com