简体   繁体   English

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

[英]com.mongodb.MongoSocketOpenException: Exception opening socket(MongoDB, Docker)

I try to starting my application(Spring Boot + Spring Cloud + Eureka + MongoDB) with using docker image, but i can't get connection to MongoDB.我尝试使用 docker 镜像启动我的应用程序(Spring Boot + Spring Cloud + Eureka + MongoDB),但我无法连接到 MongoDB。

Exception:例外:

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

I starting my application with execute command: docker-compose up --build我使用执行命令启动我的应用程序: docker-compose up --build

Docker log:码头工人日志:

在此处输入图片说明

application.yml:应用程序.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

docker-compose.yml: docker-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"

Why I have exception opening a socket?为什么我打开套接字时出现异常? How to fix this problem?如何解决这个问题?

You are setting mongodb host in property file as localhost .您正在属性文件中将 mongodb 主机设置为localhost In a container localhost address itself , but your mongodb is not in that container(car-service) which car-service run in. While you are using docker compose, you can address a container with its name.在容器本地主机地址本身中,但您的 mongodb不在运行汽车服务的那个容器(汽车服务)中。当您使用 docker compose 时,您可以使用其名称来寻址容器。 In your case it is mongodb .在您的情况下,它是mongodb

澄清@barbakini 的答案,在applications.yaml 中定义它使用:

spring.data.mongodb.host: mongodb

Your mongoDB service is not UP ,您的 mongoDB 服务未启动,

Check the status by following command通过以下命令检查状态

sudo service mongodb status

sudo service mongodb start

Hope it should work, there could be several reasons as well , like the configuration you defined in your application for mongodb service is incorrect such as port .希望它可以工作,也可能有几个原因,例如您在应用程序中为 mongodb 服务定义的配置不正确,例如 port 。

Try to add尝试添加
network_mode: host
and remove并删除
links: ....
for all services, that want to connect with mongo or eureka or postgresql, inside your docker-compose.yml.对于所有想要与 docker-compose.yml 中的 mongo 或 eureka 或 postgresql 连接的服务。
By doing so you will connect to docker localhost.通过这样做,您将连接到 docker localhost。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.mongodb.MongoSocketOpenException: 异常打开套接字 - com.mongodb.MongoSocketOpenException: Exception opening socket 无法将mongodb与ssl连接,出现异常com.mongodb.MongoSocketOpenException:异常打开套接字 - Unable to connect mongodb with ssl, getting exception com.mongodb.MongoSocketOpenException: Exception opening socket com.mongodb.MongoSocketOpenException:异常打开套接字(完全删除 mongodb 后) - com.mongodb.MongoSocketOpenException: Exception opening socket (After removing mongodb altogether) org.mongodb.driver.connection : 连接池后台维护任务 com.mongodb.MongoSocketOpenException 时抛出的异常 - org.mongodb.driver.connection : Exception thrown during connection pool background maintenance task com.mongodb.MongoSocketOpenException MongoSocketOpenException - 连接到 MongoDB 时遇到问题 - MongoSocketOpenException - trouble with connecting to MongoDB 从 Spring App (docker-compose) 获取 Mongodb 连接上的“异常打开套接字” - Getting “Exception opening socket” on Mongodb connection from Spring App (docker-compose) 尝试连接 Java Spring 应用程序到远程 MongoDB 时出现异常打开套接字,连接被拒绝 - Exception opening socket, connection refused, when trying to connect Java Spring Application to a remote MongoDB Mongodb Atlas“连接到集群时出现套接字异常” - Mongodb Atlas "Got Socket exception on Connection To Cluster" 由于套接字异常,mongodb插入失败 - mongodb insert fails due to socket exception 引起原因:com.mongodb.MongoSocketReadException:异常接收消息 - Caused by: com.mongodb.MongoSocketReadException: Exception receiving message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM