SpringBoot自有Tomcat不同方式配置

来源:互联网 发布:自动化设备编程 编辑:程序博客网 时间:2024/05/17 03:43
在之前的章节中已经有设置tomcat的相关配置说明,本章节重点讲解下3种tomcat的设置,每个方式还是有一定的差异化。

1、直接在application.properties配置,下面的内容来源于官网说明(Part X. Appendices):
server.address=# Network address to which the server should bind to.server.compression.enabled=false# If response compression is enabled.server.compression.excluded-user-agents=# List of user-agents to exclude from compression.server.compression.mime-types=# Comma-separated list of MIME types that should be compressed. For instance `text/html,text/css,application/json`server.compression.min-response-size=# Minimum response size that is required for compression to be performed. For instance 2048server.connection-timeout=# Time in milliseconds that connectors will wait for another HTTP request before closing the connection. When not set, the connector's container-specific default will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.server.context-parameters.*=# Servlet context init parameters. For instance `server.context-parameters.a=alpha`server.context-path=# Context path of the application.server.display-name=application# Display name of the application.server.max-http-header-size=0# Maximum size in bytes of the HTTP message header.server.max-http-post-size=0# Maximum size in bytes of the HTTP post content.server.error.include-stacktrace=never# When to include a "stacktrace" attribute.server.error.path=/error# Path of the error controller.server.error.whitelabel.enabled=true# Enable the default error page displayed in browsers in case of a server error.server.jetty.acceptors=# Number of acceptor threads to use.server.jetty.selectors=# Number of selector threads to use.server.jsp-servlet.class-name=org.apache.jasper.servlet.JspServlet# The class name of the JSP servlet.server.jsp-servlet.init-parameters.*=# Init parameters used to configure the JSP servletserver.jsp-servlet.registered=true# Whether or not the JSP servlet is registeredserver.port=8080# Server HTTP port.server.server-header=# Value to use for the Server response header (no header is sent if empty)server.servlet-path=/# Path of the main dispatcher servlet.server.use-forward-headers=# If X-Forwarded-* headers should be applied to the HttpRequest.server.session.cookie.comment=# Comment for the session cookie.server.session.cookie.domain=# Domain for the session cookie.server.session.cookie.http-only=# "HttpOnly" flag for the session cookie.server.session.cookie.max-age=# Maximum age of the session cookie in seconds.server.session.cookie.name=# Session cookie name.server.session.cookie.path=# Path of the session cookie.server.session.cookie.secure=# "Secure" flag for the session cookie.server.session.persistent=false# Persist session data between restarts.server.session.store-dir=# Directory used to store session data.server.session.timeout=# Session timeout in seconds.server.session.tracking-modes=# Session tracking modes (one or more of the following: "cookie", "url", "ssl").server.ssl.ciphers=# Supported SSL ciphers.server.ssl.client-auth=# Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store.server.ssl.enabled=# Enable SSL support.server.ssl.enabled-protocols=# Enabled SSL protocols.server.ssl.key-alias=# Alias that identifies the key in the key store.server.ssl.key-password=# Password used to access the key in the key store.server.ssl.key-store=# Path to the key store that holds the SSL certificate (typically a jks file).server.ssl.key-store-password=# Password used to access the key store.server.ssl.key-store-provider=# Provider for the key store.server.ssl.key-store-type=# Type of the key store.server.ssl.protocol=TLS# SSL protocol to use.server.ssl.trust-store=# Trust store that holds SSL certificates.server.ssl.trust-store-password=# Password used to access the trust store.server.ssl.trust-store-provider=# Provider for the trust store.server.ssl.trust-store-type=# Type of the trust store.server.tomcat.accesslog.directory=logs# Directory in which log files are created. Can be relative to the tomcat base dir or absolute.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.

2、上面没有server.tomcat的部分其实是多个容器的通用配置,其实通过java配置同样可以进行通用配置,例如下面的案例:
/**
* 对多个servlet容器通用性配置,tomcat、jetty、undertow
* @return
*/
@Bean
@Profile(value="dev")
public EmbeddedServletContainerCustomizer devEmbeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(Integer.valueOf(serverConfig.getPort()).intValue());
// //如果是静态页面的异常处理,如404错误,此方式不能传递异常具体信息
// //错误页面需要放在Spring Boot web应用的static内容目录下,它的默认位置是:src/main/resources/static,
// container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
// container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"));
}
};
}

3、既然在1中可以设置tomcat的特性属性,那么通过java配置是否也可以呢,答案是肯定的:
package com.shf.springboot.config;

import java.nio.charset.Charset;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Configuration
public class TomcatConfiguare {

/**
* 特定配置,仅配置tomcat
*/
@Bean
@Profile(value="dev")
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory();
tomcat.setUriEncoding(Charset.forName("UTF-8"));
tomcat.setPort(8000);
return tomcat;
}
}

小结:以上3中配置方式有一定的优先级,经过测试,优先级由高到低如下:
第二种EmbeddedServletContainerCustomizer 通用配置---->application.properties中自定义配置----->TomcatEmbeddedServletContainerFactory中的对应容易配置。


附录:为了更友好的配置我们可以对于通用配置添加一个注解:
@Bean
@Profile(value="dev")
@ConditionalOnMissingBean(value={TomcatEmbeddedServletContainerFactory.class})
public EmbeddedServletContainerCustomizer devEmbeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(Integer.valueOf(serverConfig.getPort()).intValue());
// //如果是静态页面的异常处理,如404错误,此方式不能传递异常具体信息
// //错误页面需要放在Spring Boot web应用的static内容目录下,它的默认位置是:src/main/resources/static,
// container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
// container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html"));
// container.setSessionTimeout(30);
}
};
}

0 0