NutzCN Logo
问答 nutz-boot tomcat方式集成websocket问题
发布于 1577天前 作者 qq_8c799e25 1672 次浏览 复制 上一个帖子 下一个帖子
标签:

以前是用nutz+tomcat集成的websocket,现在改成nutzboot+tomcat集成websocket,但连接ws报404,自定义的WebSocket好像没有初始化成功,

配置:

<properties>
        <nutzboot.version>2.4.0.v20200427</nutzboot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

....

<dependency>
		    <groupId>org.nutz</groupId>
		    <artifactId>nutz</artifactId>
		</dependency>

<dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutzboot-starter-tomcat</artifactId>
        </dependency>


<dependency>
	        <groupId>javax.websocket</groupId>
	        <artifactId>javax.websocket-api</artifactId>
	        <version>1.1</version>
	        <scope>provided</scope><!-- 非常重要哦 -->
	    </dependency>

<dependency>
		    <groupId>org.nutz</groupId>
		    <artifactId>nutz-plugins-websocket</artifactId>
		    <!-- <version>1.r.68.v20200427</version> -->
		</dependency>


启动日志:

[INFO ] 10:20:39.195 org.nutz.boot.banner.SimpleBannerPrinter.printBanner(SimpleBannerPrinter.java:34) - 
 _   _ ______                                      ___   
| \ | || ___ \  ______ ______ ______ ______ ______| \ \  
|  \| || |_/ / |______|______|______|______|______| |\ \ 
| . ` || ___ \  ______ ______ ______ ______ ______| | > >
| |\  || |_/ / |______|______|______|______|______| |/ / 
\_| \_/\____/                                     |_/_/  
  
:: Nutz Boot ::   (2.4.0.v20200427)
[INFO ] 10:20:39.406 org.nutz.ioc.loader.annotation.AnnotationIocLoader.<init>(AnnotationIocLoader.java:51) -  > scan 'com.smartpower.dataflow'
[INFO ] 10:20:39.442 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'mainLauncher                            ' - com.smartpower.dataflow.MainLauncher
[INFO ] 10:20:39.455 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'dataLinkModule                          ' - com.smartpower.dataflow.module.DataLinkModule
[INFO ] 10:20:39.456 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'timeModule                              ' - com.smartpower.dataflow.module.TimeModule
[INFO ] 10:20:39.462 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'userModule                              ' - com.smartpower.dataflow.module.UserModule
[INFO ] 10:20:39.474 org.nutz.ioc.loader.annotation.AnnotationIocLoader.<init>(AnnotationIocLoader.java:51) -  > scan 'org.nutz.boot.starter'
[INFO ] 10:20:39.507 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'swaggerServletStarter                   ' - org.nutz.boot.starter.swagger.SwaggerServletStarter
[INFO ] 10:20:39.514 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'nutFilterStarter                        ' - org.nutz.boot.starter.nutz.mvc.NutFilterStarter
[INFO ] 10:20:39.517 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'whaleFilterStarter                      ' - org.nutz.boot.starter.nutz.mvc.WhaleFilterStarter
[INFO ] 10:20:39.518 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'nutDaoStarter                           ' - org.nutz.boot.starter.nutz.dao.NutDaoStarter
[INFO ] 10:20:39.527 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'dataSourceStarter                       ' - org.nutz.boot.starter.jdbc.DataSourceStarter
[INFO ] 10:20:39.528 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'druidWebStatFilterStarter               ' - org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter
[INFO ] 10:20:39.528 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'druidWebStatServletStarter              ' - org.nutz.boot.starter.jdbc.DruidWebStatServletStarter
[INFO ] 10:20:39.529 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'tomcatStarter                           ' - org.nutz.boot.starter.tomcat.TomcatStarter
[INFO ] 10:20:39.538 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:103) -    > add 'nbServletContextListener                ' - org.nutz.boot.starter.servlet3.NbServletContextListener
[INFO ] 10:20:39.557 org.nutz.boot.NbApp.prepare(NbApp.java:310) - Configure Manual:
|id  |key                                     |required  |Possible Values     |Default   |Description         |                                starters|
|----|----------------------------------------|----------|--------------------|----------|--------------------|----------------------------------------|
|0   |druid.web.filter.exclusions             |no        |                    |          |需要排除的路径             |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|1   |druid.web.filter.principalCookieName    |no        |                    |          |用户权限信息的cookie属性名称   |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|2   |druid.web.filter.principalSessionName   |no        |                    |          |用户权限信息的session属性名称  |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|3   |druid.web.filter.profileEnable          |no        |                    |          |是否开启性能监控            |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|4   |druid.web.filter.realIpHeader           |no        |                    |          |Header中ReadIp对应的key |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|5   |druid.web.filter.sessionStatEnable      |no        |                    |true      |是否开启session状态监控     |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|6   |druid.web.filter.sessionStatMaxCount    |no        |                    |          |session最大状态数量       |org.nutz.boot.starter.jdbc.DruidWebStatFilterStarter|
|7   |druid.web.servlet.allow                 |no        |                    |          |允许访问的ip列表           |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|8   |druid.web.servlet.deny                  |no        |                    |          |禁止访问的ip列表           |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|9   |druid.web.servlet.enable                |no        |                    |true      |是否启动monitor页面       |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|10  |druid.web.servlet.jmxPassword           |no        |                    |          |JMX的密码              |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|11  |druid.web.servlet.jmxUrl                |no        |                    |          |读取JMX信息的URL         |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|12  |druid.web.servlet.jmxUsername           |no        |                    |          |JMX的用户名             |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|13  |druid.web.servlet.loginPassword         |no        |                    |随机值,打印在日志中|访问monitor页面的密码      |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|14  |druid.web.servlet.loginUsername         |no        |                    |driud     |访问monitor页面的用户名     |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|15  |druid.web.servlet.resetEnable           |no        |                    |true      |是否允许重置统计结果          |org.nutz.boot.starter.jdbc.DruidWebStatServletStarter|
|16  |jdbc.password                           |no        |                    |          |数据库密码               |org.nutz.boot.starter.jdbc.DataSourceStarter|
|17  |jdbc.type                               |no        |druid,simple,hikari |druid     |连接池类型               |org.nutz.boot.starter.jdbc.DataSourceStarter|
|18  |jdbc.url                                |yes       |                    |          |JDBC URL            |org.nutz.boot.starter.jdbc.DataSourceStarter|
|19  |jdbc.username                           |no        |                    |          |数据库用户名              |org.nutz.boot.starter.jdbc.DataSourceStarter|
|20  |nutz.dao.interceptor.cache.cache4Null   |no        |                    |true      |是否缓存null结果          |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|21  |nutz.dao.interceptor.cache.debug        |no        |                    |false     |打印daocache详细调试日志    |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|22  |nutz.dao.interceptor.cache.enable       |no        |                    |false     |是否使用daocache        |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|23  |nutz.dao.interceptor.cache.enableWhenTrans|no        |                    |false     |事务内是否启用daocache     |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|24  |nutz.dao.interceptor.cache.provider.ioc.name|no        |                    |daoCacheProvider|daocache提供者DaoCacheProvider的IocBean名称|org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|25  |nutz.dao.interceptor.cache.provider.memory.cacheSize|no        |                    |          |daocache提供者MemoryDaoCacheProvider的默认缓存大小|org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|26  |nutz.dao.interceptor.cache.provider.type|no        |memory,ehcache,jedis,ioc|memory    |daocache提供者         |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|27  |nutz.dao.interceptor.cache.table.names  |no        |                    |          |需要缓存的表名称,英文逗号分隔     |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|28  |nutz.dao.interceptor.cache.table.pattern|no        |                    |          |需要缓存的表名称的正则表达式      |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|29  |nutz.dao.interceptor.log.enable         |no        |                    |true      |是否打印dao的SQL日志       |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|30  |nutz.dao.interceptor.time.enable        |no        |                    |false     |是否打印dao的SQL耗时日志     |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|31  |nutz.dao.sqls.path                      |no        |                    |sqls/     |sql目录               |org.nutz.boot.starter.nutz.dao.NutDaoStarter|
|32  |nutz.mvc.exclusions                     |no        |                    |          |排除指定请求路径的正则表达式      |org.nutz.boot.starter.nutz.mvc.NutFilterStarter|
|33  |nutz.mvc.ignore                         |no        |                    |          |过滤指定请求路径的正则表达式      |org.nutz.boot.starter.nutz.mvc.NutFilterStarter|
|34  |nutz.mvc.whale.enc.input                |no        |                    |UTF-8     |在其他Filter之前设置input编码|org.nutz.boot.starter.nutz.mvc.WhaleFilterStarter|
|35  |nutz.mvc.whale.enc.output               |no        |                    |UTF-8     |在其他Filter之前设置output编码|org.nutz.boot.starter.nutz.mvc.WhaleFilterStarter|
|36  |nutz.mvc.whale.http.hidden_method_param |no        |                    |          |隐形http方法参数转换所对应的参数名 |org.nutz.boot.starter.nutz.mvc.WhaleFilterStarter|
|37  |nutz.mvc.whale.http.method_override     |no        |                    |false     |是否允许使用X-HTTP-Method-Override|org.nutz.boot.starter.nutz.mvc.WhaleFilterStarter|
|38  |nutz.mvc.whale.upload.enable            |no        |                    |false     |是否启用隐形Upload支持      |org.nutz.boot.starter.nutz.mvc.WhaleFilterStarter|
|39  |swagger.enable                          |no        |                    |true      |是否启用swagger         |org.nutz.boot.starter.swagger.SwaggerServletStarter|
|40  |tomcat.connector.*                      |no        |                    |          |自定义Connector配置群     |org.nutz.boot.starter.tomcat.TomcatStarter|
|41  |tomcat.contextPath                      |no        |                    |          |上下文路径               |org.nutz.boot.starter.tomcat.TomcatStarter|
|42  |tomcat.executor.maxThreads              |no        |                    |256       |最大线程数               |org.nutz.boot.starter.tomcat.TomcatStarter|
|43  |tomcat.host                             |no        |                    |0.0.0.0   |监听的ip地址             |org.nutz.boot.starter.tomcat.TomcatStarter|
|44  |tomcat.maxPostSize                      |no        |                    |64 * 1024 * 1024|POST表单最大尺寸          |org.nutz.boot.starter.tomcat.TomcatStarter|
|45  |tomcat.page.404                         |no        |                    |          |自定义404页面,同理,其他状态码也是支持的|org.nutz.boot.starter.tomcat.TomcatStarter|
|46  |tomcat.page.java.lang.Throwable         |no        |                    |          |自定义java.lang.Throwable页面,同理,其他异常也支持|org.nutz.boot.starter.tomcat.TomcatStarter|
|47  |tomcat.port                             |no        |                    |8080      |监听的端口               |org.nutz.boot.starter.tomcat.TomcatStarter|
|48  |tomcat.staticPath                       |no        |                    |static    |静态文件路径              |org.nutz.boot.starter.tomcat.TomcatStarter|
|49  |tomcat.staticPathLocal                  |no        |                    |          |本地静态文件路径            |org.nutz.boot.starter.tomcat.TomcatStarter|
|50  |tomcat.welcome_files                    |no        |                    |index.html,index.htm,index.do|WelcomeFile列表       |org.nutz.boot.starter.tomcat.TomcatStarter|
|51  |web.filter.order.nutz                   |no        |                    |          |指定NutFilter执行顺序     |org.nutz.boot.starter.nutz.mvc.NutFilterStarter|
|52  |web.session.timeout                     |no        |                    |30        |Session空闲时间,单位分钟    |org.nutz.boot.starter.tomcat.TomcatStarter|
[INFO ] 10:20:39.562 org.nutz.ioc.impl.NutIoc.<init>(NutIoc.java:130) - ... NutIoc init complete
[INFO ] 10:20:40.316 org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:566) - Initializing ProtocolHandler ["http-nio-8088"]
[INFO ] 10:20:40.339 org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector(NioSelectorPool.java:67) - Using a shared selector for servlet write/read
[INFO ] 10:20:40.349 org.apache.catalina.core.StandardService.startInternal(StandardService.java:416) - Starting service [Tomcat]
[INFO ] 10:20:40.349 org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:255) - Starting Servlet Engine: Apache Tomcat/8.5.51
[INFO ] 10:20:40.523 org.nutz.boot.starter.jdbc.DruidWebStatServletStarter.getInitParameters(DruidWebStatServletStarter.java:74) - druid stat view random user=druid password=qc50kouoreg5srkd1s6vcka0ct
[INFO ] 10:20:40.569 org.nutz.mvc.NutFilter._init(NutFilter.java:85) - NutFilter[nutz] starting ...
[INFO ] 10:20:40.573 org.nutz.resource.impl.ErrorResourceLocation.<init>(ErrorResourceLocation.java:28) - [loc=C:\Users\zhangwei\git\dataflow\target\classes\static\WEB-INF\classes]not exist
[INFO ] 10:20:40.582 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:55) - Nutz Version : 1.r.68.v20200309 
[INFO ] 10:20:40.583 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:56) - Nutz.Mvc[nutz] is initializing ...
[INFO ] 10:20:40.599 org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:159) - Build URL mapping by org.nutz.mvc.impl.UrlMappingImpl ...
[INFO ] 10:20:40.668 org.nutz.mvc.impl.NutActionChainMaker.getProcessorByName(NutActionChainMaker.java:72) - Optional processor class not found, disabled : org.nutz.integration.shiro.NutShiroProcessor
[INFO ] 10:20:40.680 org.nutz.mvc.impl.NutActionChainMaker.getProcessorByName(NutActionChainMaker.java:72) - Optional processor class not found, disabled : org.nutz.plugins.validation.ValidationProcessor
[INFO ] 10:20:40.744 org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:221) - Found 13 module methods
[INFO ] 10:20:40.749 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:141) - Nutz.Mvc[nutz] is up in 166ms
[INFO ] 10:20:40.750 org.nutz.mvc.NutFilter._init(NutFilter.java:117) - exclusionsPrefix  = ^(/druid/)
[INFO ] 10:20:40.750 org.nutz.mvc.NutFilter._init(NutFilter.java:125) - exclusionsPath   = [/swagger/swagger.json]
[INFO ] 10:20:41.112 org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:594) - Starting ProtocolHandler ["http-nio-8088"]
[INFO ] 10:20:41.268 org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:103) - Get Connection from DataSource for JdbcExpert, if you lock at here, check your database server and configure
[INFO ] 10:20:41.305 com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:1010) - {dataSource-1} inited
Thu Aug 27 10:20:41 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[INFO ] 10:20:41.585 org.nutz.boot.NbApp.execute(NbApp.java:219) - NB started : 2573ms

代码:

//ServerEndpoint是websocket的必备注解, value是映射路径, configurator是配置类.
@ServerEndpoint(value = "/ws/wsh", configurator=NutWsConfigurator.class)
@IocBean // 使用NutWsConfigurator的必备条件
public class MyWebsocket extends AbstractWsEndpoint {
 // 并不需要你马上实现任何方法,它也马上能工作
	
	public WsHandler createHandler(Session session, EndpointConfig config) {
//        return new MySimpleWsHandler(); // 是的,返回你自己的实现类就可以了,需要每次新建哦
	     return new WebWsHandler(); // 是的,返回你自己的实现类就可以了,需要每次新建哦
    }
}
6 回复

自带支持,不需要手工添加依赖

您是说把nutz-plugins-websocket依赖去掉吧,我试过连ws还是404,jdk是1.8的,还有哪里的问题?

知道啥原因了,没有注入

tomcat的方式有bug啊,

case1: 采用jetty的方式(注释掉nutzboot-starter-tomcat依赖),websocket功能正常:

jetty.port=8088
jetty.host=0.0.0.0
jetty.contextPath=/xxx

case2:采用tomcat的方式,注释掉nutzboot-starter-jetty, websocket异常(404)

tomcat.port=8088
tomcat.host=0.0.0.0
tomcat.contextPath=/xxx

<nutzboot.version>2.4.0.v20200427</nutzboot.version>


javax.websocket
javax.websocket-api
1.1
provided

我昨天说成功,其实是jetty方式, tomcat一直没有成功过

case3:采用Undertow的方式, websocket正常

undertow.port=8088
undertow.host=0.0.0.0
undertow.contextPath=/xxx

再还原成tomcat方式,还是异常, 看来很可能是tomcat方式有bug

添加回复
请先登陆
回到顶部