解决:com.mongodb.MongoSocketOpenException: Exception opening socket ...

打印 上一主题 下一主题

主题 906|帖子 906|积分 2718

背景

springboot项目启动时,报错
  1. com.mongodb.MongoSocketOpenException: Exception opening socket
  2.         at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-3.11.2.jar:na]
  3.         at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-3.11.2.jar:na]
  4.         at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.11.2.jar:na]
  5.         at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
  6. Caused by: java.net.ConnectException: Connection refused: connect
  7.         at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_181]
  8.         at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_181]
  9.         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_181]
  10.         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_181]
  11.         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_181]
  12.         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_181]
  13.         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
  14.         at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
  15.         at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.11.2.jar:na]
  16.         at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-3.11.2.jar:na]
  17.         at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-3.11.2.jar:na]
  18.         ... 3 common frames omitted
复制代码
而且用工具能够连接mongodb,已经排除是mongodb不能连接的问题
缘故原由

项目中设置了自界说的MongoClient:
  1.         @Bean
  2.     public MongoDatabase mongoDatabase() {
  3.         String host = mongodbProperties.getHost();
  4.         Integer port = mongodbProperties.getPort();
  5.         MongoClientOptions.Builder options = new MongoClientOptions.Builder();
  6.         options.connectionsPerHost(mongodbProperties.getPoolSize());
  7.         options.threadsAllowedToBlockForConnectionMultiplier(mongodbProperties.getBlockSize());
  8.         options.build();
  9.         MongoClient mongoClient = null;
  10.         ServerAddress serverAddress = new ServerAddress(mongodbProperties.getHost(), mongodbProperties.getPort());
  11.         List<ServerAddress> seeds = new ArrayList<>();
  12.         seeds.add(serverAddress);
  13.         String userName = mongodbProperties.getUserName();
  14.         String password = mongodbProperties.getPassword();
  15.         if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(password)){
  16.             MongoCredential credentials = MongoCredential.createCredential(userName, mongodbProperties.getDatabase(), password.toCharArray());
  17.             List<MongoCredential> credentialsList = new ArrayList<>();
  18.             credentialsList.add(credentials);
  19.             mongoClient = new MongoClient(seeds, credentialsList);
  20.         }else {
  21.             mongoClient = new MongoClient(host, port);
  22.         }
  23.        return mongoClient.getDatabase(mongodbProperties.getDatabase());
  24.     }
复制代码
而springboot在启动时会自动实例化一个MongoClient:

源码如下:
  1. /*
  2. * Copyright 2012-2019 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.boot.autoconfigure.mongo;
  17. import com.mongodb.MongoClientURI;
  18. import org.springframework.boot.context.properties.ConfigurationProperties;
  19. /**
  20. * Configuration properties for Mongo.
  21. *
  22. * @author Dave Syer
  23. * @author Phillip Webb
  24. * @author Josh Long
  25. * @author Andy Wilkinson
  26. * @author Eddú Meléndez
  27. * @author Stephane Nicoll
  28. * @author Nasko Vasilev
  29. * @author Mark Paluch
  30. * @author Artsiom Yudovin
  31. * @since 1.0.0
  32. */
  33. @ConfigurationProperties(prefix = "spring.data.mongodb")
  34. public class MongoProperties {
  35.         /**
  36.          * Default port used when the configured port is {@code null}.
  37.          */
  38.         public static final int DEFAULT_PORT = 27017;
  39.         /**
  40.          * Default URI used when the configured URI is {@code null}.
  41.          */
  42.         public static final String DEFAULT_URI = "mongodb://localhost/test";
  43.         /**
  44.          * Mongo server host. Cannot be set with URI.
  45.          */
  46.         private String host;
  47.         /**
  48.          * Mongo server port. Cannot be set with URI.
  49.          */
  50.         private Integer port = null;
  51.         /**
  52.          * Mongo database URI. Cannot be set with host, port and credentials.
  53.          */
  54.         private String uri;
  55.         /**
  56.          * Database name.
  57.          */
  58.         private String database;
  59.         /**
  60.          * Authentication database name.
  61.          */
  62.         private String authenticationDatabase;
  63.         /**
  64.          * GridFS database name.
  65.          */
  66.         private String gridFsDatabase;
  67.         /**
  68.          * Login user of the mongo server. Cannot be set with URI.
  69.          */
  70.         private String username;
  71.         /**
  72.          * Login password of the mongo server. Cannot be set with URI.
  73.          */
  74.         private char[] password;
  75.         /**
  76.          * Fully qualified name of the FieldNamingStrategy to use.
  77.          */
  78.         private Class<?> fieldNamingStrategy;
  79.         /**
  80.          * Whether to enable auto-index creation.
  81.          */
  82.         private Boolean autoIndexCreation;
  83.         public String getHost() {
  84.                 return this.host;
  85.         }
  86.         public void setHost(String host) {
  87.                 this.host = host;
  88.         }
  89.         public String getDatabase() {
  90.                 return this.database;
  91.         }
  92.         public void setDatabase(String database) {
  93.                 this.database = database;
  94.         }
  95.         public String getAuthenticationDatabase() {
  96.                 return this.authenticationDatabase;
  97.         }
  98.         public void setAuthenticationDatabase(String authenticationDatabase) {
  99.                 this.authenticationDatabase = authenticationDatabase;
  100.         }
  101.         public String getUsername() {
  102.                 return this.username;
  103.         }
  104.         public void setUsername(String username) {
  105.                 this.username = username;
  106.         }
  107.         public char[] getPassword() {
  108.                 return this.password;
  109.         }
  110.         public void setPassword(char[] password) {
  111.                 this.password = password;
  112.         }
  113.         public Class<?> getFieldNamingStrategy() {
  114.                 return this.fieldNamingStrategy;
  115.         }
  116.         public void setFieldNamingStrategy(Class<?> fieldNamingStrategy) {
  117.                 this.fieldNamingStrategy = fieldNamingStrategy;
  118.         }
  119.         public String getUri() {
  120.                 return this.uri;
  121.         }
  122.         public String determineUri() {
  123.                 return (this.uri != null) ? this.uri : DEFAULT_URI;
  124.         }
  125.         public void setUri(String uri) {
  126.                 this.uri = uri;
  127.         }
  128.         public Integer getPort() {
  129.                 return this.port;
  130.         }
  131.         public void setPort(Integer port) {
  132.                 this.port = port;
  133.         }
  134.         public String getGridFsDatabase() {
  135.                 return this.gridFsDatabase;
  136.         }
  137.         public void setGridFsDatabase(String gridFsDatabase) {
  138.                 this.gridFsDatabase = gridFsDatabase;
  139.         }
  140.         public String getMongoClientDatabase() {
  141.                 if (this.database != null) {
  142.                         return this.database;
  143.                 }
  144.                 return new MongoClientURI(determineUri()).getDatabase();
  145.         }
  146.         public Boolean isAutoIndexCreation() {
  147.                 return this.autoIndexCreation;
  148.         }
  149.         public void setAutoIndexCreation(Boolean autoIndexCreation) {
  150.                 this.autoIndexCreation = autoIndexCreation;
  151.         }
  152. }
复制代码
解决方法

禁用SpringBoot对Mongo的自动设置,以免和自界说的设置冲突,在SpringBoot启动类上加上:
  1. @SpringBootApplication(exclude = MongoAutoConfiguration.class)
复制代码
由此可见,在任何springboot项目中,如果自界说了MongoClient且交与了springboot管理,都需要排除掉springboot对mongodb的自动设置

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表