【问题标题】:GAE unable to look up SRV record for mongodb atlas instanceGAE 无法查找 mongodb atlas 实例的 SRV 记录
【发布时间】:2019-03-14 15:48:08
【问题描述】:

我有一个在 Atlas 上运行的 mongodb 实例和一个在 GAE 上运行的 Spring Boot 应用程序,试图访问这个数据库。我在本地运行它没有问题,但是在 GAE 上运行时,我在日志中看到了这个错误。

com.mongodb.MongoConfigurationException: Unable to look up SRV record for host xyz-abc-delta-james.gcp.mongodb.net

这是我的 mongodb 的 spring boot 配置

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.field-naming-strategy=org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy
spring.data.mongodb.uri=mongodb+srv://<user>:<password>@xyz-abc-delta-james.gcp.mongodb.net/test?retryWrites=true

我试图在 GAE 文档中查找某些内容,但没有发现此类错误的痕迹。

Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is com.mongodb.MongoConfigurationException: Unable to look up SRV record for host xyz-abc-delta-james.gcp.mongodb.net
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333)
    at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:157)
    at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:137)
    at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
    at org.eclipse.jetty.plus.annotation.ContainerInitializer.callStartup(ContainerInitializer.java:140)
    at org.eclipse.jetty.annotations.ServletContainerInitializersStarter.doStart(ServletContainerInitializersStarter.java:63)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:330)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1406)
    at com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.startWebapp(AppEngineWebAppContext.java:159)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1368)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:778)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:262)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:522)
    at com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.doStart(AppEngineWebAppContext.java:116)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.createHandler(AppVersionHandlerMap.java:244)
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.getHandler(AppVersionHandlerMap.java:182)
    at com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:109)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchServletRequest(JavaRuntime.java:692)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchRequest(JavaRuntime.java:654)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:624)
    at com.google.apphosting.runtime.JavaRuntime$NullSandboxRequestRunnable.run(JavaRuntime.java:818)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:274)
    at java.lang.Thread.run(Thread.java:745)

有没有人遇到过这样的事情?这可能是 GAE 的网络限制吗?

谢谢!

注意:我已经在serverfault 上发布了这个帖子,但没有得到任何回复。

【问题讨论】:

  • 您使用的是哪个mongo-java-driver 版本?谢谢

标签: java mongodb spring-boot google-app-engine google-cloud-platform


【解决方案1】:

显然 GAE 不支持 mogodb uri 的最新(3.6x 及更高版本)格式,在我切换到相当长的 uri 表示的旧版(3.4x 及更高版本)后,一切都可以无缝运行。

spring.data.mongodb.uri=mongodb://<user>:<password>@xyz-abc-delta-james.gcp.mongodb.net:27017,foo-bar-shard-00-01-tlxzf.gcp.mongodb.net:27017,foo-bar-shard-00-02-tlxzf.gcp.mongodb.net:27017/test?ssl=true&replicaSet=foo-bar-shard-0&authSource=admin&retryWrites=true

由于 Spring boot 对 3.6x 版本有内部依赖,所以我不得不手动为 3.4 版本添加 mongodb 依赖。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.4.3</version>
    </dependency>

【讨论】:

    【解决方案2】:

    我找到了解决办法:

    您必须在 Google Cloud Console 中解锁默认的传出 TCP 端口(MongoDB Atlas 的默认值为 27017)。

    更准确地说:在 Network >> VPC Network >> Firewall 部分中添加 new rule for mongo traffic。

    【讨论】:

    • 这解决了连接到 mongo 的问题,但没有解决 SRV 的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    相关资源
    最近更新 更多