代码之家  ›  专栏  ›  技术社区  ›  bsuart

通用域名格式。mongodb。MongoSocketOpenException:异常打开套接字(MongoDB,Docker)

  •  10
  • bsuart  · 技术社区  · 7 年前

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

    例外情况:

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

    我用execute命令启动应用程序: docker组装--构建

    Docker日志:

    enter image description here

    # 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
    

    docker撰写。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 回复  |  直到 7 年前
        1
  •  14
  •   barbakini    7 年前

    您正在将属性文件中的mongodb主机设置为 . 在容器本地主机地址中 它本身 在那个集装箱里(汽车服务)哪个汽车服务运行。在使用docker compose时,可以用容器的名称对其进行寻址。对你来说是这样的 数据库

        2
  •  7
  •   cs95 abhishek58g    5 年前

    澄清@barbakini的答案,并在应用程序中定义它。yaml使用:

    spring.data.mongodb.host: mongodb
    
        3
  •  0
  •   dinesh kandpal    4 年前

    你的mongoDB服务没有启动,

    通过以下命令检查状态

    sudo service mongodb status
    
    sudo service mongodb start
    

    就像您在应用程序中为mongodb服务定义的配置不正确一样,例如端口。

        4
  •  0
  •   Pankaj gupta    4 年前

    尝试添加
    network_mode: host

    links: ....
    对于所有希望连接mongo、eureka或postgresql的服务,请在docker compose中使用。yml。
    通过这样做,您将连接到docker localhost。