首页 文章

Spring Boot以及如何在 Cloud 代工厂上配置MongoDB的连接细节?

提问于
浏览
0

我目前正在使用mongodb创建一个spring boot web应用程序 . 在本地,我能够运行我的应用程序,并且正在检索和存储数据 . 但不是 Cloud 代工厂 . 任何提示,链接或任何帮助我的东西将不胜感激!

Problem

当我把它推入 Cloud 代工厂时,我无法连接到我的mongodb . 我知道我们必须在 Cloud 代工厂配置一些东西以使其工作 . 我尝试使用mongodb PCF,即使我将它绑定到我的应用程序,它仍然没有运行我的应用程序 . 我一直在互联网上搜索,试图找到正确的配置 .

Other thoughts

我在使用MongoDb时从未设置用户名和密码 . 我目前只使用localhost和port27017 . 也许我需要在我的V_CAP或env varibles上配置凭据,但是我无法在cloudfoundry.com的UI上编辑这些凭据 . 是否有命令行提示将这些变量配置到cf中?我查看了几个文档,但我不清楚将这些bean添加到哪个配置文件中,或者它有助于让mongodb在 Cloud 代工厂运行Doc1

Error of connection on cloud foundry

OUT org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'indexController'的bean时出错:通过字段'apL'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'appPortList'的bean时出错:init方法的调用失败;嵌套异常是org.springframework.dao.DataAccessResourceFailureException:在等待与ReadPreferenceServerSelector 匹配的服务器后30000 ms后超时 . 集群状态的客户端视图是{type = UNKNOWN,servers = [{address = 127.0.0.1:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java . net.ConnectException:Connection refused}}];嵌套异常是com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector 匹配的服务器后30000 ms后超时 . 集群状态的客户端视图是{type = UNKNOWN,servers = [{address = 127.0.0.1:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:Exception opening socket},由{java . net.ConnectException:连接被拒绝}}]

2 回答

  • 0

    从理论上讲,Spring启动 Cloud 连接器应该为你做这个,但我过去遇到过这个问题 .

    或者,您需要添加一个扩展AbstractMongoConfiguration的spring bean . 在此配置中,您必须插入在VCAP_SERVICES环境变量中找到的主机,端口,用户名,数据库名称和密码 .

    您可以通过使用System.getEnv(“VCAP_SERVICES”)获取VCAP_SERVICES变量的值来自动插入它,并使用JACKSON或其他一些JSON库进行解析 .

    @Configuration
    @Profile("cloud")
    public class DatabaseConfiguration extends AbstractMongoConfiguration {
    
        @Bean
        public ValidatingMongoEventListener validatingMongoEventListener() {
            return new ValidatingMongoEventListener(validator());
        }
    
        @Bean
        public LocalValidatorFactoryBean validator() {
            return new LocalValidatorFactoryBean();
        }
    
        @Override
        public String getDatabaseName() {
            return this.databaseName;
        }
    
        @Override
        @Bean
        public Mongo mongo() throws Exception {
            List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
            ServerAddress address = new ServerAddress("my_host", 7777);
            serverAddresses.add(address);
            return new MongoClient(serverAdresses,
                    singletonList(
                            MongoCredential.createCredential(
                                    "my_user_name",
                                    "my_database_name",
                                    "my_password".toCharArray()
                            )
                    )
            );
        }
    }
    

    关于你的“其他想法” . 无法编辑CloudFoundry提供的VCAP_SERVICES变量 . 相反,应该由您的应用程序读取,以便您的应用程序知道在哪里可以找到CF为其提供的mongodb实例 .

    但是,您可以使用“cf set-env”为应用程序添加更多环境变量 . 然而,就我所见,这无助于解决您的问题 .

  • 1

    在Spring Framework博客上有关于绑定到CF上的数据服务的非常好的博客文章:https://spring.io/blog/2015/04/27/binding-to-data-services-with-spring-boot-in-cloud-foundry .

    这篇文章在其示例中使用了与JPA的关系,但显示的所有内容也适用于MongoDB .

相关问题