简体   繁体   English

春季启动 mongo mongoSocketException

[英]Spring boot mongo mongoSocketException

I am using spring boot for my rest api but I have a problem about mongo database.我正在为我的 rest api 使用 spring boot,但我有一个关于 mongo 数据库的问题。 Application sometimes throws mongo socket exception and does not execute following codes, when endpoint which needs mongo operation is triggred.当触发需要 mongo 操作的端点时,应用程序有时会抛出 mongo 套接字异常并且不执行以下代码。 I assign true to value of socketkeepalive but it did bot solve my problem.我将 true 分配给 socketkeepalive 的值,但它确实解决了我的问题。 How can I get rid of this problem and can you offer me spring boot mongo db configuration values that are suitable?我怎样才能摆脱这个问题,你能给我提供合适的 spring boot mongo db 配置值吗?

By the way, program is working properly.顺便说一句,程序运行正常。 But sometimes it throws this exception.但有时它会抛出这个异常。

Thanks谢谢

INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server **.***.***.***:42015
com.mongodb.MongoSocketOpenException: Exception opening socket
        at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
        at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
        ... 3 common frames omitted
  1. Spring Boot has a feature called "auto configuration". Spring Boot有一个叫做“自动配置”的特性。 In this case, as soon as the Mongo driver is detected on the classpath, the MongoAutoConfiguration is activated with default values, which point to localhost:27017 .在这种情况下,一旦在类路径上检测到 Mongo 驱动程序,就会使用指向localhost:27017的默认值激活MongoAutoConfiguration If you don't want that behaviour, you can now either configure the properties for MongoDB (see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb for valid property keys) or disable the MongoAutoConfiguration:如果您不想要这种行为,您现在可以配置MongoDB的属性(请参阅http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb以获得有效的属性键)或禁用 MongoAutoConfiguration:

    @SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

  2. Spring boot throws this exception when Mongo DB is not running.当 Mongo DB 未运行时, Spring boot会抛出此异常。 Please make sure that Mongodb is running.请确保Mongodb正在运行。 It got resolved for me after starting Mongo DB.启动 Mongo DB 后,它为我解决了。

  3. you can check if mongoDB is running on 27017 is running or not.您可以检查 mongoDB 是否正在运行 27017 是否正在运行。 use this code in your terminal在您的终端中使用此代码

    netstat -plntu

And Please show me your configurations file or properties file.请告诉我您的配置文件或属性文件。

Got the same issue while running mongo inside a docker stack and i just missed the port mapping from 27017 (external) to 27017 (internal), so applying在 docker 堆栈中运行 mongo 时遇到同样的问题,我只是错过了从 27017(外部)到 27017(内部)的端口映射,所以应用

    ports:
      - 27017:27017

to my docker-compose.yml file worked for me.docker-compose.yml文件对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM