如何配置spring-data-mongodb以通过属性使用副本集 [英] How to configure spring-data-mongodb to use a replica set via properties

查看:1368
本文介绍了如何配置spring-data-mongodb以通过属性使用副本集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个应该使用MongoDB副本集的应用程序。它是一个基于Spring Boot的应用程序,以下属性可以很好地连接到一台服务器:

I am currently writing an application which should use a replica set of MongoDB. It is a Spring Boot based application and the following properties work perfectly fine to connect to one server:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=demo

这对我的本地开发环境来说绝对没问题。但是后来它应该针对MongoDB副本集运行,所以我必须提供至少2个,更好的3个副本集种子,但我怎么能用属性做这个?

This is absolutely fine for my local dev environment. But later on it should run against a MongoDB replica set, so I have to provide at least 2, better 3 replica set seeds, but how can I do this with properties?

我在这个页面上看了一下: http:// docs .spring.io / spring-boot / docs / current / reference / html / common-application-properties.html ,但没有提到的副本集的显式属性。
提供逗号分隔的地址列表,如下所示:

I had a look on this page: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html, but there is no explicit property for replica sets mentioned. Providing a comma separated list of addresses like this:

spring.data.mongodb.host=127.0.0.1,127.0.1.1,127.0.2.1
spring.data.mongodb.uri=mongo://127.0.0.1,mongo://127.0.0.1:27018

(我一个接一个地尝试过。)

(I tried one after another.)

这也不起作用(事实上,它产生了一个允许Spring使用默认配置的异常。)

This is also not working (in fact, it produces an exception which lets Spring uses the default configuration).

我也尝试使用以下config.xml,没有运气:

I also tried to use the following config.xml, with no luck:

<?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:context="http://www.springframework.org/schema/context"
          xmlns:mongo="http://www.springframework.org/schema/data/mongo"
          xsi:schemaLocation=
          "http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mongo:mongo id="replicaSetMongo" replica-set="127.0.0.1:27017,localhost:27018"/>

</beans>

我知道上述配置略有不同,但我目前正在尝试的是获得例外这告诉我没有可以访问副本集节点。

I know that the above configs are slightly different, but what I am currently trying is to get an exception which is showing me that no replica set node was reachable.

任何想法,提示?

推荐答案

没有明确的支持,没有。但你应该能够通过 uri 参数配置好。

There is no explicit support for that, no. But you should be able to configure that just fine via the uri parameter.

我们实际上已经更新了文档。

We've actually updated the documentation recently.

这篇关于如何配置spring-data-mongodb以通过属性使用副本集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆