[英]Accessing the same MongoRepository collection from two different Spring Boot Applications
[英]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.