2017-04-06 100 views
5

在我的項目中,我創建了3個彈簧引導應用程序。第一個spring引導應用程序有h2嵌入式數據庫。現在我想直接從第二和第三彈簧啓動應用程序訪問這個數據庫,而不用編寫任何服務來獲取這些數據。那麼有誰能告訴我,我怎麼能做到這一點?如何從另一個彈簧引導應用程序訪問內存中的一個彈簧引導應用程序的數據庫

+0

Raj,別忘了接受一個幫助你的答案... – Cepr0

回答

11

您可以設置H2 Server作爲春豆。

編輯的pom.xml(刪除<scope>runtime</scope>):

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

H2服務器豆加入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連接到您的應用程序的數據庫。

UPDATE

有試圖連接到H2爲1.5.x的版本春季啓動應用程序時,得到一個錯誤的機會。在這種情況下,只需更換一個版本的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"); 
} 

然後你可以用下面的URL連接到這些應用程序的H2 DB:

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

@Salman'Server'是來自'com.h2database:h2'的'org.h2.tools'包中的類artifact - 看依賴。並且不要忘記從中刪除' runtime'。 – Cepr0

+1

謝謝。順便說一句,在你嘗試連接的其他應用程序的配置文件中,你應該將'spring.datasource.url'設置爲'jdbc:h2:tcp:// localhost:9092/mem:dbname' – sedooe

+0

@sedooe關閉原因。我寫到了 - 請看答案的最後幾行。或者你的意思是不同的? – Cepr0

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 

How to use h2 as a server

Similar question 1

Similar question 2

相關問題