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

时间:2017-04-06 13:13:56

标签: spring-boot spring-data spring-data-jpa h2

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

2 个答案:

答案 0 :(得分:18)

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

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

<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

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

<强>更新

尝试连接到1.5.x版本的Spring Boot应用程序的H2时,可能会出错。在这种情况下,只需将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

答案 1 :(得分:0)

You can run H2 in the server mode.

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