简体   繁体   English

在 Docker 上使用 MongoDB 进行 Spring Boot

[英]Spring Boot with MongoDB on Docker

In these days, I am trying to deploy my Spring Boot OAuth2 project.这些天,我正在尝试部署我的 Spring Boot OAuth2 项目。 It has 3 different modules.(Authentication Server, Resource Server and Front-end) Authentication and Resource servers have own *.yml file for configurations such as mongodb name-port, server profile-ip etc. What I am trying to do exactly?它有 3 个不同的模块。(身份验证服务器、资源服务器和前端)身份验证和资源服务器有自己的 *.yml 文件,用于诸如 mongodb 名称端口、服务器配置文件 ip 等配置。我到底想做什么? I want to deploy spring boot application on docker but i dont want to put my database(mongodb) on docker as a container.我想在 docker 上部署 spring boot 应用程序,但我不想将我的数据库(mongodb)作为容器放在 docker 上。 I am not sure this structure is possible or not ?我不确定这种结构是否可行? Because When i run my mongodb on my local(localhost:27017) after that try to deploy spring boot application on local docker as a container, i am getting Timeout exception for MongoDB .因为当我在本地(localhost:27017)上运行我的 mongodb 之后,尝试将 Spring Boot 应用程序部署在本地 docker 上作为容器,我收到MongoDB 的超时异常 The application couldnt connect to external mongoDB(non docker container).应用程序无法连接到外部 mongoDB(非 docker 容器)。

What should I do?我该怎么办? Should I run mongodb on docker?我应该在 docker 上运行 mongodb 吗? I tried it also, Mongo runs successfully but still spring container couldnt run and connect to mongo.我也试过了,Mongo 运行成功,但 spring 容器仍然无法运行并连接到 mongo。 I tried to run another spring boot app without mongodb, it is working successfully and i made request from browser by ip&port, i got response from application as i expected.我试图在没有 mongodb 的情况下运行另一个 Spring Boot 应用程序,它运行成功,我通过 ip&port 从浏览器发出请求,我得到了应用程序的响应,正如我预期的那样。

*** MONGO URL ****
mongodb://127.0.0.1:27017/db-localhost

**** Authentication server .yml file   ****
server:
    port: 9080
    contextPath: /auth-service
    tomcat:
          access_log_enabled: true
          basedir: target/tomcat
security:
    basic:
        enabled: false
spring:
    profiles:
        active: development
    thymeleaf:
        cache: false
mongo:
    db:
      server: 127.0.0.1
      port: 27017
logging:
 level:
  org.springframework.security: DEBUG

---

spring:
  profiles: development
  data:
    mongodb:
      database: db-localhost

---

spring:
  profiles: production
  data:
    mongodb:
      database: db-prod

---


***** DOCKER FILE *******
FROM java:8
VOLUME /tmp
ADD auth-server-1.0-SNAPSHOT.jar app.jar
EXPOSE 9080
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]


**** DOCKER COMMAND *******
docker run -it -P --name authserver authserver

The issue with your configuration is referencing the mongodb from inside of the authservice on 127.0.0.1 which is the loopback adapter inside of the authservice container.您的配置问题是从127.0.0.1上的 authservice 内部引用 mongodb,这是 authservice 容器内部的环回适配器。 So you tell your spring application that mongodb is running in the same container as the authservice spring application, which is not the case.所以你告诉你的 spring 应用程序 mongodb 与 authservice spring 应用程序在同一个容器中运行,但事实并非如此。

Either you are running your database as an own container (which requires to handle the data volumes correctly) and referencing it using the container name as hostname (via link) or you need to reference the externally running mongodb instance with the correct address.您要么将数据库作为自己的容器运行(这需要正确处理数据量)并使用容器名称作为主机名(通过链接)引用它,要么需要使用正确的地址引用外部运行的 mongodb 实例。 This would be the ip address of the machine running the docker daemon (I assume for your local environment something like 192.168.0.xxx ).这将是运行 docker 守护进程的机器的 IP 地址(我假设您的本地环境类似于192.168.0.xxx )。

Question: What should I do?问题:我该怎么办?

At least for developing purposes I would recommend to also use docker for your mongodb instance.至少出于开发目的,我建议您也将 docker 用于您的 mongodb 实例。 I had a similar setup with RabbitMQ in addition and it solved a lot of problems when I used docker for those as well.此外,我对 RabbitMQ 进行了类似的设置,当我使用 docker 时,它也解决了很多问题。 Using docker-compose to set everything up makes it even easier.使用docker-compose来设置一切使它变得更加容易。 Later you can still specify which mongodb instance you want to use through your spring properties.稍后您仍然可以通过 spring 属性指定要使用的 mongodb 实例。

Problem: I tried it also, Mongo runs successfully but still spring container couldnt run and connect to mongo问题:我也试过了,Mongo运行成功,但是spring容器仍然无法运行并连接到mongo

The problem is probably because you have not set up any networks or hostnames for you services.问题可能是因为您尚未为您的服务设置任何网络或主机名。 Your spring application can not resolve the hostname of your mongo server, since you specified 127.0.0.1 for your mongodb server in your properties.您的 spring 应用程序无法解析 mongo 服务器的主机名,因为您在属性中为 mongodb 服务器指定了127.0.0.1

I would recommend using docker for your mongodb and use a docker-compose.yml file like this to set everything up:我建议为您的 mongodb 使用docker-compose.yml并使用这样docker-compose.yml文件来设置所有内容:

version: '3.7'
services:
  resource-server:
    image: demo/resource-server:latest
    container_name: resource-server
    depends_on:
      - mongodb-example
    networks:
      - your-network
    ports:
      - 8080:8080

  auth-server:
    image: demo/auth-server:latest
    container_name: auth-server
    depends_on:
      - mongodb-example
    networks:
      - your-network
    ports:
      - 8081:8080

  mongodb-example:
    image: mongo:latest
    container_name: mongo-example
    hostname: mongo-example
    networks:
      - your-network
    ports:
      - 27017:27017

networks:
  your-network:
    name: network-name

Of course you then need to adapt your property file or specify environment variables through your docker-compose.yml file.当然,您需要调整您的属性文件或通过您docker-compose.yml文件指定环境变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM