Springboot Mongo docker-compose无法连接

时间:2019-07-30 02:39:40

标签: spring mongodb spring-boot docker

我的docker-compose.yml如图所示

version: '3'
services:
  backend-service:
    image: 'storio-360-service:v1.0'
    build: .
    container_name: storio-360-service
    ports:
      - 8090:8080
    depends_on:
      - db_mongo
    links:
      - db_mongo
  db_mongo:
    image: 'mongo'
    container_name: db_mongo
    ports:
      - 27018:27017
    volumes:
      - /home/bob/Bob/data:/data/db
    restart: always

我的Dockerfile是

FROM openjdk:8u111-jdk-alpine
VOLUME /tmp
ADD /target/backend-service-1.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
CMD curl "db_mongo:27018"

由于此设置,我春天的存储库中就有此代码

@Repository
public class WatcherRepository {

    private MongoOperations mongoOps;

    @Autowired
    public WatcherRepository() {
        String myHost = "db_mongo";
        this.mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient(myHost), MongoConstant.DATABASE_NAME));
    }

    // Other code to access the mongo db
}

但是,当我运行docker-compose up --build,

当Spring boot启动时,我得到以下信息,

02:24:55.871 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
storio-360-service    | 02:24:55.923 [cluster-ClusterId{value='5d3faa779470450001317225', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017
storio-360-service    | com.mongodb.MongoSocketOpenException: Exception opening socket
storio-360-service    |         at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
storio-360-service    |         at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
storio-360-service    |         at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
storio-360-service    |         at java.lang.Thread.run(Thread.java:745)
storio-360-service    | Caused by: java.net.ConnectException: Connection refused (Connection refused)
storio-360-service    |         at java.net.PlainSocketImpl.socketConnect(Native Method)
storio-360-service    |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
storio-360-service    |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
storio-360-service    |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
storio-360-service    |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
storio-360-service    |         at java.net.Socket.connect(Socket.java:589)
storio-360-service    |         at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
storio-360-service    |         at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
storio-360-service    |         ... 3 common frames omitted
storio-360-service    | 02:24:56.060 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500

然后,Spring引导程序会在成功设置后显示正在等待呼叫。

02:34:39.274 [main] INFO  o.d.i.i.storio.service.Service - Started Service in 5.353 seconds (JVM running for 6.085)

但是,当我进行将调用mongodb的api调用时,它向我显示错误

Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1: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=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]

我是Docker的新手,很明显,我的springboot无法访问mongo。我在Dockerfile或docker-compose中做错了什么?谢谢

1 个答案:

答案 0 :(得分:0)

好像您要将mongodb端口转发到27018

   - 27018:27017

您还没有在Spring配置中指定27018

因此,默认情况下,Spring引导会尝试连接到27017

解决方案: 要么删除端口转发,要么更新Spring Boot配置以在27018上使用mongo


更新:

以下是一些Spring Boot OP的mongo自动配置技巧。

依赖于mongodb启动器进行春季启动

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>

在应用程序属性中提供Mongo DB详细信息。

spring.data.mongodb.database= # Database name.
spring.data.mongodb.host= # Mongo server host. Cannot be set with URI.
spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with URI.
spring.data.mongodb.port= # Mongo server port. Cannot be set with URI.
spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with URI

(这里OP需要指定他的mongodb主机和端口)

现在,剩下的唯一事情就是在您要使用的地方自动接线MongoTemplate

@Autowired
private MongoTemplate mongoTemplate;