尝试向服务器发送大请求时遇到以下问题。
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.connection.SocketStream.read(SocketStream.java:88) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:96) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:440) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:143) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.UpdateOperation.executeCommandProtocol(UpdateOperation.java:89) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:139) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:133) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:422) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:413) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60) ~[mongodb-driver-core-3.4.2.jar!/:na]
at com.mongodb.Mongo.execute(Mongo.java:845) ~[mongodb-driver-3.4.2.jar!/:na]
at com.mongodb.Mongo$2.execute(Mongo.java:828) ~[mongodb-driver-3.4.2.jar!/:na]
at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:342) ~[mongodb-driver-3.4.2.jar!/:na]
at com.mongodb.DBCollection.replaceOrInsert(DBCollection.java:420) ~[mongodb-driver-3.4.2.jar!/:na]
at com.mongodb.DBCollection.save(DBCollection.java:409) ~[mongodb-driver-3.4.2.jar!/:na]
at com.mongodb.DBCollection.save(DBCollection.java:382) ~[mongodb-driver-3.4.2.jar!/:na]
...
根据我的调查,发生此错误是因为与MongoDb的连接未保持活动状态。所以我尝试添加一些配置以延长超时时间:
@SpringBootApplication
@EnableResourceServer
@EnableDiscoveryClient
@EnableFeignClients
@EnableOAuth2Client
@EnableConfigurationProperties
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class CodelabArticleServiceApplication extends ResourceServerConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(CodelabArticleServiceApplication.class, args);
}
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.authorizeRequests().antMatchers("/*").permitAll().anyRequest().authenticated();
// @formatter:on
}
@Bean
@ConfigurationProperties(prefix = "security.oauth2.client")
public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
return new ClientCredentialsResourceDetails();
}
@Bean
public MongoClientOptions mongoOptions() {
int timeout = 3600000;
return MongoClientOptions.builder().socketKeepAlive(true).maxConnectionIdleTime(timeout)
.maxConnectionLifeTime(timeout).maxWaitTime(timeout).socketTimeout(timeout).build();
}
@Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails());
}
@Bean
public OAuth2RestTemplate clientCredentialsRestTemplate() {
return new OAuth2RestTemplate(clientCredentialsResourceDetails());
}
}
由于某些原因,它仍然无效。我想知道我的配置是否正确,或者还有其他任务可以完成这项工作?