之前我们用tomcat源码跑起来了,那么如何加入我们的应用呢?springboot内嵌了tomcat,怎么剥离?如果项目里有websocket配置,又有哪些注意项?好,我们看一下操作。
1、要剥离springboot的内嵌tomcat,先修改pom.xml:
1.1、新增
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <scope>provided</scope> </dependency>
1.2、修改:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
如果原来的打jar包的话,记得改成war包:
<packaging>war</packaging>
还有别忘记配置springboot的maven插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.wlf.DemoApplication</mainClass> </configuration> </plugin> </plugins> </build>
2、修改启动类:
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
3、修改websocket配置类:
// WebSocketMessageBrokerConfigurer 是用来处理 STOMP协议 的 //@Configuration //@EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { // @Override // public void configureMessageBroker(MessageBrokerRegistry config) { // config.enableSimpleBroker("/ws"); // } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/subtitleConnect"). setAllowedOrigins("*"); } //这个是处理基本的 websocket 连接的 // @Bean // public ServerEndpointExporter serverEndpointExporter() { // return new ServerEndpointExporter(); // } }
这样就可以跑maven打war包了。接下来我们把这个war包扔到tomcat源码的webapps目录下:
把tomcat跑起来,它会自动加压war包的。跑起来后我们跑一个试试:
日志如下:
六月 30, 2020 7:42:27 下午 org.apache.coyote.AbstractProtocol init 信息: åå§ååè®®å¤çå¨ ["http-nio-8080"] 六月 30, 2020 7:42:28 下午 org.apache.coyote.AbstractProtocol init 信息: åå§ååè®®å¤çå¨ ["ajp-nio-8009"] 六月 30, 2020 7:42:28 下午 org.apache.catalina.startup.Catalina load 信息: æå¡å¨å¨[1,539]毫ç§å åå§å 六月 30, 2020 7:42:28 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service [Catalina] 六月 30, 2020 7:42:28 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet engine: [Apache Tomcat/9.0.x-dev] 六月 30, 2020 7:42:28 下午 org.apache.catalina.startup.HostConfig deployWAR 信息: Deploying web application archive [E:workspace omcatwebappssubtitle-1.0-SNAPSHOT.war] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/E:/workspace/tomcat/webapps/subtitle-1.0-SNAPSHOT/WEB-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/wulf/tomcat-build-libs/bnd-4.0.0/biz.aQute.bnd-4.0.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 六月 30, 2020 7:42:38 下午 org.apache.catalina.core.ApplicationContext log 信息: 2 Spring WebApplicationInitializers detected on classpath . ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.6.RELEASE) 2020-06-30 19:42:40 [main] INFO com.lxy.DemoApplication - Starting DemoApplication v1.0-SNAPSHOT on wulf00 with PID 60796 (E:workspace omcatwebappssubtitle-1.0-SNAPSHOTWEB-INFclasses started by wulf in E:workspace omcat) 2020-06-30 19:42:40 [main] INFO com.lxy.DemoApplication - No active profile set, falling back to default profiles: default 2020-06-30 19:42:42 [main] INFO o.a.c.c.C.[.[localhost].[/subtitle-1.0-SNAPSHOT] - Initializing Spring embedded WebApplicationContext 2020-06-30 19:42:42 [main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1639 ms 2020-06-30 19:42:43 [main] INFO o.s.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor' 2020-06-30 19:42:43 [main] INFO com.lxy.DemoApplication - Started DemoApplication in 4.197 seconds (JVM running for 17.181) 2020-06-30 19:42:43 [main] INFO org.apache.jasper.servlet.TldScanner - è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªå å«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å ¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã 2020-06-30 19:42:43 [main] INFO org.apache.catalina.startup.HostConfig - Deployment of web application archive [E:workspace omcatwebappssubtitle-1.0-SNAPSHOT.war] has finished in [15,423] ms 2020-06-30 19:42:43 [main] INFO org.apache.catalina.startup.HostConfig - æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:workspace omcatwebappsdocs] 2020-06-30 19:42:47 [main] INFO org.apache.jasper.servlet.TldScanner - è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªå å«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å ¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã 2020-06-30 19:42:47 [main] INFO org.apache.catalina.startup.HostConfig - Deployment of web application directory [E:workspace omcatwebappsdocs] has finished in [3,673] ms 2020-06-30 19:42:47 [main] INFO org.apache.catalina.startup.HostConfig - æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:workspace omcatwebappsexamples] 2020-06-30 19:42:51 [main] INFO org.apache.jasper.servlet.TldScanner - è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªå å«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å ¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã 2020-06-30 19:42:51 [main] INFO o.a.c.c.C.[Catalina].[localhost].[/examples] - ContextListener: contextInitialized() 2020-06-30 19:42:51 [main] INFO o.a.c.c.C.[Catalina].[localhost].[/examples] - SessionListener: contextInitialized() 2020-06-30 19:42:51 [main] INFO o.a.c.c.C.[Catalina].[localhost].[/examples] - ContextListener: attributeAdded('StockTicker', 'async.Stockticker@69e9cbc2') 2020-06-30 19:42:51 [main] INFO org.apache.catalina.startup.HostConfig - Deployment of web application directory [E:workspace omcatwebappsexamples] has finished in [4,329] ms 2020-06-30 19:42:51 [main] INFO org.apache.catalina.startup.HostConfig - æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:workspace omcatwebappshost-manager] 2020-06-30 19:42:55 [main] INFO org.apache.jasper.servlet.TldScanner - è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªå å«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å ¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã 2020-06-30 19:42:55 [main] INFO org.apache.catalina.startup.HostConfig - Deployment of web application directory [E:workspace omcatwebappshost-manager] has finished in [3,719] ms 2020-06-30 19:42:55 [main] INFO org.apache.catalina.startup.HostConfig - æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:workspace omcatwebappsmanager] 2020-06-30 19:42:59 [main] INFO org.apache.jasper.servlet.TldScanner - è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªå å«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å ¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã 2020-06-30 19:42:59 [main] INFO org.apache.catalina.startup.HostConfig - Deployment of web application directory [E:workspace omcatwebappsmanager] has finished in [3,625] ms 2020-06-30 19:42:59 [main] INFO org.apache.catalina.startup.HostConfig - æweb åºç¨ç¨åºé¨ç½²å°ç®å½ [E:workspace omcatwebappsROOT] 2020-06-30 19:43:02 [main] INFO org.apache.jasper.servlet.TldScanner - è³å°æä¸ä¸ªJAR被æ«æç¨äºTLDä½å°æªå å«TLDã 为æ¤è®°å½å¨å¯ç¨è°è¯æ¥å¿è®°å½ï¼ä»¥è·åå·²æ«æä½æªå¨å ¶ä¸æ¾å°TLDçå®æ´JARå表ã å¨æ«ææé´è·³è¿ä¸éè¦çJARå¯ä»¥ç¼©çå¯å¨æ¶é´åJSPç¼è¯æ¶é´ã 2020-06-30 19:43:02 [main] INFO org.apache.catalina.startup.HostConfig - Deployment of web application directory [E:workspace omcatwebappsROOT] has finished in [3,576] ms 2020-06-30 19:43:02 [main] INFO org.apache.coyote.http11.Http11NioProtocol - å¼å§åè®®å¤çå¥æ["http-nio-8080"] 2020-06-30 19:43:02 [main] INFO org.apache.coyote.ajp.AjpNioProtocol - å¼å§åè®®å¤çå¥æ["ajp-nio-8009"] 2020-06-30 19:43:02 [main] INFO org.apache.catalina.startup.Catalina - Server startup in [34,471] milliseconds 2020-06-30 19:46:11 [http-nio-8080-exec-5] INFO o.a.c.c.C.[.[localhost].[/subtitle-1.0-SNAPSHOT] - Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-06-30 19:46:11 [http-nio-8080-exec-5] INFO org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet' 2020-06-30 19:46:11 [http-nio-8080-exec-5] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 22 ms 2020-06-30 19:46:12 [http-nio-8080-exec-5] INFO com.lxy.controller.LiveController - liveSync info : LiveConfig(cutNumberForm=0, cutNumberTo=0, To=2, from=1, senseText=@, senseScreen=0, hotText=null, modalFilter=0, liveUrl=rtmp://58.200.131.2:1935/livetv/hunantv, timestamp=2000000)