- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:解决方案如Valijon所述。但是此外,在我第一次插入DemoApplication.java之前,缺少以下行:
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDbFactory(new ConnectionString("mongodb://docker_mongo:27017/db")));
docker-compose up
创建了一个容器“docker_mongo”和一个名为“docker_spring”的容器。
application.properties
(请参见下文)。
spring.data.mongodb.host=mongo
spring.data.mongodb.port=27017
spring.data.mongodb.database=db
spring.data.mongodb.uri=mongodb://docker_mongo:27017/db
FROM openjdk:11
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} /tmp/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/tmp/app.jar"]
RUN echo "hello world"
version: '3.1'
services:
mongo:
container_name: docker_mongo
networks:
- gateway
ports:
- "27017:27017"
hostname: mongo
image: mongo:latest
command: mongod --port 27017
volumes:
- ./data/db:/data/db
spring:
build: .
container_name: docker_spring
networks:
- gateway
ports:
- "8080:8080"
hostname: spring
depends_on:
- mongo
networks:
gateway:
driver: "bridge"
package com.example.demo;
import com.example.model.Expenditure;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.time.LocalDate;
/*
@SpringBootApplication(exclude={
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
*/
@SpringBootApplication
@ComponentScan({"com.example.client","com.example.demo","com.example.model"})
public class DemoApplication implements CommandLineRunner {
@Autowired
private ExpenditureRepository repository;
@Override
public void run(String... args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDbFactory(MongoClients.create(), "db"));
mongoOps.insert(new Expenditure("Aldi",10.01, LocalDate.parse("2019-10-05")));
System.out.println(mongoOps.findOne(new Query(Criteria.where("name").is("Aldi")),Expenditure.class));
mongoOps.dropCollection("expenditure");
PopulateExpenditureDB.populateExpenditures(mongoOps);
}
}
package com.example.demo;
import com.mongodb.client.MongoDatabase;
import org.springframework.dao.DataAccessException;
public interface MongoDbFactory {
MongoDatabase getDb() throws DataAccessException;
MongoDatabase getDb(String dbName) throws DataAccessException;
}
package com.example.demo;
import com.example.model.Expenditure;
import java.time.LocalDate;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
//mongodb Host & Port defined in application.properties
public interface ExpenditureRepository extends MongoRepository<Expenditure, String> {
/*
public void create(Expenditure expenditure) {
if(expenditure.)
}
public Expenditure findById(int id) {
return data.get(id)
}
*/
public Expenditure findByName (String name);
public List<Expenditure> findByExDate (LocalDate exDate);
}
~/new_Webapp$ sudo docker-compose up
Creating network "newwebapp_gateway" with driver "bridge"
Building spring
Step 1/6 : FROM openjdk:11
---> 612d4d483eee
Step 2/6 : VOLUME /tmp
---> Running in 3aff2f893162
Removing intermediate container 3aff2f893162
---> 2bf19d4c67b1
Step 3/6 : ARG JAR_FILE=target/*.jar
---> Running in b7c3782bbc1a
Removing intermediate container b7c3782bbc1a
---> b029607db12b
Step 4/6 : COPY ${JAR_FILE} /tmp/app.jar
---> f3168bc17ae3
Step 5/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/tmp/app.jar"]
---> Running in f729f7204cdd
Removing intermediate container f729f7204cdd
---> aceb4c1a0bda
Step 6/6 : RUN echo "hello world"
---> Running in d4ab9bb0f498
hello world
Removing intermediate container d4ab9bb0f498
---> 45ae63ef2bd1
Successfully built 45ae63ef2bd1
Successfully tagged newwebapp_spring:latest
WARNING: Image for service spring was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker_mongo ...
Creating docker_mongo ... done
Creating docker_spring ...
Creating docker_spring ... done
Attaching to docker_mongo, docker_spring
docker_mongo | 2020-02-07T14:45:26.462+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=mongo
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] db version v4.2.2
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] allocator: tcmalloc
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] modules: none
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] build environment:
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] distmod: ubuntu1804
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] distarch: x86_64
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] target_arch: x86_64
docker_mongo | 2020-02-07T14:45:26.464+0000 I CONTROL [initandlisten] options: { net: { bindIp: "*", port: 27017 } }
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten]
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
docker_mongo | 2020-02-07T14:45:26.464+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7471M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
docker_mongo | 2020-02-07T14:45:26.877+0000 I STORAGE [initandlisten] WiredTiger message [1581086726:877548][1:0x7ff0c5201b00], txn-recover: Recovering log 36 through 37
docker_mongo | 2020-02-07T14:45:26.904+0000 I STORAGE [initandlisten] WiredTiger message [1581086726:904522][1:0x7ff0c5201b00], txn-recover: Recovering log 37 through 37
docker_mongo | 2020-02-07T14:45:26.950+0000 I STORAGE [initandlisten] WiredTiger message [1581086726:950147][1:0x7ff0c5201b00], txn-recover: Main recovery loop: starting at 36/5888 to 37/256
docker_mongo | 2020-02-07T14:45:27.008+0000 I STORAGE [initandlisten] WiredTiger message [1581086727:8723][1:0x7ff0c5201b00], txn-recover: Recovering log 36 through 37
docker_mongo | 2020-02-07T14:45:27.044+0000 I STORAGE [initandlisten] WiredTiger message [1581086727:44798][1:0x7ff0c5201b00], txn-recover: Recovering log 37 through 37
docker_mongo | 2020-02-07T14:45:27.075+0000 I STORAGE [initandlisten] WiredTiger message [1581086727:75209][1:0x7ff0c5201b00], txn-recover: Set global recovery timestamp: (0,0)
docker_mongo | 2020-02-07T14:45:27.102+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
docker_mongo | 2020-02-07T14:45:27.106+0000 I STORAGE [initandlisten] Timestamp monitor starting
docker_mongo | 2020-02-07T14:45:27.109+0000 I CONTROL [initandlisten]
docker_mongo | 2020-02-07T14:45:27.110+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
docker_mongo | 2020-02-07T14:45:27.110+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
docker_mongo | 2020-02-07T14:45:27.110+0000 I CONTROL [initandlisten]
docker_mongo | 2020-02-07T14:45:27.112+0000 I SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.114+0000 I STORAGE [initandlisten] Flow Control is enabled on this deployment.
docker_mongo | 2020-02-07T14:45:27.114+0000 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.114+0000 I SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.116+0000 I SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.116+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
docker_mongo | 2020-02-07T14:45:27.118+0000 I SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.118+0000 I NETWORK [initandlisten] Listening on /tmp/mongodb-27017.sock
docker_mongo | 2020-02-07T14:45:27.118+0000 I NETWORK [initandlisten] Listening on 0.0.0.0
docker_mongo | 2020-02-07T14:45:27.118+0000 I SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
docker_mongo | 2020-02-07T14:45:27.118+0000 I NETWORK [initandlisten] waiting for connections on port 27017
docker_spring |
docker_spring | . ____ _ __ _ _
docker_spring | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
docker_spring | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
docker_spring | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
docker_spring | ' |____| .__|_| |_|_| |_\__, | / / / /
docker_spring | =========|_|==============|___/=/_/_/_/
docker_spring | :: Spring Boot :: (v2.2.1.RELEASE)
docker_spring |
docker_spring | 2020-02-07 14:45:27.552 INFO 1 --- [ main] com.example.demo.MainClass : Starting MainClass v0.0.1-SNAPSHOT on spring with PID 1 (/tmp/app.jar started by root in /)
docker_spring | 2020-02-07 14:45:27.556 INFO 1 --- [ main] com.example.demo.MainClass : No active profile set, falling back to default profiles: default
docker_spring | 2020-02-07 14:45:27.950 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
docker_spring | 2020-02-07 14:45:27.992 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 38ms. Found 1 repository interfaces.
docker_mongo | 2020-02-07T14:45:28.000+0000 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
docker_spring | 2020-02-07 14:45:28.364 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
docker_spring | 2020-02-07 14:45:28.372 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
docker_spring | 2020-02-07 14:45:28.372 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
docker_spring | 2020-02-07 14:45:28.425 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
docker_spring | 2020-02-07 14:45:28.426 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 822 ms
docker_spring | 2020-02-07 14:45:28.616 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[docker_mongo:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
docker_mongo | 2020-02-07T14:45:28.643+0000 I NETWORK [listener] connection accepted from 192.168.0.3:53260 #1 (1 connection now open)
docker_mongo | 2020-02-07T14:45:28.649+0000 I NETWORK [conn1] received client metadata from 192.168.0.3:53260 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.2" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "5.3.0-28-generic" }, platform: "Java/Oracle Corporation/11.0.6+10" }
docker_spring | 2020-02-07 14:45:28.659 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to docker_mongo:27017
docker_spring | 2020-02-07 14:45:28.663 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=docker_mongo:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 2]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2669943}
docker_spring | 2020-02-07 14:45:28.703 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.703 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.717 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.717 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:28.951 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
docker_spring | 2020-02-07 14:45:29.076 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
docker_spring | 2020-02-07 14:45:29.078 INFO 1 --- [ main] com.example.demo.MainClass : Started MainClass in 1.787 seconds (JVM running for 2.142)
docker_spring | 2020-02-07 14:45:29.081 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
docker_spring | 2020-02-07 14:45:29.083 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.083 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.085 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.LocalDateTime to class java.time.Instant as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.085 WARN 1 --- [ main] o.s.data.convert.CustomConversions : Registering converter from class java.time.Instant to class java.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might wanna check you annotation setup at the converter implementation.
docker_spring | 2020-02-07 14:45:29.086 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
docker_spring |
docker_spring | com.mongodb.MongoSocketOpenException: Exception opening socket
docker_spring | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
docker_spring | Caused by: java.net.ConnectException: Connection refused (Connection refused)
docker_spring | at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
docker_spring | at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
docker_spring | at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
docker_spring | at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
docker_spring | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
docker_spring | at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
docker_spring | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | ... 3 common frames omitted
docker_spring |
docker_spring | 2020-02-07 14:45:29.119 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster description not yet available. Waiting for 30000 ms before timing out
docker_spring | 2020-02-07 14:45:59.125 INFO 1 --- [ main] ConditionEvaluationReportLoggingListener :
docker_spring |
docker_spring | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
docker_spring | 2020-02-07 14:45:59.131 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
docker_spring |
docker_spring | java.lang.IllegalStateException: Failed to execute CommandLineRunner
docker_spring | at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at com.example.demo.MainClass.main(MainClass.java:8) ~[classes!/:0.0.1-SNAPSHOT]
docker_spring | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
docker_spring | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
docker_spring | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
docker_spring | at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
docker_spring | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52) ~[app.jar:0.0.1-SNAPSHOT]
docker_spring | Caused by: org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
docker_spring | at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:90) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2902) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:587) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.insertDocument(MongoTemplate.java:1494) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:1294) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1226) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1211) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at com.example.demo.DemoApplication.run(DemoApplication.java:40) ~[classes!/:0.0.1-SNAPSHOT]
docker_spring | at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) ~[spring-boot-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | ... 13 common frames omitted
docker_spring | Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
docker_spring | at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:182) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41) ~[mongodb-driver-core-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:145) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:100) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:277) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:201) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1048) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:498) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:482) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:476) ~[mongodb-driver-3.11.2.jar!/:na]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocument$16(MongoTemplate.java:1500) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:585) ~[spring-data-mongodb-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
docker_spring | ... 19 common frames omitted
docker_spring |
docker_spring | 2020-02-07 14:45:59.140 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
docker_mongo | 2020-02-07T14:45:59.146+0000 I NETWORK [conn1] end connection 192.168.0.3:53260 (0 connections now open)
docker_spring exited with code 1
最佳答案
很简单
MongoClients.create()
without any parameters to connect to a MongoDB instance running on localhost on port 27017.
docker_spring | 2020-02-07 14:45:28.616 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[docker_mongo:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
docker_mongo | 2020-02-07T14:45:28.643+0000 I NETWORK [listener] connection accepted from 192.168.0.3:53260 #1 (1 connection now open)
docker_mongo | 2020-02-07T14:45:28.649+0000 I NETWORK [conn1] received client metadata from 192.168.0.3:53260 conn1: { driver: { name: "mongo-java-driver|legacy", version: "3.11.2" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "5.3.0-28-generic" }, platform: "Java/Oracle Corporation/11.0.6+10" }
docker_spring | 2020-02-07 14:45:28.659 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to docker_mongo:27017
docker_spring | 2020-02-07 14:45:28.663 INFO 1 --- [ker_mongo:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=docker_mongo:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 2]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2669943}
@SpringBootApplication
@ComponentScan({"com.example.client","com.example.demo","com.example.model"})
public class DemoApplication implements CommandLineRunner {
@Autowired
private MongoOperations mongoOps;
@Override
public void run(String... args) throws Exception {
mongoOps.insert(new Expenditure("Aldi",10.01, LocalDate.parse("2019-10-05")));
System.out.println(mongoOps.findOne(new Query(Criteria.where("name").is("Aldi")),Expenditure.class));
mongoOps.dropCollection("expenditure");
PopulateExpenditureDB.populateExpenditures(mongoOps);
}
}
关于spring - Spring Docker Container停止是因为它已尝试连接到localhost MongoDB,即使它已正确连接到MongoDB Container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60116376/
我已经在 kubernetes 中部署了一个 3 pod mongodb statefulset,并且我正在尝试使用新的 mongodb+srv 连接字符串 (mongodb 3.6) 连接到具有 S
我已经创建了 MongoDB Atlas 帐户,并尝试连接。但出现以下错误。 MongoDB 连接错误 MongoNetworkError: 首次连接时无法连接到服务器 [cluster0-shard
我正在使用 Node-WebKit 创建桌面应用程序。该应用程序基本上是创建文档(员工日常工作的详细信息),任何注册用户都可以对这些文档发表评论。我正在创建的文档将被分成几个部分。用户将对特定部分发表
我正在尝试使用官方网站上的安装程序在我的本地机器上安装 mongo DB。但是我不断收到这条消息,有人可以帮忙吗? 我试过提供的解决方案 here但没有帮助。 最佳答案 我建议执行以下操作: 按 Wi
我对 MongoDB 和 MongoDB Compass 非常陌生。 我的客户集合中有大约 1000 条记录。如何通过 MongoDB 指南针一次删除所有记录。 非常感谢, 最佳答案 您可以使用 Mo
当我尝试在我的 Ubuntu 机器中安装 mongodb 时,apt-get 会显示以下选项 mongodb mongodb-clients mongodb-dev mongodb-server 谁能
如何将 Robomongo(或任何其他 mongodb 客户端)连接到由本地 Meteor 应用程序创建的 mongodb 实例? 最佳答案 确保 Meteor 正在本地主机上运行。打开终端窗口并运行
我需要在 MongoDB 中生成一个简单的频率表。假设我在名为 books 的集合中有以下文档。 { "_id": 1, genre: [ "Fantasy", "Crime"
我如何在 mongos mapreduce 中指定一个条件,就像我们在 mongos group 函数中所做的那样。 我的数据是这样的 {lid:1000, age:23}, {lid:3000, a
我的 mongodb 数据库文档中有几个 ID。我需要通过脚本在这些 ID 上创建索引,这样我就不必一次又一次地运行 ensureIndex 命令。 db.getCollection("element
在我的数据库中,每个包含项目的文档中都有一个嵌套的元素数组,格式如下: elements:[ { "elem_id": 12, items: [ {"i_id": 1
我正在构建一个应用程序,其中用户可以位于不同的时区,并且我运行的查询对他们的时区很敏感。 我遇到的问题是 MongoDB 似乎在查询时忽略了时区! 这是日期字段“2019-09-29T23:52:13
我正在研究使用 mongodb 进行分片,我有以下结构: 1 个 Mongod 到我的 ConfigServer,在 ReplicaSet 中只有 1 个成员 2 个分片,每个分片在 ReplicaS
我正在尝试获取一个 mongoDB 对象,例如 Friend1 包含另一个 mongoDB 对象 Friend2,该对象又包含第一个对象 Friend1本质上使它成为一个循环对象引用。 要么这样,要么
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
Mongo 版本 5.0.2。 Ubuntu 20.0 我在本地主机中启用了 MongoDB 连接的安全性。 我正在尝试通过以下命令使用身份验证详细信息连接我的本地主机 MongoDBmongo ad
我即将将分片的 MongoDB 环境从 2.0.7 升级到 2.2.9,最终我想升级到 2.4.9,但显然我需要通过 2.2 来完成。 2.2 的发行说明声明配置服务器应该首先升级其二进制文件,然后是
目前,我无法在我的虚拟 Ubuntu 机器上远程连接 mongodb 服务器。我无法使用在我的 Windows PC 上运行的 Robomongo 客户端连接,该 PC 也运行 vm。 这是两台电脑的
我创建了一个免费的 mongodb 集群。我创建了一个用户,设置了与 mongodb compass 的连接,复制了连接字符串,然后打开了我的 mongodb compass。将复制的字符串粘贴到那里
我使用 java 代码创建了 mongo 数据库集合索引 dbCollection.createIndex("accountNumber"); 当我看到索引使用 db.accounts.getInde
我是一名优秀的程序员,十分优秀!