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

H2 db not accessible at localhost:8080/h2-console when using webflux

使用 webflux 时,无法在 localhost:8080/h2-console 访问 H2 数据库。我在某处读到这仅在开发基于 Servlet 的应用程序时可用。但我正在将 Webflux 与 Netty 结合使用。那么有没有办法在这样的应用程序中看到h2控制台?

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

@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)。

我找到了一个与 sp00m 描述完全相同的库,它可能对某些人有帮助。开箱即用。

https://mvnrepository.com/artifact/me.yaman.can/spring-boot-webflux-h2-console

和github页面:https://github.com/canyaman/spring-boot-webflux-h2-console