【问题标题】:How change host for mongodb using spring boot?如何使用spring boot更改mongodb的主机?
【发布时间】:2015-07-30 05:18:02
【问题描述】:

这是我的豆子:

<?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。有什么我想念的想法吗?

【问题讨论】:

    标签: spring mongodb spring-boot host


    【解决方案1】:

    我最终通过在我的主类中添加以下行来更改主机。

    /*
     * Factory bean that creates the com.mongodb.Mongo instance
     */
    public @Bean MongoClientFactoryBean mongo() {
        MongoClientFactoryBean mongo = new MongoClientFactoryBean();
        mongo.setHost(host);
        return mongo;
    }
    

    【讨论】:

      【解决方案2】:

      替换

      <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" />
      

      【讨论】:

        【解决方案3】:

        使用时

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

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

        为了修复它,请使用

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

        它引用了刚才创建的 mongo bean 实例。

        【讨论】:

        • 这仍然不起作用。我也添加了我的主文件,只是为了看看我是否遗漏了什么。
        【解决方案4】:

        除了 spring boot 之外,你为什么还要使用所有这些 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
        

        作为程序的参数

        【讨论】:

        • 要使用什么上下文 XML 配置,以确保正在使用此配置?
        猜你喜欢
        • 2014-06-21
        • 2019-10-01
        • 1970-01-01
        • 2022-12-07
        • 1970-01-01
        • 2017-03-13
        • 1970-01-01
        • 2022-12-06
        • 2021-11-22
        相关资源
        最近更新 更多