简体   繁体   English

如何从 2 个不同的 spring 引导应用程序访问相同的数据库

[英]How to access the same DB from 2 different spring boot applications

I have 2 spring microservice Applications and want them to access the same DB.我有 2 个 spring 微服务应用程序,并希望它们访问同一个数据库。 But both Applications create a different databases when i run them.但是当我运行它们时,这两个应用程序都会创建不同的数据库。 The Application.yml files: Application.yml 文件:

Service 1:服务一:

server.port: 8002

logging:
  level:
    org:
      springframework:
        jdbc:
          core:
            DEBUG

spring:
  application:
    name: movie
  datasource:
    url: jdbc:h2:mem:movie_service
    driver-class-name: org.h2.Driver
  h2:
    console:
      enabled: true

eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8001/eureka}
    registryFetchIntervalSeconds: 1
  instance:
    leaseRenewalIntervalInSeconds: 1
    #preferIpAddress: true
eureka.instance.prefer-ip-address: false

Service 2:服务二:

server.port: 8003

spring:
  application.name: client
  datasource:
    url: jdbc:h2:mem:movie_service
    driver-class-name: org.h2.Driver
  h2:
    console:
      enabled: true

logging:
  level:
    org:
      springframework:
        jdbc:
          core:
            DEBUG

eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8001/eureka}
    registryFetchIntervalSeconds: 1
  instance:
    leaseRenewalIntervalInSeconds: 1
    #preferIpAddress: true
eureka.instance.prefer-ip-address: false

I thought by providing the same URL, one service will create the DB and the other one will just use it.我认为通过提供相同的 URL,一项服务将创建数据库,而另一项服务将只使用它。

H2 is an in memory database, use a database that allows multiple users. H2是memory数据库中的一个,使用一个允许多个用户的数据库。

UPDATE:更新:

actually, I was not precise.实际上,我并不精确。 H2 has a server mode, but you are running the database embedded in your application. H2 具有服务器模式,但您正在运行嵌入在应用程序中的数据库。

You need the database as a service and then you can connect from your microservice applications to that running database.您需要将数据库作为服务,然后您可以从微服务应用程序连接到正在运行的数据库。

As a side note, it is a common antipattern to have different microservices accessing the same database.附带说明一下,让不同的微服务访问同一个数据库是一种常见的反模式。

The database - if it is shared at all - should only be shared by instances of one microservice component.数据库——如果它被共享的话——应该只被一个微服务组件的实例共享。

暂无
暂无

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

相关问题 从两个不同的 Spring 引导应用程序访问相同的 MongoRepository 集合 - Accessing the same MongoRepository collection from two different Spring Boot Applications 如何远程调试在相同容器中运行的具有相同代码库但配置文件不同的两个spring boot应用程序? - How to remote debug two spring boot applications running in the same container with the same codebase but with different profiles? 在同一tomcat的不同端口上部署Angular 5和Spring Boot应用程序 - Deploy Angular 5 and spring boot applications on same tomcat at different ports 如何使用相同的log4j.properties文件记录两个不同的Spring-Boot应用程序? - How to log with same log4j.properties files for two different Spring-Boot Applications? 如何在同一台服务器上运行两个 Spring Boot 应用程序? - How to run two spring boot applications on same server? 如何在同一个 Tomcat 上部署多个带有外部配置的 Spring Boot 应用程序? - How to deploy multiple Spring boot applications with external configurations on the same Tomcat? 在Spring Boot Java项目中,如何将不同数据类型的属性从Mongo DB映射到Java类? - In spring boot java project how to map a property of different data types from a mongo DB to java class? 是否可以从两个不同的Java应用程序访问同一Ehcache对象? - Is it possible to access the same Ehcache object from two different java applications? 从脚本启动 Spring Boot 应用程序 - Starting Spring boot applications from script Spring Boot Multitenant - 使用 spring 引导同时或根据 http 请求从多个模式访问数据 - Spring Boot Multitenant - Access data from multiple schema at the same time or per http request using spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM