具體情況是我的聚合項目,在使用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <!– <version>2.1.5.RELEASE</version>–> </dependency>
遇到報錯,大概就是連接linux 在一段時間,大概8分鍾左右,會自動斷開,下次第一次刷新會報錯,之后就會好了
2020-06-28 10:06:55.058 WARN 25076 --- [nio-9080-exec-2] org.mongodb.driver.connection : Got socket exception on connection [connectionId{localValue:8, serverValue:5252}] to 112.124.46.197:22017. All connections to 112.124.46.197:22017 will be closed. 2020-06-28 10:06:55.058 INFO 25076 --- [nio-9080-exec-2] org.mongodb.driver.connection : Closed connection [connectionId{localValue:8, serverValue:5252}] to 112.124.46.197:22017 because there was a socket exception raised by this connection. org.springframework.data.mongodb.UncategorizedMongoDbException: Exception sending message; nested exception is com.mongodb.MongoSocketWriteException: Exception sending message at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138) at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2774)
自己創建一個demo ,使用的高版本后,並沒有此問題,可是老項目中,主依賴中我修改了mongodb的版本,問題依然存在,講mongodb單純分出一個項目后,這個問題可以解決.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
於是只能單獨將mongo分出來了,在主項目中引入配置文件
此時啟動會遇到問題:
Unsatisfied dependency expressed through field 'mongoTemplate'
疑惑,測試的demo里面也是這樣寫的,沒有出現此問題,為何遷移到主項目中mongotemplate沒有注入
原因:
去掉主項目的啟動類中的兩個mongoauto配置,就解決了這個問題.