在我的项目中,我创建了 3 个 Spring Boot 应用程序。第一个 Spring Boot 应用程序具有 h2 嵌入式数据库。现在我想直接从我的第二个和第三个 Spring Boot 应用程序访问此数据库,而无需编写任何服务来获取此数据。那么谁能告诉我如何实现这一目标?
您可以将 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
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
我的问题就在这里;我有相同的实现,但应用程序无法运行并出现以下错误;
原因:org.flywaydb.core.internal.exception.FlywaySqlException:无法从数据库获取连接:连接已损坏:“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