com.mongodb.MongoSocketOpenException:异常打开套接字(MongoDB,Docker)

时间:2017-08-17 18:56:52

标签: java mongodb spring-boot docker-compose netflix-eureka

我尝试使用docker image启动我的应用程序(Spring Boot + Spring Cloud + Eureka + MongoDB),但我无法连接到MongoDB。

例外:

exception "com.mongodb.MongoSocketOpenException: Exception opening socket."

我使用执行命令启动我的应用程序: docker-compose up --build

Docker日志:

enter image description here

application.yml:

# Spring properties
spring:
  application:
    name: car-service
  data:
    mongodb.host: localhost
    mongodb.port: 32769
    mongodb.uri: mongodb://localhost/test
    mongo.repositories.enabled: true


# Discovery Server Access
eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/

# HTTP Server (Tomcat) Port
server: 
  port: 2220

error:
  whitelabel:
    enabled: false

搬运工-compose.yml:

eureka:
  build: ./eureka-discovery-service
  ports:
    - "8761:8761"

mongodb:
  image: mongo:3.0.4
  ports:
    - "32769:32769"

postgresql:
  image: postgres:9.6.1
  ports:
    - "32770:32770"

gateway-service:
  build: ./gateway-service
  ports:
    - "9090:9090"
  links:
    - eureka
  environment:
    SPRING_APPLICATION_NAME: gateway-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/

airplane-service:
  build: ./airplane-service
  ports:
    - "2222:2222"
  links:
    - eureka
    - postgresql
  environment:
    SPRING_APPLICATION_NAME: airplane-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
    SPRING_SLEUTH_ENABLED: "true"
    SPRING_DATASOURCE_POSTGRESQL_URL: jdbc:postgresql://localhost:32770/postgres

car-service:
  build: ./car-service
  ports:
    - "2220:2220"
  links:
    - eureka
    - mongodb
  environment:
    SPRING_APPLICATION_NAME: car-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
    SPRING_SLEUTH_ENABLED: "true"
    SPRING_DATA_MONGODB_URI: mongodb://localhost:32769/test


machine-service:
  build: ./machine-service
  ports:
    - "2224:2224"
  links:
    - eureka
  environment:
    SPRING_APPLICATION_NAME: machine-service
    SPRING_PROFILES_ACTIVE: enableEureka
    EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
    SPRING_SLEUTH_ENABLED: "true"

为什么我打开套接字有异常?如何解决这个问题?

4 个答案:

答案 0 :(得分:5)

您正在将属性文件中的mongodb主机设置为 localhost 。在容器本地主机地址本身中,但您的mongodb在该容器(汽车服务)中哪个汽车服务运行。当您使用docker compose时,您可以使用其名称寻址容器。在你的情况下,它是 mongodb

答案 1 :(得分:0)

阐明@barbakini的答案,在application.yaml中定义它,请使用:

spring.data.mongodb.host: mongodb

答案 2 :(得分:0)

您的mongoDB服务未启动

通过以下命令检查状态

sudo service mongodb status

sudo service mongodb start

希望它能正常工作,可能还有几个原因, 例如您在应用程序中为mongodb服务定义的配置不正确,例如port。

答案 3 :(得分:0)

尝试添加
network_mode: host
并删除
links: ....
对于想要与mongo或eureka或postgresql连接的所有服务,都在docker-compose.yml中。
这样,您将连接到docker localhost。