Соединение с MongoDB Atlas из веб-приложения Azure Tomcat: невозможно найти запись SRV для хоста

У меня есть веб-приложение Azure, работающее на Tomcat 8.5. Приложение использует MongoDB для постоянства. У меня есть бесплатный план MongoDB Atlas, который должен обслуживать приложение.

Когда я запускаю приложение из локального Tomcat (подключаясь к тому же веб-кластеру, кластеру Atlas) - все работает нормально. После развертывания приложения в Azure я получаю следующее исключение:

com.mongodb.MongoConfigurationException: Unable to look up SRV record for host cluster0-xxxxx.mongodb.net

Я использую следующую строку подключения (также пробовал без параметров):

mongodb+srv://admin:xxxxx@cluster0-xxxxx.mongodb.net/msapi?ssl=true&connectTimeoutMS=60000&socketTimeoutMS=60000

И следующий код для создания соединения:

MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoConnectionString));

Я занесен в белый список 0.0.0.0/0 на сервере MongoDB.

Не могу понять, что еще попробовать.

Полный журнал:

07-Apr-2018 20:38:19.323 SEVERE [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable
 com.mongodb.MongoConfigurationException: Unable to look up SRV record for host cluster0-ot7ig.mongodb.net
 at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:79)
 at com.mongodb.ConnectionString.<init>(ConnectionString.java:321)
 at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:234)
 at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:216)
 at com.idog.vis.academicvisapi.VisServerAppResources.getMongoClient(VisServerAppResources.java:128)
 at com.idog.vis.academicvisapi.VisServerAppResources.<init>(VisServerAppResources.java:92)
 at com.idog.vis.academicvisapi.VisServerAppBinder.configure(VisServerAppBinder.java:19)
 at org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:187)
 at org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:187)
 at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.register(ImmediateHk2InjectionManager.java:88)
 at java.lang.Iterable.forEach(Iterable.java:75)
 at org.glassfish.jersey.model.internal.CommonConfig.configureExternalObjects(CommonConfig.java:664)
 at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:633)
 at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:823)
 at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:351)
 at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:316)
 at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
 at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
 at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:256)
 at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:315)
 at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:282)
 at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:335)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
 at javax.servlet.GenericServlet.init(GenericServlet.java:158)
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4931)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
 at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
 at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at java.lang.Thread.run(Thread.java:748)
Caused by: javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name '_mongodb._tcp.cluster0-ot7ig.mongodb.net'
 at com.sun.jndi.dns.DnsClient.query(DnsClient.java:309)
 at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
 at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
 at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
 at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
 at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
 at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
 at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:60)
 ... 39 more
Caused by: java.net.SocketTimeoutException: Receive timed out
 at java.net.TwoStacksPlainDatagramSocketImpl.receive0(Native Method)
 at java.net.TwoStacksPlainDatagramSocketImpl.receive(TwoStacksPlainDatagramSocketImpl.java:114)
 at java.net.DatagramSocket.receive(DatagramSocket.java:812)
 at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:418)
 at com.sun.jndi.dns.DnsClient.query(DnsClient.java:208)
 ... 46 more

07-Apr-2018 20:38:19.323 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [com.idog.vis.academicvisapi.VisServerApp] in web application [/AcademicVisAPI] threw load() exception
 java.net.SocketTimeoutException: Receive timed out
 at java.net.TwoStacksPlainDatagramSocketImpl.receive0(Native Method)
 at java.net.TwoStacksPlainDatagramSocketImpl.receive(TwoStacksPlainDatagramSocketImpl.java:114)
 at java.net.DatagramSocket.receive(DatagramSocket.java:812)
 at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:418)
 at com.sun.jndi.dns.DnsClient.query(DnsClient.java:208)
 at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
 at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
 at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
 at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
 at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
 at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
 at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:60)
 at com.mongodb.ConnectionString.<init>(ConnectionString.java:321)
 at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:234)
 at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:216)
 at com.idog.vis.academicvisapi.VisServerAppResources.getMongoClient(VisServerAppResources.java:128)
 at com.idog.vis.academicvisapi.VisServerAppResources.<init>(VisServerAppResources.java:92)
 at com.idog.vis.academicvisapi.VisServerAppBinder.configure(VisServerAppBinder.java:19)
 at org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:187)
 at org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:187)
 at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.register(ImmediateHk2InjectionManager.java:88)
 at java.lang.Iterable.forEach(Iterable.java:75)
 at org.glassfish.jersey.model.internal.CommonConfig.configureExternalObjects(CommonConfig.java:664)
 at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:633)
 at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:823)
 at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:351)
 at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:316)
 at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
 at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
 at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:256)
 at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:315)
 at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:282)
 at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:335)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
 at javax.servlet.GenericServlet.init(GenericServlet.java:158)
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4931)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
 at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
 at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at java.lang.Thread.run(Thread.java:748)

07-Apr-2018 20:38:21.166 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
07-Apr-2018 20:38:21.166 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
07-Apr-2018 20:38:21.994 SEVERE [http-nio-23668-exec-2] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable
 java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
 at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:270)
 at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:217)
 at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:450)
 at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:330)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
 at javax.servlet.GenericServlet.init(GenericServlet.java:158)
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:795)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 at java.lang.Thread.run(Thread.java:748)

07-Apr-2018 20:38:22.010 SEVERE [http-nio-23668-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet [com.idog.vis.academicvisapi.VisServerApp]
 java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
 at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:270)
 at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:217)
 at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:450)
 at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:330)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
 at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
 at javax.servlet.GenericServlet.init(GenericServlet.java:158)
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:795)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 at java.lang.Thread.run(Thread.java:748)

1 ответ

Просто столкнулся с той же проблемой. Не знаю, что блокирует поиск SRV, но в качестве обходного пути вы можете использовать старую форму строки подключения MongoDB, которая не использует запись SRV.

Просто найдите строку подключения для MongoDB до версии 3.6. Это должно выглядеть примерно так:

mongodb://<username>:<password>@<shard-1>:27017,<shard2>:27017,<shard3>:27017/<dbname>?ssl=true&replicaSet=<replica set>&authSource=admin&retryWrites=true&w=majority

Другие вопросы по тегам