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

问题描述 投票:0回答:3

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

spring-boot spring-data spring-data-jpa h2
3个回答
30
投票

您可以将 H2 Server 设置为 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

使用此 URL 的好处是,您可以直接从 IDE 连接到应用程序的数据库

更新

尝试连接 1.5.x 版本的 H2 for Spring Boot 应用程序时,有可能出现错误。在这种情况下,只需将 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"); }
然后您可以使用以下网址连接到这些应用程序的 H2 DB:

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

0
投票
您可以在服务器模式下运行

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

如何使用h2作为服务器

类似问题1

类似问题2


0
投票
问题就出在这里。考虑同一应用程序的不同实例。这意味着很难配置,假设 Sample-app-instance-1 在 :mem 上运行,sample-app-instance-2 在 :tcp 上运行。它应该直接在 TCP 上运行,以便不同的实例能够共享相同的内存数据库。

我的问题就在这里;我有相同的实现,但应用程序无法运行并出现以下错误;

原因:org.flywaydb.core.internal.exception.FlywaySqlException:无法从数据库获取连接:连接已损坏:“java.net.ConnectException:连接被拒绝:没有更多信息:localhost:9092”[90067-220]

SQL状态:90067 错误代码:90067 消息:连接已损坏:“java.net.ConnectException:连接被拒绝:没有更多信息:localhost:9092”[90067-220]

但是如果我如下手动运行 h2 服务器,我的不同实例可以连接并且应用程序可以正常运行。

java -cp h2-2.2.220.jar org.h2.tools.Server -tcp -tcpAllowOthers -tcpPort 9092 -baseDir \h2db -ifNotExists

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.