[英]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.