如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库 [英] How to access in memory h2 database of one spring boot application from another spring boot application

查看:28
本文介绍了如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我创建了 3 个 spring boot 应用程序.第一个 Spring Boot 应用程序具有 h2 嵌入式数据库.现在我想直接从我的第二个和第三个 spring boot 应用程序访问这个数据库,而不需要编写任何服务来获取这些数据.那么谁能告诉我如何才能做到这一点?

In my project, i have created 3 spring boot application. First spring boot application has h2 embedded database. Now i want to access this database from my 2nd and 3rd spring boot application directly without writing any services to get this data. So can anyone tell me how can i achieve this?

推荐答案

您可以将 H2 Server 设置为 Spring Bean.

You can setup H2 Server as Spring Bean.

首先编辑 pom.xml - 从 h2 依赖中删除 <scope>runtime</scope>:

First edit pom.xml - delete <scope>runtime</scope> from h2 dependency:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

然后将H2服务器bean添加到SpringBootApplicationConfiguration类:

Then add H2 server bean to SpringBootApplication or Configuration class:

@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——设置数据库名称:

Last - edit application.properties - set the name of the database:

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 连接到您的应用程序):

Then you can connect to this H2 Server from outside (e.g. to your application with H2 DB) using this connection:

jdbc:h2:tcp://localhost:9092/mem:dbname

使用此网址作为奖励,您可以直接从您的 IDE 连接到应用程序的数据库.

As a bonus using this url you can connect to the database of your app right from your IDE.

更新

尝试连接到 H2 for Spring Boot app 1.5.x 版本时可能会出错.在这种情况下,只需将 H2 的版本更改为以前的版本,例如:

There is a chance of getting an error when trying to connect to the H2 for Spring Boot app of 1.5.x version. In this case just change a version of H2 to previous one, for example:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.193</version>
</dependency>

更新 2

如果您需要在同一主机上同时运行多个带有H2的应用程序,您应该在Server.createTcpServer方法中为它们设置不同的H2端口,例如:9092、9093等.

If you need to run several apps with H2 simultaneously on the same host you should set the different H2 ports on them in Server.createTcpServer mothod, for example: 9092, 9093, etc..

// 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:

Then you can connect to the H2 DB of these apps with following urls:

App1 H2: jdbc:h2:tcp://localhost:9092/mem:dbname
App2 H2: jdbc:h2:tcp://localhost:9093/mem:dbname

这篇关于如何从另一个 Spring Boot 应用程序访问一个 Spring Boot 应用程序的内存 H2 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆