英文:
Error when trying to connect Spring boot application with mongoDB
问题
我正在尝试将我的Spring Boot应用程序连接到MongoDB,但是我遇到了以下错误:
> 错误启动应用程序上下文。要显示条件报告,请使用启用“debug”的方式重新运行应用程序。2020-09-16 17:21:17.728
> 错误 10036 --- [ restartedMain] o.s.boot.SpringApplication
> : 应用程序运行失败
>
> org.springframework.beans.factory.BeanCreationException: 在类路径资源中定义的名称为“mongo”的bean创建错误
> [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]:通过工厂方法创建bean失败;嵌套异常是
> org.springframework.beans.BeanInstantiationException: 实例化[com.mongodb.client.MongoClient]失败:工厂方法“mongo”
> 抛出异常;嵌套异常是
> com.mongodb.MongoConfigurationException: 无法查找主机simulacao.yko5j.mongodb.net的TXT记录 at
> org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] ...
> 出现错误的帧已省略。Caused by:
> org.springframework.beans.BeanInstantiationException: 实例化[com.mongodb.client.MongoClient]失败:工厂方法“mongo”
> 抛出异常;嵌套异常是
> com.mongodb.MongoConfigurationException: 无法查找主机simulacao.yko5j.mongodb.net的TXT记录 at
> org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] ...
> 出现错误的帧已省略。Caused by: com.mongodb.MongoConfigurationException: 无法查找主机simulacao.yko5j.mongodb.net的TXT记录 at
> com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:131)
> ~[mongodb-driver-core-4.0.5.jar:na] ...
> 出现错误的帧已省略。Caused by: javax.naming.NameNotFoundException: 未找到DNS名称
> [响应代码3] at
> jdk.naming.dns/com.sun.jndi.dns.DnsClient.checkResponseCode(DnsClient.java:664)
> ~[jdk.naming.dns:na] ...
> 出现错误的帧已省略。
我的application.properties:
spring.data.mongodb.uri=mongodb+srv://renan:masterkey@simulacao.yko5j.mongodb.net/simulacao?retryWrites=true&w=majority
我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- 从仓库查找父项 -->
</parent>
<groupId>com.simulacao</groupId>
<artifactId>simulacao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>simulacao</name>
<description>Spring Boot演示项目</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
我通过Atlas MongoDB创建了与数据库的连接。
我不知道如何解决这个问题。
提前致谢。
英文:
I'm trying to connect my Spring Boot application to MongoDB but I am getting the following error:
> Error starting ApplicationContext. To display the conditions report
> re-run your application with 'debug' enabled. 2020-09-16 17:21:17.728
> ERROR 10036 --- [ restartedMain] o.s.boot.SpringApplication
> : Application run failed
>
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'mongo' defined in class path resource
> [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]:
> Bean instantiation via factory method failed; nested exception is
> org.springframework.beans.BeanInstantiationException: Failed to
> instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo'
> threw exception; nested exception is
> com.mongodb.MongoConfigurationException: Unable to look up TXT record
> for host simulacao.yko5j.mongodb.net at
> org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
> ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
> ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
> ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> com.simulacao.simulacao.SimulacaoApplication.main(SimulacaoApplication.java:10)
> ~[classes/:na] at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:na] at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:na] at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:na] at
> java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
> at
> org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
> ~[spring-boot-devtools-2.3.3.RELEASE.jar:2.3.3.RELEASE] Caused by:
> org.springframework.beans.BeanInstantiationException: Failed to
> instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo'
> threw exception; nested exception is
> com.mongodb.MongoConfigurationException: Unable to look up TXT record
> for host simulacao.yko5j.mongodb.net at
> org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at
> org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] ... 25 common frames
> omitted Caused by: com.mongodb.MongoConfigurationException: Unable to
> look up TXT record for host simulacao.yko5j.mongodb.net at
> com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:131)
> ~[mongodb-driver-core-4.0.5.jar:na] at
> com.mongodb.ConnectionString.<init>(ConnectionString.java:378)
> ~[mongodb-driver-core-4.0.5.jar:na] at
> org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.applyHostAndPort(MongoClientFactorySupport.java:102)
> ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.computeClientSettings(MongoClientFactorySupport.java:70)
> ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.createMongoClient(MongoClientFactorySupport.java:61)
> ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:57)
> ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE] at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:na] at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:na] at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:na] at
> java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
> at
> org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
> ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] ... 26 common frames
> omitted Caused by: javax.naming.NameNotFoundException: DNS name not
> found [response code 3] at
> jdk.naming.dns/com.sun.jndi.dns.DnsClient.checkResponseCode(DnsClient.java:664)
> ~[jdk.naming.dns:na] at
> jdk.naming.dns/com.sun.jndi.dns.DnsClient.isMatchResponse(DnsClient.java:582)
> ~[jdk.naming.dns:na] at
> jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:430)
> ~[jdk.naming.dns:na] at
> jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:214)
> ~[jdk.naming.dns:na] at
> jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81)
> ~[jdk.naming.dns:na] at
> jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
> ~[jdk.naming.dns:na] at
> java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
> ~[na:na] at
> java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
> ~[na:na] at
> java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
> ~[na:na] at
> java.naming/javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:171)
> ~[na:na] at
> com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:114)
> ~[mongodb-driver-core-4.0.5.jar:na] ... 36 common frames omitted
My application.properties:
spring.data.mongodb.uri=mongodb+srv://renan:masterkey@simulacao.yko5j.mongodb.net/simulacao?retryWrites=true&w=majority
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.simulacao</groupId>
<artifactId>simulacao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>simulacao</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
I created the connection to the database through Atlas MongoDB.
I don't know how to solve this problem.
Thanks in advance.
答案1
得分: 0
这似乎是与数据库的连接问题,根据错误跟踪信息。请检查一下 27017 端口是否被防火墙阻止。您需要在防火墙中打开此端口的出站和入站连接。
Windows 操作步骤:
- 前往防火墙
- 高级设置
- 入站规则
- 在“操作”选项卡下使用“新建规则”创建新规则
- 选择端口
- 选择 TCP 并指定端口号(27015、27016、27017)
- 允许连接
- 点击“下一步”
- 为新规则添加名称,然后点击“完成”
- 对于出站规则,按照相同步骤进行操作。完成后,重新启动您的系统。
英文:
This seems to be a connection issue with the database from the error trace. Can you check if the port 27017 blocked by your firewall. You need to open the outbound and inbound for this port in your firewall.
Steps for Windows:
- Go to Firewall
- Advanced Settings
- Inbound Rules
- Create a new rule with New Rule under Actions tab
- Select Port
- Select TCP and specify port no (27015,27016,27017)
- Allow the connection
- Click Next
- Add name for new Rule Finish
- Follow the same steps for Outbound Rule. Once done, restart your
system.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论