在本文中,我们将详细介绍SpringBoot如何控制Tomcat缓存?的各个方面,并为您提供关于springboot设置tomcat内存的相关解答,同时,我们也将为您带来关于#SpringBoot|怎
在本文中,我们将详细介绍Spring Boot如何控制Tomcat缓存?的各个方面,并为您提供关于springboot设置tomcat内存的相关解答,同时,我们也将为您带来关于# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。、spring boot 2 内嵌Tomcat Stopping service [Tomcat]、spring boot 2 内嵌Tomcat 抛出异常 “Stopping service [Tomcat]”、Spring Boot tomcat的有用知识。
本文目录一览:- Spring Boot如何控制Tomcat缓存?(springboot设置tomcat内存)
- # SpringBoot | 怎样启动tomcat以及怎样配置tomcat。
- spring boot 2 内嵌Tomcat Stopping service [Tomcat]
- spring boot 2 内嵌Tomcat 抛出异常 “Stopping service [Tomcat]”
- Spring Boot tomcat
Spring Boot如何控制Tomcat缓存?(springboot设置tomcat内存)
我正在将具有5年历史的带有JSP的Spring MVC应用程序移植到Spring
Boot。因此,根据http://docs.spring.io/spring-boot/docs/current-
SNAPSHOT/reference/htmlsingle/#boot-features-jsp-
limitations中的示例,我使用的是“战争”包装。
嵌入式tomcat启动。但是,日志中充满了缓存警告,如以下示例所示
2016-08-25 14:59:01.442 INFO 28884 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2016-08-25 14:59:01.456 INFO 28884 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2016-08-25 14:59:01.458 INFO 28884 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.42016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/displaytag-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/decrex-maven-0.1.10-SNAPSHOT.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/spring-boot-actuator-1.4.0.RELEASE.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/validation-api-1.1.0.Final.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:01.532 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/lucene-backward-codecs-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:01.532 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/lucene-queries-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache.....2016-08-25 14:59:05.121 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/jstl-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:05.139 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/classes/commons-logging.properties] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache2016-08-25 14:59:05.139 INFO 28884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2016-08-25 14:59:05.139 INFO 28884 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 7117 ms.....2016-08-25 15:02:03.960 INFO 28884 --- [ndardContext[]]] org.apache.catalina.webresources.Cache : The background cache eviction process was unable to free [10] percent of the cache for Context [] - consider increasing the maximum size of the cache. After eviction approximately [9,251] KB of data remained in the cache.
我很乐意增加tomcat缓存,但是我没有找到在Spring Boot中控制它的方法。请 指教!!!
我在下面尝试了安迪·威尔金森的建议。也尝试在EmbeddedServletContainerCustomizer
@Componentpublic class ServletContainerCustomizer implements EmbeddedServletContainerCustomizer { private static final Log log = LogFactory.getLog(ServletContainerCustomizer.class); @Override public void customize(ConfigurableEmbeddedServletContainer container) { if (TomcatEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) { int cacheSize = 256 * 1024; log.info("Customizing tomcat factory. New cache size (KB) is " + cacheSize); TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) container; tomcatFactory.addContextCustomizers((context) -> { StandardRoot standardRoot = new StandardRoot(context); standardRoot.setCacheMaxSize(cacheSize); }); } }}
有关更改高速缓存大小的消息在日志中,但是 上面 的 代码 对警告 没有影响
答案1
小编典典我有一段时间一直遇到相同的问题,但是安迪·威尔金森(Andy
Wilkinson)的提示使我走上了正确的轨道。对我有用的是像Andy一样设置缓存,但同时还要在上下文中显式设置资源。
@Beanpublic EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { final int cacheSize = 40 * 1024; StandardRoot standardRoot = new StandardRoot(context); standardRoot.setCacheMaxSize(cacheSize); context.setResources(standardRoot); // This is what made it work in my case. logger.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize())); } }; return tomcatFactory;}
希望这可以帮助!
# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。
SpringBoot | 怎样启动tomcat以及默认的tomcat配置。
标签(空格分隔): springboot tomcat
因为springboot已经被大部分公司运用,所以基于springboot 来讲解tomcat。
- springboot 怎样引入的tomcat
- springboot 怎样创建一个tomcat实例
- springboot 从哪里读取tomcat配置
- springboot 中tomcat的配置详解
本文主要讲解这三个问题
springboot 怎样引入的tomcat
当我们在项目中加入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
加入这个依赖后,maven会把tomcat的一些jar也加入进来。 这些jar包应该是一些开源工作者在维护着。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.31</version>
</dependency>
上面这个依赖是springboot为我们准备好的,版本号为8.5.31,springboot的版本为:2.0.x。 这个依赖包含了tomcat的核心jar包。springboot 初始化tomcat,启动tomcat都需要这个jar。
当我们 run springboot的引导文件时,springboot 会创建WebServer, 我们跟着这个方法(createWebServer())走下去。
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
这里会去获取一个webServier,这个方法里面很多引用的类都存在于上面提到的jar包中。 new Tomcat() 创建一个tomcat 实例。到这里我们已经看到springboot已经创建了一个tomcat实例。
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat"));
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatWebServer(tomcat);
}
当我们追踪到里面的类时,发现里面已经配置好了端口以及一些其他配置。 这样已经能创建一个默认的tomcat。 我们需要知道的是springboot是怎样吧appplication.property中的配置运用到tomcat实例中的。
探果网
spring boot 2 内嵌Tomcat Stopping service [Tomcat]
我在使用springboot时,当代码有问题时,发现控制台打印下面信息:
Connected to the target VM, address: ''127.0.0.1:42091'', transport: ''socket''
log4j:WARN No appenders could be found for logger (org.springframework.boot.devtools.settings.DevToolsSettings). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. . ____ _ __ _ _ /\\ / ___''_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ''_ | ''_| | ''_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) '' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.6.RELEASE) 2018-10-25 10:10:21.425 INFO 102158 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-10-25 10:10:21.427 INFO 102158 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34 2018-10-25 10:10:21.444 INFO 102158 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 2018-10-25 10:10:21.590 INFO 102158 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-10-25 10:10:24.522 INFO 102158 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] Disconnected from the target VM, address: ''127.0.0.1:42091'', transport: ''socket'' Process finished with exit code 0
WTF?没有错误信息怎么解决问题? 各种搜索,总之就是代码有问题,自己检查把...
好吧,直接debug把
内嵌tomcat的入口类是org.apache.catalina.core.StandardService
//TODO 后面补上过程
最终找到org.springframework.context.support.AbstractApplicationContext
定位方法refresh()
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
debug可以正常进入,然后就看到我们希望看到的 ex了
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''fabricApiController'': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''fabricTemplate'': Injection of resource dependencies failed; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name ''fabricConfiguration'': Could not bind properties to ''FabricConfiguration'' : prefix=blockchain, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under ''blockchain.channel-peers'' to java.util.List<com.smy.bc.fabric.core.configuration.FabricConfiguration$EndPoint>
问题发现了,解决自己代码问题,然后重新启动,正常! 万事大吉?错,这才开始
上面我们简单解决了问题,但是根源没有解决!
要说解决方案把当前流行的日志体系简单说一遍
下面整理的来源网络:
常见的日志框架,注意不是具体解决方案
1 Commons-logging: apache 最早提供的日志的门面接口。避免和具体的日志方案直接耦合。类似于JDBC的api接口,具体的的JDBC driver实现由各数据库提供商实现。通过统一接口解耦,不过其内部也实现了一些简单日志方案
2 Slf4j: 全称为Simple Logging Facade for JAVA:java简单日志门面。是对不同日志框架提供的一个门面封装。可以在部署的时候不修改任何配置即可接入一种日志实现方案。和commons-loging应该有一样的初衷。
常见的日志实现:
log4j
logback
jdk-logging
详细优缺点不是本文重点,请自行搜索。
接着分析上面的问题,Commons-logging 是tomcat默认的日志系统(apache自家东西得支持),具体的日志实现,根据系统已存在日志系统选择。 简单列举以下log的实现: org.apache.commons.logging.Log | org.apache.commons.logging.impl.SimpleLog org.apache.commons.logging.impl.NoOpLog org.apache.commons.logging.impl.Log4JLogger org.apache.commons.logging.impl.SLF4JLog org.apache.commons.logging.impl.Jdk14Logger
springboot 默认使用的是logback日志实现,问题就出现在这里了!!!common-logs并没有logback的实现!
根据maven依赖,我们看到log4j和logback的包都被引入了,然后tomcat之能选择的是log4j,springboot使用的是logback。 log4j和logback只见缺少一个桥梁,正是缺少的这个桥梁,导致springboot只能输出logback!!!
中间的桥梁就是下面这个依赖
<dependency>
<groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency>
这个依赖可以将log4j输出到slf4j,从而从sl4j输出。
总结: 总结一下,已经搞明白是slf4j/common-logs <> log4j和logback的恩怨情仇
第一种解决方式:根据日志定位问题,然后采用加法处理,增加jcl-over-slf4j,打通slf4j和common-logs通道
第二种解决方式:解决冲突,一山不容二虎,排除掉slf4j,common-logs任意一方,spring使用slf4j,那可以排除调common-logs
从项目优化的角度看,第二种更优,可以减少不必要的依赖。
如果日志出现问题,那就是日志体系发生冲突了,可以参考这个思路,处理项目中日志异常问题
spring boot 2 内嵌Tomcat 抛出异常 “Stopping service [Tomcat]”
我在使用springboot时,当代码有问题时,发现控制台打印下面信息:
Connected to the target VM, address: ''127.0.0.1:42091'', transport: ''socket''
log4j:WARN No appenders could be found for logger (org.springframework.boot.devtools.settings.DevToolsSettings).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
. ____ _ __ _ _
/\\ / ___''_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ''_ | ''_| | ''_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
'' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.6.RELEASE)
2018-10-25 10:10:21.425 INFO 102158 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-10-25 10:10:21.427 INFO 102158 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-10-25 10:10:21.444 INFO 102158 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-10-25 10:10:21.590 INFO 102158 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-10-25 10:10:24.522 INFO 102158 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
Disconnected from the target VM, address: ''127.0.0.1:42091'', transport: ''socket''
Process finished with exit code 0
WTF?没有错误信息怎么解决问题? 各种搜索,总之就是代码有问题,自己检查把...
好吧,直接debug把
内嵌tomcat的入口类是org.apache.catalina.core.StandardService
//TODO 后面补上过程
最终找到org.springframework.context.support.AbstractApplicationContext
定位方法refresh()
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
debug可以正常进入,然后就看到我们希望看到的 ex了
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''fabricApiController'': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''fabricTemplate'': Injection of resource dependencies failed; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name ''fabricConfiguration'': Could not bind properties to ''FabricConfiguration'' : prefix=blockchain, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under ''blockchain.channel-peers'' to java.util.List<com.smy.bc.fabric.core.configuration.FabricConfiguration$EndPoint>
问题发现了,解决自己代码问题,然后重新启动,正常! 万事大吉?错,这才开始
上面我们简单解决了问题,但是根源没有解决!
要说解决方案把当前流行的日志体系简单说一遍
下面整理的来源网络:
常见的日志框架,注意不是具体解决方案
1 Commons-logging: apache 最早提供的日志的门面接口。避免和具体的日志方案直接耦合。类似于JDBC的api接口,具体的的JDBC driver实现由各数据库提供商实现。通过统一接口解耦,不过其内部也实现了一些简单日志方案
2 Slf4j: 全称为Simple Logging Facade for JAVA:java简单日志门面。是对不同日志框架提供的一个门面封装。可以在部署的时候不修改任何配置即可接入一种日志实现方案。和commons-loging应该有一样的初衷。
常见的日志实现:
log4j
logback
jdk-logging
详细优缺点不是本文重点,请自行搜索。
接着分析上面的问题,Commons-logging 是tomcat默认的日志系统(apache自家东西得支持),具体的日志实现,根据系统已存在日志系统选择。 简单列举以下log的实现: org.apache.commons.logging.Log | org.apache.commons.logging.impl.SimpleLog org.apache.commons.logging.impl.NoOpLog org.apache.commons.logging.impl.Log4JLogger org.apache.commons.logging.impl.SLF4JLog org.apache.commons.logging.impl.Jdk14Logger
springboot 默认使用的是logback日志实现,问题就出现在这里了!!!common-logs并没有logback的实现!
根据maven依赖,我们看到log4j和logback的包都被引入了,然后tomcat之能选择的是log4j,springboot使用的是logback。 log4j和logback只见缺少一个桥梁,正是缺少的这个桥梁,导致springboot只能输出logback!!!
中间的桥梁就是下面这个依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
这个依赖可以将log4j输出到slf4j,从而从sl4j输出。
总结: 总结一下,已经搞明白是slf4j/common-logs <> log4j和logback的恩怨情仇
第一种解决方式:根据日志定位问题,然后采用加法处理,增加jcl-over-slf4j,打通slf4j和common-logs通道
第二种解决方式:解决冲突,一山不容二虎,排除掉slf4j,common-logs任意一方,spring使用slf4j,那可以排除调common-logs
从项目优化的角度看,第二种更优,可以减少不必要的依赖。
如果日志出现问题,那就是日志体系发生冲突了,可以参考这个思路,处理项目中日志异常问题。
Spring Boot tomcat
定制内嵌 Tomcat
设置内嵌 Tomcat 的端口
Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口。如果,我们需要修改 Tomcat 的端口,我们可以在 src/main/resources/application.properties 中配置 Tomcat 信息。
server.port=8089
设置内嵌 Tomcat 的最大线程数
我们还可以修改内嵌的 Tomcat 服务器的最大线程数。
server.tomcat.max-threads=1000
设置内嵌 Tomcat 的编码
在一些场景下,我们可能需要改动 Tomcat 的编码,例如是 GBK 还是 UTF-8。
server.tomcat.uri-encoding = UTF-8
官方提供的常用配置参数
除了上面讲到的 3 个场景外,我们还可以自定义设置路径地址、 SSL 等参数。
这里列举一些官方提供的常用配置参数,如果有特定需求,可以进行内嵌 Tomcat 的定制。
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods.
server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses.
server.tomcat.max-threads=0 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=0 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
使用 war 包部署
如果我们希望通过 war 包的方式,部署到外部的 Tomcat 服务器上, Spring Boot 也是支持的,不过需要一些额外的配置。
首先,要将最终的打包形式改为 war 包,所以需要将 packaging 的值修改为 war。
<packaging>war</packaging>
接着,对依赖进行适当的配置,值得注意的是,在这里需要移除对嵌入的 Tomcat 的依赖,这样打出的 WAR 包中,在 lib 目录下才不会包含 Tomcat 相关的 JAR 包。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--移除对嵌入的Tomcat的依赖-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--额外添加tomcat的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
另外,为了保证编译正确,还需要添加对 servlet-api 的依赖,因此添加如下的配置。
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
设置,打包后的项目访问名称,在 <build> 节点里添加 < finalName > 内容。
<build>
<finalName>springboot-web-demo</finalName>
</bulid>
修改项目启动类,继承 SpringBootServletInitializer,如下:
package com.winner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* @author winner_0715
* @date 2018/12/07
*/
@SpringBootApplication
public class SpringBootServerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootServerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootServerApplication.class);
}
}
大功告成,此时,我们可以通过 Maven 的 "mvn clean package" 打包出一个 war 包,并部署到外部的 Tomcat 服务器运行。
总结
Spring Boot 默认使用的是 Tomcat 作为内嵌的服务器。所以,我们搭建一个 Web 工程将会变得非常的简单,只需要一个 jar 包即可运行。此外,我们还可以对内嵌的 Tomcat 进行一些定制,例如端口、最大线程数、编码、 SSL 等。如果,我们还是希望通过 war 包的方式,部署到外部的 Tomcat 服务器上, Spring Boot 也是支持的,不过需要一些额外的配置,这个配置过程也只需要几个简单的步骤即可实现。
关于Spring Boot如何控制Tomcat缓存?和springboot设置tomcat内存的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。、spring boot 2 内嵌Tomcat Stopping service [Tomcat]、spring boot 2 内嵌Tomcat 抛出异常 “Stopping service [Tomcat]”、Spring Boot tomcat的相关知识,请在本站寻找。
本文标签: