在Docker上使用MongoDB进行Spring Boot [英] Spring Boot with MongoDB on Docker

查看:96
本文介绍了在Docker上使用MongoDB进行Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天来,我正在尝试部署我的Spring Boot OAuth2项目。它具有3个不同的模块。(身份验证服务器,资源服务器和前端)
身份验证和资源服务器具有自己的* .yml文件,用于进行配置,例如mongodb名称端口,服务器配置文件IP等。
我到底想做什么?我想在docker上部署Spring Boot应用程序,但我不想将我的数据库(mongodb)作为容器放置在Docker上。
我不确定这种结构是否可行?
因为当我在本地(localhost:27017)上运行mongodb之后,尝试在本地上部署spring boot应用程序docker作为容器,我收到MongoDB的超时异常。该应用程序无法连接到外部mongoDB(非docker容器)。

In these days, I am trying to deploy my Spring Boot OAuth2 project. 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? I want to deploy spring boot application on docker but i dont want to put my database(mongodb) on docker as a container. 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. The application couldnt connect to external mongoDB(non docker container).

我该怎么办?我应该在docker上运行mongodb吗?我也尝试过,Mongo运行成功,但是弹簧容器仍然无法运行并连接到mongo。
我试图在不使用mongodb的情况下运行另一个spring boot应用程序,它运行成功,并且我通过ip& port从浏览器发出了请求,我得到了应用程序的预期响应。

What should I do? Should I run mongodb on docker? I tried it also, Mongo runs successfully but still spring container couldnt run and connect to 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.


*** 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


推荐答案

问题:我该怎么办?

至少出于开发目的,我建议对您的mongodb实例也使用docker。另外,我使用RabbitMQ进行了类似的设置,当我将docker用于Docker时,它也解决了很多问题。使用 docker-compose 设置所有内容都变得更加容易。稍后,您仍然可以通过spring属性指定要使用的mongodb实例。

At least for developing purposes I would recommend to also use docker for your mongodb instance. I had a similar setup with RabbitMQ in addition and it solved a lot of problems when I used docker for those as well. Using docker-compose to set everything up makes it even easier. Later you can still specify which mongodb instance you want to use through your spring properties.

问题:我也尝试过,Mongo运行成功,但spring容器仍然无法运行运行并连接到mongo

问题可能是因为您尚未为服务设置任何网络或主机名。您的spring应用程序无法解析mongo服务器的主机名,因为您在属性中为mongodb服务器指定了 127.0.0.1

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.

我建议为您的mongodb使用docker,并使用 docker-compose.yml 这样的文件来设置所有内容:

I would recommend using docker for your mongodb and use a docker-compose.yml file like this to set everything up:

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

当然,您随后需要通过修改属性文件或指定环境变量docker-compose.yml 文件。

Of course you then need to adapt your property file or specify environment variables through your docker-compose.yml file.

这篇关于在Docker上使用MongoDB进行Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆