使用WebFlux时,无法在localhost:8080/h2-console访问H2数据库。

20
H2 db在使用webflux时无法通过localhost:8080/h2-console访问。我在某个地方读到只有在开发基于Servlet的应用程序时才能使用此功能。但是我正在使用Webflux和Netty。所以在这样的应用程序中是否有办法查看h2控制台?
2个回答

28

我遇到了相同的问题,最终我手动在另一个端口上启动了控制台服务器:

@Component
@Profile("test") // <-- up to you
public class H2 {

    private org.h2.tools.Server webServer;

    private org.h2.tools.Server tcpServer;

    @EventListener(org.springframework.context.event.ContextRefreshedEvent.class)
    public void start() throws java.sql.SQLException {
        this.webServer = org.h2.tools.Server.createWebServer("-webPort", "8082", "-tcpAllowOthers").start();
        this.tcpServer = org.h2.tools.Server.createTcpServer("-tcpPort", "9092", "-tcpAllowOthers").start();
    }

    @EventListener(org.springframework.context.event.ContextClosedEvent.class)
    public void stop() {
        this.tcpServer.stop();
        this.webServer.stop();
    }

}

然后导航到 http://localhost:8082(不带 /h2-console)。


2
谢谢。这已经足够了,完全符合我的要求。人们克服问题的方式令人惊叹。 - Krishnakumar Ramachandran
2
嗨,使用与上面相同的代码,出现“文件未找到:h2”的错误。 - prasannajoshi
1
与@prasannajoshi相同的问题,“未找到文件:h2-console”,没有异常或堆栈跟踪。 - EvilJinious1
2
删除URL路径。它可以在http://localhost:8082上访问。 - Rodrigo Ribeiro
我似乎无法让它工作,当我访问localhost:8082时,会出现以下错误:"无法访问此网站"。 - AKJ
显示剩余3条评论

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接