如何使用spring boot更改mongodb的主机?

时间:2015-07-30 05:18:02

标签: spring mongodb spring-boot host

这是我的豆子:

<?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/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Activate annotation configured components -->
<context:annotation-config/>

<!-- Scan components for annotations within the configured package -->
<context:component-scan base-package="document">
    <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
</context:component-scan>

<!-- Define the MongoTemplate which handles connectivity with MongoDB -->

<mongo:mongo host="192.168.0.10" port="27017" />
<mongo:db-factory dbname="yourdb" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>

这是我的DAO

@Repository
public class DocumentRepository {

    @Autowired
    MongoTemplate mongoTemplate;

    def insertDocument(DocumentModel document) {
        return mongoTemplate.insert(document)
    }
}

每次调用insert时都会出现以下错误:

*Timed out after 10000 ms while waiting for a server that matches
 AnyServerSelector{}. Client view of cluster state is {type=Unknown, 
 servers=[{address=localhost:27017, type=Unknown, state=Connecting,  
 exception={com.mongodb.MongoException$Network: Exception opening the 
 socket}, caused by {java.net.ConnectException: Connection refused}}]; 

 nested exception is com.mongodb.MongoTimeoutException: Timed out after 
 10000 ms while waiting for a server that matches AnyServerSelector{}.

 Client view of cluster state is {type=Unknown, servers=
 [{address=localhost:27017, type=Unknown, state=Connecting, exception=
{com.mongodb.MongoException$Network: Exception opening the socket}, 
 caused by {java.net.ConnectException: Connection refused}}]*

这是我的主要文件

@Configuration
@EnableAutoConfiguration
@ComponentScan("document , filter")
class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我有mongodb在另一个盒子上运行,即192.168.0.10。由于某种原因,它一直显示它正在尝试连接到localhost。我缺少什么想法?

4 个答案:

答案 0 :(得分:1)

我最终通过在主班中添加以下行来让主持人改变。

00008e10 <bubble_sort>:
    8e10:   e92d0030    push    {r4, r5}
    8e14:   e2414001    sub r4, r1, #1
    8e18:   e3540000    cmp r4, #0
    8e1c:   da00000d    ble 8e58 <bubble_sort+0x48>
    8e20:   e080c101    add ip, r0, r1, lsl #2
    8e24:   e2805004    add r5, r0, #4
    8e28:   e3a00000    mov r0, #0
    8e2c:   e1a03005    mov r3, r5
    8e30:   e5131004    ldr r1, [r3, #-4]
    8e34:   e4932004    ldr r2, [r3], #4
    8e38:   e1510002    cmp r1, r2
    8e3c:   c5031004    strgt   r1, [r3, #-4]
    8e40:   c5032008    strgt   r2, [r3, #-8]
    8e44:   e153000c    cmp r3, ip
    8e48:   1afffff8    bne 8e30 <bubble_sort+0x20>
    8e4c:   e2800001    add r0, r0, #1
    8e50:   e1500004    cmp r0, r4
    8e54:   1afffff4    bne 8e2c <bubble_sort+0x1c>
    8e58:   e3a00001    mov r0, #1
    8e5c:   e8bd0030    pop {r4, r5}
    8e60:   e12fff1e    bx  lr

答案 1 :(得分:0)

替换

<mongo:mongo host="192.168.0.10" port="27017" />
<mongo:db-factory dbname="yourdb" />

<mongo:db-factory host="192.168.0.10" port="27017" dbname="yourdb" />

答案 2 :(得分:0)

使用时

<mongo:db-factory dbname="yourdb" />

使用默认主机和端口号向db工厂注册内部Mongo客户端实例。

要修复它,请使用

<mongo:db-factory dbname="yourdb" mongo-ref="mongo"/>

引用上面创建的mongo bean实例。

答案 3 :(得分:0)

除了弹簧启动之外,为什么还要使用所有这些XML配置的东西,这对你来说几乎是一样的。 添加

spring.data.mongodb.host=192.168.0.10 
spring.data.mongodb.database=YOURDB

到你的application.properties:

-Dspring.data.mongodb.host=192.168.0.10 -Dspring.data.mongodb.database=YOURDB

作为程序的参数