簡體   English   中英

如何從 2 個不同的 spring 引導應用程序訪問相同的數據庫

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

我有 2 個 spring 微服務應用程序,並希望它們訪問同一個數據庫。 但是當我運行它們時,這兩個應用程序都會創建不同的數據庫。 Application.yml 文件:

服務一:

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

服務二:

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

我認為通過提供相同的 URL,一項服務將創建數據庫,而另一項服務將只使用它。

H2是memory數據庫中的一個,使用一個允許多個用戶的數據庫。

更新:

實際上,我並不精確。 H2 具有服務器模式,但您正在運行嵌入在應用程序中的數據庫。

您需要將數據庫作為服務,然后您可以從微服務應用程序連接到正在運行的數據庫。

附帶說明一下,讓不同的微服務訪問同一個數據庫是一種常見的反模式。

數據庫——如果它被共享的話——應該只被一個微服務組件的實例共享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM