我正在使用spring boot web应用程序,该应用程序连接到开箱即用的mongo db。我只使用以下属性:
NSDictionary *result = [[NSDictionary alloc] init];
for(float i = 0; i < 120; i += 0.25){
[result setValue:[[NSMutableArray alloc] init] forKey:[NSString stringWithFormat:@"%.2f", i]];
}
MongoDB的默认超时为10秒。我想配置超时。我试过了
spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true
spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword
或spring.data.mongodb.socketTimeout=2
这些属性都不起作用。这是我可以在属性中指定的东西,Spring框架会处理它,或者有人可以举例说明通过声明Bean来实现它。
答案 0 :(得分:10)
这将覆盖Spring Boot自动配置:
@Configuration
public class MongoDbSettings {
@Bean
public MongoClientOptions mongoOptions() {
return MongoClientOptions.builder().socketTimeout(2000).build();
}
}
答案 1 :(得分:7)
要按照您的主题,这将是最好的回答您的问题。 看起来您想在application.properties文件中设置超时。
为此:
而不是
spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase
spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword
试试这个
spring.data.mongodb.uri=mongodb://myUser:myPassword@myHost:27017,myHost:27017/myDatabase?serverSelectionTimeoutMS=2000&connectTimeoutMS=2000
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true
将时间(以毫秒为单位)修改为所需时间。
答案 2 :(得分:4)
MongoClientOptions是一个具有所需属性的类。
<强>编程强> 如果您以编程方式配置了MongoDB,则下面是帮助您解决问题的代码段。
DB mongoDb;
MongoClient mongoClient = new MongoClient(new ServerAddress(url, port),
MongoClientOptions.builder()
.socketTimeout(3000)
.minHeartbeatFrequency(25)
.heartbeatSocketTimeout(3000)
.build());
mongoDb = mongoClient.getDB(dbname);
XML配置 - 高级
<beans>
<mongo:mongo host="localhost" port="27017">
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500}"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo/>
</beans>
另请参阅here以获取完整的文档。
答案 3 :(得分:2)
这个问题专门用于Spring启动。我使用以下方法运行良好,请建议是否有一种方法不使用new并使用Spring进行配置。
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
@Configuration
@EnableMongoRepositories("com.myapp.repository")
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Value("${myapp.mongodb.host}")
private String host;
@Value("${myapp.mongodb.port}")
private int port;
@Value("${myapp.mongodb.database}")
private String mongoDB;
@Value("${myapp.mongodb.userName}")
private String userName;
@Value("${myapp.mongodb.password}")
private String password;
@Value("${myapp.mongodb.socketTimeoutMS}")
private int socketTimeout;
@Value("${myapp.mongodb.connectionTimeoutMS}")
private int connectionTimeout;
@Override
public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
return super.mongoMappingContext();
}
@Override
@Bean
public Mongo mongo() throws Exception {
List<ServerAddress> servers = new ArrayList<ServerAddress>();
servers.add(new ServerAddress(host, port));
List<MongoCredential> creds = new ArrayList<MongoCredential>();
creds.add(MongoCredential.createCredential(userName, mongoDB, password.toCharArray()));
MongoClientOptions builder = MongoClientOptions.builder().socketTimeout(socketTimeout).connectTimeout(connectionTimeout).build();
return new MongoClient(servers, creds, builder);
}
@Override
protected String getDatabaseName() {
return mongoDB;
}
}
答案 4 :(得分:2)
问题是陈旧的,但是对于那些遇到Mongo DB JAVA驱动程序超时问题的人来说:设置maxConnectionIdleTime属性。
Spring Boot示例:
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.annotation.PreDestroy;
import java.net.UnknownHostException;
@Configuration
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
@Autowired
private MongoProperties properties;
@Autowired(required = false)
private MongoClientOptions options;
@Autowired
private Environment environment;
private MongoClient mongo;
@PreDestroy
public void close() {
if (this.mongo != null) {
this.mongo.close();
}
}
@Bean
@ConditionalOnMissingBean
public MongoClient mongo() throws UnknownHostException {
this.options = options.builder().maxConnectionIdleTime(1000 * 60 * 4).build();
this.mongo = this.properties.createMongoClient(this.options, this.environment);
return this.mongo;
}
}
这里有一个非常有用的链接:Java DriverJAVA-2535 com.mongodb.MongoSocketWriteException ......
答案 5 :(得分:1)
对于具有反应性MongoDb的Spring数据:
我的情况有些不同(我有关闭的连接-抛出“原因:java.lang.IllegalStateException:状态应为:打开”)
但是您可以在案件中使用它。
application.yml:
spring.data.mongodb:
host: localhost
database: myDb
port: 27017
username: admin
password: test
如果您的实现使用 MongoReactiveAutoConfiguration 来创建Bean,则可以使用此Bean对其进行配置:
@Bean
public MongoClientSettings mongoClientSettings() {
final MongoClientSettings clientSettings = MongoClientSettings.builder()
.retryWrites(true)
.applyToConnectionPoolSettings((ConnectionPoolSettings.Builder builder) -> {
builder.maxSize(300) //connections count
.minSize(100)
.maxConnectionLifeTime(0, TimeUnit.MILLISECONDS)
.maxConnectionIdleTime(0, TimeUnit.MILLISECONDS)
.maxWaitTime(5000, TimeUnit.MILLISECONDS)
.maxWaitQueueSize(5000);
})
.applyToSocketSettings(builder -> {
builder.connectTimeout(2000, TimeUnit.MILLISECONDS);
})
.applicationName("app")
.build();
return clientSettings;
}
答案 6 :(得分:0)
我找到了以下可能的解决方法的文件。
https://www.programmersought.com/article/1927319190/
它提到以下内容:
spring.data.mongodb.address=172.16.250.234:27017,172.16.250.239:27017,172.16.250.240:27017
spring.data.mongodb.replica-set=rs0
spring.data.mongodb.database=test
spring.data.mongodb.username=admin
spring.data.mongodb.password=admin
# Configure spring.data.mongodbDB Pool
spring.data.mongodb.min-connections-per-host=10
spring.data.mongodb.max-connections-per-host=100
spring.data.mongodb.threads-allowed-to-block-for-connection-multiplier=5
spring.data.mongodb.server-selection-timeout=30000
spring.data.mongodb.max-wait-time=120000
spring.data.mongodb.max-connection-idel-time=0
spring.data.mongodb.max-connection-life-time=0
spring.data.mongodb.connect-timeout=10000
spring.data.mongodb.socket-timeout=0
spring.data.mongodb.socket-keep-alive=false
spring.data.mongodb.ssl-enabled=false
spring.data.mongodb.ssl-invalid-host-name-allowed=false
spring.data.mongodb.always-use-m-beans=false
spring.data.mongodb.heartbeat-socket-timeout=20000
spring.data.mongodb.heartbeat-connect-timeout=20000
spring.data.mongodb.min-heartbeat-frequency=500
spring.data.mongodb.heartbeat-frequency=10000
spring.data.mongodb.local-threshold=15
spring.data.mongodb.authentication-database=auth_dev
我正在尝试对此进行测试,以确保它可以正常工作,但至少看起来似乎是合理的。