如何从另一个Spring Boot应用程序访问一个Spring Boot应用程序的内存h2数据库

拉吉

在我的项目中,我创建了3个spring boot应用程序。第一个spring boot应用程序具有h2嵌入式数据库。现在,我想直接从第二和第三次Spring Boot应用程序访问此数据库,而无需编写任何服务来获取此数据。那么有人可以告诉我如何实现这一目标吗?

Cepr0

您可以将H2服务器设置为Spring Bean。

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

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

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

@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

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

更新

尝试连接到1.5.x版本的Spring Boot应用程序的H2时,可能会出现错误。在这种情况下,只需将H2的版本更改为先前的版本,例如:

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

更新2

如果需要在同一主机上同时运行带有H2的多个应用程序,则应在它们的主机上分别设置不同的H2端口Server.createTcpServer,例如: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");
}

然后,您可以使用以下网址连接到这些应用程序的H2 DB:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从2个不同的Spring Boot应用程序访问同一数据库

如何从另一个Maven模块启动Spring Boot应用程序?

将Spring Boot应用程序添加为对另一个Spring Boot应用程序的依赖

具有H2文件数据库的Spring Boot应用程序

在Spring Boot应用程序中找不到我的h2数据库

H2-多个应用程序访问同一个H2数据库

如何在Spring Boot中将POST请求从一个Web应用程序正确转发到另一个Web应用程序?

是否可以在另一个Spring Boot应用程序中嵌套一个Spring Boot应用程序?

将Spring Boot应用程序导入另一个项目

如何从我的Spring Boot应用程序控制器创建对另一个API的PATCH请求?

Spring Boot保留一个域的应用程序

如何使用一个 Dockerfile dockerizing 一个 spring boot 应用程序?

从Android应用程序访问MySQL中的另一个数据库

如何访问另一个应用程序数据库并插入一些记录

H2 数据库 - 为多个 Spring Boot 应用程序使用持久存储

H2 数据库 - 为多个 Spring Boot 应用程序使用持久存储

如何与另一个 Python 应用程序共享 Django 数据库?

如何将Swagger API从一个Spring应用程序包含到另一个Spring应用程序中?

如何在一个存储库中具有多个spring boot应用程序?

从另一个应用程序访问特定于应用程序的数据

我可以从另一个应用程序(Spring Boot 或 Micronaut 应用程序)内部启动另一个应用程序(启动 Redis 缓存服务器)吗?

如何监控另一个应用程序?

如何在具有共同现有数据库的不同应用程序中访问另一个应用程序的模型类?

如何将数据库的数据从一个 django 应用程序复制到另一个应用程序

如何允许另一个应用程序访问我的应用程序的数据目录?

无法在Spring应用程序中接收从另一个Java应用程序发送的表单数据

从另一个应用程序服务访问应用程序服务

在RUNTIME时从Spring Boot应用程序中的两个相同数据库连接到一个数据库

如果移到Spring Boot应用程序的另一个软件包中,为什么找不到我的Spring Data存储库?