SpringBoot配置tomcat
在Spring Boot中,Tomcat是默认嵌入式Servlet容器。如果需要配置Tomcat,可以在application.properties
或application.yml
文件中设置相关属性,或者通过编程的方式进行配置。
以下是一些常见的Tomcat配置示例:
- 修改
application.properties
:
server.tomcat.max-threads=200
server.tomcat.uri-encoding=UTF-8
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.pattern=%h %t "%r" %s %b %D
- 修改
application.yml
:
server:
tomcat:
max-threads: 200
uri-encoding: UTF-8
accesslog:
enabled: true
directory: logs
pattern: "%h %t \"%r\" %s %b %D"
- 编程方式配置(在配置类中):
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TomcatConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
return (tomcatServletWebServerFactory) -> {
tomcatServletWebServerFactory.setPort(8080);
tomcatServletWebServerFactory.setUriEncoding(Charset.forName("UTF-8"));
tomcatServletWebServerFactory.addContextValves(getAccessLogValve());
};
}
private AccessLogValve getAccessLogValve() {
AccessLogValve logValve = new AccessLogValve();
logValve.setDirectory("logs");
logValve.setEnabled(true);
logValve.setPattern("%h %t \"%r\" %s %b %D");
logValve.setPrefix("access_log.");
logValve.setSuffix(".txt");
return logValve;
}
}
请注意,编程配置需要你在配置类路径下添加Apache Tomcat相关依赖,并且AccessLogValve
是Apache Tomcat特有的类,不是Spring Boot的标准库。
评论已关闭