h2 不能访问localhost_使用Webflux时无法在localhost:8080 / h2-console上访问H2 db

本文介绍了解决H2数据库控制台在使用WebFlux和Netty时无法访问的问题。通过手动启动H2控制台服务器并指定不同端口,成功实现了在WebFlux环境下访问H2控制台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bd96500e110b49cbb3cd949968f18be7.png

H2 db is not accessible at localhost:8080/h2-console when using webflux. I read somewhere that this is available only when developing a Servlet based application. But I am using Webflux with Netty. So is there a way to see the h2 console in such an application?

解决方案

I had the same issue, I ended up booting the console server manually on another port:

@Component

@Profile("test") //

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

}

}

Then navigate to http://localhost:8082 (without /h2-console).

<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>*.properties</include> <include>*.yml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>*.sql</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>static/**</include> </includes> <targetPath>${project.build.outputDirectory}</targetPath> </resource> </resources> server.port=8081 server.servlet.context-path=/demo logging.level.com.esoft.crm.mapper=debug spring.datasource.url=jdbc:h2:file:./test;MODE=MySQL;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.h2.console.enabled=true spring.h2.console.path=/h2-console #spring.sql.init.mode=always #spring.sql.init.schema-locations=classpath:schema.sql #spring.sql.init.data-locations=classpath:data.sql spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.type.descriptor.sql=trace spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.maximum-pool-size=10 #spring.sql.init.mode=always #spring.sql.init.schema-locations=classpath:schema.sql #spring.sql.init.data-locations=classpath:data.sql spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql spring.datasource.initialization-mode=always spring.web.resources.static-locations=classpath:/static/ spring.mvc.static-path-pattern=/static/** mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.esoft.crm.po,com.esoft.crm.vo 且前端页面资源(css,img,index.html)等都存在与resource/static下,且没有打包,直接在idea环境下运行,运行后前端打不开,报错GET http://localhost:8080/js/chunk-vendors.1a0a0613.js net::ERR_CONNECTION_REFUSED :8080/img/icons/favicon-32x32.png:1 GET http://localhost:8080/img/icons/favicon-32x32.png net::ERR_CONNECTION_REFUSED :8080/img/icons/android-chrome-192x192.png:1 GET http://localhost:8080/img/icons/android-chrome-192x192.png net::ERR_CONNECTION_REFUSED :8080/img/icons/favicon-16x16.png:1 GET http://localhost:8080/img/icons/favicon-16x16.png net::ERR_CONNECTION_REFUSED localhost/:1 Error while trying to use the following icon from the Manifest: http://localhost:8080/img/icons/android-chrome-192x192.png (Download error or resource isn't a valid image),访问http://localhost:8081/demo/static/js/chunk-vendors.1a0a0613.js成功,内容是...//# sourceMappingURL=chunk-vendors.1a0a0613.js.map
最新发布
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值