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

Kri*_*ran 12 java spring h2 spring-webflux

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

sp0*_*00m 20

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

@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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢你。这足以满足我的要求。对人们克服事物的方式感到惊讶。 (2认同)
  • 嗨,使用与上面相同的代码,出现“找不到文件:h2”之类的错误 (2认同)
  • 删除 url 路径。可以通过 http://localhost:8082 访问 (2认同)