【问题标题】:H2 db not accessible at localhost:8080/h2-console when using webflux使用 webflux 时无法在 localhost:8080/h2-console 访问 H2 db
【发布时间】:2019-03-27 16:39:17
【问题描述】:

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

【问题讨论】:

    标签: java spring h2 spring-webflux


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。这足以满足我的要求。对人们克服事物的方式感到惊讶。
    • 嗨,使用与上面相同的代码,出现类似“找不到文件:h2”的错误
    • 与@prasannajoshi 相同的问题,“找不到文件:h2-console”,没有异常或堆栈跟踪
    • 去掉url路径。可通过localhost:8082 访问
    • 您可以将 start 方法移动到您的应用程序类中,然后在 main() 函数中启动它,它就可以工作了。只需访问 localhost:8082
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2017-01-16
      • 2017-12-24
      • 2011-07-01
      • 1970-01-01
      • 2023-04-04
      • 2017-06-14
      • 2019-09-28
      • 2020-06-04
      相关资源
      最近更新 更多