如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库
Posted
技术标签:
【中文标题】如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库【英文标题】:How to access in memory h2 database of one spring boot application from another spring boot application 【发布时间】:2017-09-01 13:11:52 【问题描述】:在我的项目中,我创建了 3 个 spring boot 应用程序。第一个 Spring Boot 应用程序具有 h2 嵌入式数据库。现在我想直接从我的第二个和第三个 spring boot 应用程序访问这个数据库,而不需要编写任何服务来获取这些数据。那么谁能告诉我如何才能做到这一点?
【问题讨论】:
Raj,别忘了接受对你有帮助的答案... 【参考方案1】:您可以将 H2 服务器设置为 Spring Bean。
首先编辑 pom.xml - 从 h2 依赖中删除 <scope>runtime</scope>
:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
然后将H2服务器bean添加到SpringBootApplication
或Configuration
类:
@SpringBootApplication
public class Application
public static void main(String[] args)
SpringApplication.run(Application.class, args);
/**
* Start internal H2 server so we can query the DB from IDE
*
* @return H2 Server instance
* @throws SQLException
*/
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
最后 - 编辑application.properties
- 设置数据库名称:
spring.datasource.url=jdbc:h2:mem:dbname
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
然后您可以使用此连接从外部连接到此 H2 服务器(例如,使用 H2 DB 连接到您的应用程序):
jdbc:h2:tcp://localhost:9092/mem:dbname
使用此网址作为奖励,您可以直接从您的 IDE 连接到应用程序的数据库。
更新
尝试连接到 H2 for Spring Boot app 1.5.x 版本时可能会出错。在这种情况下,只需将 H2 的版本更改为以前的版本,例如:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
更新 2
如果您需要在同一主机上同时运行多个带有 H2 的应用程序,您应该在 Server.createTcpServer
方法中为它们设置不同的 H2 端口,例如:9092、9093 等。
// First App
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
// Second App
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9093");
然后您可以使用以下 url 连接到这些应用程序的 H2 DB:
App1 H2: jdbc:h2:tcp://localhost:9092/mem:dbname
App2 H2: jdbc:h2:tcp://localhost:9093/mem:dbname
【讨论】:
@SalmanServer
是来自 com.h2database:h2
工件的 org.h2.tools
包的类 - 请参阅依赖项。并且不要忘记从中删除<scope>runtime</scope>
。
谢谢。顺便说一句,在您尝试连接的其他应用程序的配置文件中,您应该将spring.datasource.url
设置为jdbc:h2:tcp://localhost:9092/mem:dbname
@sedooe 关闭原因。我写的 - 见答案的最后几行。或者你的意思是不同的?..
啊抱歉,没看到那行:)
@AndyDufresne 您只需要第一个应用程序拥有数据库,第二个应用程序连接到第一个应用程序数据库?在这种情况下,您应该只在第一个应用程序中设置 h2Server
bean。第一个应用程序应该使用 url jdbc:h2:mem:dbname
连接到自己的数据库,第二个应用程序应该使用 url jdbc:h2:tcp://localhost:9092/mem:dbname
连接到数据库。【参考方案2】:
您可以在服务器模式下运行H2
。
import org.h2.tools.Server;
...
// start the TCP Server
server = Server.createTcpServer("-tcpAllowOthers").start();
...
// stop the TCP Server
server.stop();
Usage: java org.h2.tools.Server
When running without options, -tcp, -web, -browser and -pg are started.
Options are case sensitive. Supported options are:
[-help] or [-?] Print the list of options
[-web] Start the web server with the H2 Console
[-webAllowOthers] Allow other computers to connect - see below
[-webDaemon] Use a daemon thread
[-webPort ] The port (default: 8082)
[-webSSL] Use encrypted (HTTPS) connections
[-browser] Start a browser connecting to the web server
[-tcp] Start the TCP server
[-tcpAllowOthers] Allow other computers to connect - see below
[-tcpDaemon] Use a daemon thread
[-tcpPort ] The port (default: 9092)
[-tcpSSL] Use encrypted (SSL) connections
[-tcpPassword ] The password for shutting down a TCP server
[-tcpShutdown ""] Stop the TCP server; example: tcp://localhost
[-tcpShutdownForce] Do not wait until all connections are closed
[-pg] Start the PG server
[-pgAllowOthers] Allow other computers to connect - see below
[-pgDaemon] Use a daemon thread
[-pgPort ] The port (default: 5435)
[-properties ""] Server properties (default: ~, disable: null)
[-baseDir ] The base directory for H2 databases (all servers)
[-ifExists] Only existing databases may be opened (all servers)
[-trace] Print additional trace information (all servers)
The options -xAllowOthers are potentially risky.
For details, see Advanced Topics / Protection against Remote Access.
See also http://h2database.com/javadoc/org/h2/tools/Server.html
How to use h2 as a server
Similar question 1
Similar question 2
【讨论】:
以上是关于如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库的主要内容,如果未能解决你的问题,请参考以下文章