目录

需求分析

全局配置文件修改 Servlet 容器配置

web服务定制器修改 Servlet 容器配置


需求分析

1、SpringBoot 默认使用 Tomcat 作为嵌入式的 Servlet 容器;如下所示,找到应用的 pom.xml 文件,文件中右击选择 “Diagrams(图解)”,可以看到 Spring Boot 2.0.3 版本默认使用了内置的 Tomcat 8.5.31 版本。

2、对于以前外置的 Tomcat 服务器,则可以直接修改其 conf 目录下的所有配置文件,比如默认端口等。

3、Servlet 容器也不只有 Tomcat 服务器,还有 Jetty,Jetty 和 Tomcat 都是目前全球范围内最著名的两款开源的webserver/servlet容器。 Spring Boot 都可以提供配置切换。

3、Spring Boot 采用了内置集成容器的方式,当然就会提供修改配置的方式,主要有两种:

1)一是直接在全局配置文件中进行配置修改(推荐方式)

2)二是自己编写嵌入式的 Servlet 容器定制器,来修改 Servlet 容器的配置。

全局配置文件修改 Servlet 容器配置

1、这种方式好处就是使用简单,其底层也是使用的定制 Servlet 容器定制器,当前程序员自己配置了时,则使用用户配置的,否则使用默认的。

2、修改和 server(服务器) 有关的配置都可以在 ServerProperties 配置属性类中找到,当然也可以从官方文档中查找

3、如下所示为 Spring Boot 2.0.4 版本关于 servlet 容器的所有可修改配置(所有配置项都以 “ server. *”开头):

# ----------------------------------------
# WEB PROPERTIES
# ----------------------------------------# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.address= # Network address to which the server should bind.
server.compression.enabled=false # Whether response compression is enabled.
server.compression.excluded-user-agents= # List of user-agents to exclude from compression.
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript # Comma-separated list of MIME types that should be compressed.
server.compression.min-response-size=2048 # Minimum "Content-Length" value that is required for compression to be performed.
server.connection-timeout= # 连接器在关闭连接之前等待另一个 HTTP 请求的时间,未设置时使用连接器的特定于容器的默认值,-1 表示无限等待。
server.error.include-exception=false # Include the "exception" attribute.
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 # Whether to enable the default error page displayed in browsers in case of a server error.
server.http2.enabled=false # Whether to enable HTTP/2 support, if the current environment supports it.
server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.accesslog.append=false # Append to log.
server.jetty.accesslog.date-format=dd/MMM/yyyy:HH:mm:ss Z # Timestamp format of the request log.
server.jetty.accesslog.enabled=false # Enable access log.
server.jetty.accesslog.extended-format=false # Enable extended NCSA format.
server.jetty.accesslog.file-date-format= # Date format to place in log file name.
server.jetty.accesslog.filename= # Log filename. If not specified, logs redirect to "System.err".
server.jetty.accesslog.locale= # Locale of the request log.
server.jetty.accesslog.log-cookies=false # Enable logging of the request cookies.
server.jetty.accesslog.log-latency=false # Enable logging of request processing time.
server.jetty.accesslog.log-server=false # Enable logging of the request hostname.
server.jetty.accesslog.retention-period=31 # Number of days before rotated log files are deleted.
server.jetty.accesslog.time-zone=GMT # Timezone of the request log.
server.jetty.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post or put content.
server.jetty.selectors= # Number of selector threads to use.
server.max-http-header-size=8KB # HTTP消息头的最大大小(字节),单位 KB、MB、GB、TB
server.port=8080 # Server HTTP port.
server.server-header= # Value to use for the Server response header (if empty, no header is sent).
server.use-forward-headers= # Whether X-Forwarded-* headers should be applied to the HttpRequest.
server.servlet.context-parameters.*= # Servlet context init parameters.
server.servlet.context-path= # Context path of the application.
server.servlet.application-display-name=application # Display name of the application.
server.servlet.jsp.class-name=org.apache.jasper.servlet.JspServlet # The class name of the JSP servlet.
server.servlet.jsp.init-parameters.*= # Init parameters used to configure the JSP servlet.
server.servlet.jsp.registered=true # Whether the JSP servlet is registered.
server.servlet.path=/ # Path of the main dispatcher servlet.
server.servlet.session.cookie.comment= # Comment for the session cookie.
server.servlet.session.cookie.domain= # Domain for the session cookie.
server.servlet.session.cookie.http-only= # "HttpOnly" flag for the session cookie.
server.servlet.session.cookie.max-age= # Maximum age of the session cookie. If a duration suffix is not specified, seconds will be used.
server.servlet.session.cookie.name= # 会话 cookie 名称。
server.servlet.session.cookie.path= # Path of the session cookie.
server.servlet.session.cookie.secure= # "Secure" flag for the session cookie.
server.servlet.session.persistent=false # Whether to persist session data between restarts.
server.servlet.session.store-dir= # Directory used to store session data.
server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds will be used.
server.servlet.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.accept-count=0 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
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 # Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=30s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.
server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is 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-connections=0 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-header-size=0 # Maximum size, in bytes, of the HTTP message header.
server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content.
server.tomcat.max-threads=0 # Maximum number of worker threads.
server.tomcat.min-spare-threads=0 # Minimum number 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 indicating whether 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.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # 用于解码 URI 的字符编码。
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.
server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Whether to enable the access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.
server.undertow.accesslog.prefix=access_log. # Log file name prefix.
server.undertow.accesslog.rotate=true # Whether to enable access log rotation.
server.undertow.accesslog.suffix=log # Log file name suffix.
server.undertow.buffer-size= # Size of each buffer, in bytes.
server.undertow.direct-buffers= # Whether to allocate buffers outside the Java heap.
server.undertow.io-threads= # Number of I/O threads to create for the worker.
server.undertow.eager-filter-init=true # Whether servlet filters should be initialized on startup.
server.undertow.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content.
server.undertow.worker-threads= # Number of worker threads.

4、参考上面的修改项直接在全局配置文件(application.properties 或 application.yml)中修改即可:

server:port: 8080    #修改服务器 http 端口,默认为 8080servlet:context-path: /h2Smil   #设置应用上下文,必须以"/"开头session:timeout: 60m #会话超时时间,如果未指定单位,则默认为秒。默认为 30m 即 30 分钟。tomcat:max-connections: 800  #服务器在任何给定时间接受和处理的最大连接数。默认为 1000max-threads: 300      #最大工作线程数。默认 200min-spare-threads: 30 #最小工作线程数。默认 10max-http-post-size: 4MB  # HTTP post 请求内容的最大大小。默认为 2MB

web服务定制器修改 Servlet 容器配置

1、实现 org.springframework.boot.web.server.WebServerFactoryCustomizer 接口的 customize方法。如下所示创建 WebServerFactoryCustomizer 实例的时候,可以传入泛型 T,T 又是继承 WebServerFactory 接口的子接口。

import org.springframework.boot.web.server.WebServerFactory;
@FunctionalInterface
public interface WebServerFactoryCustomizer<T extends WebServerFactory> {void customize(T var1);
}

2、如下所示是 WebServerFactory 接口的所有子接口,创建 WebServerFactoryCustomizer(web 服务器工厂定制器)时根据传入的泛型参数(WebServerFactory)不同,customize 方法中参数也就不同,customize 方法参数不同,则能调用的方法也就不同

import com.lct.component.MyLocaleResolve;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
/*** Created by Administrator on 2018/7/28 0028.* 自定义配置类*/
@Configuration
public class MyMvcConfig {/*** 将我们自己的 LocaleResolver 组件交于 Spring 容器管理* 从而覆盖 Spring Boot默认的区域信息解析器** @return*/@Beanpublic LocaleResolver localeResolver() {return new MyLocaleResolve();}/*** 自定义嵌入式Servlet容器定制器 组件* 然后修改应用上下文路径以及 Tomcat 端口** @return*/@Beanpublic WebServerFactoryCustomizer webServerFactoryCustomizer() {/*** 使用 ConfigurableServletWebServerFactory*/return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {@Overridepublic void customize(ConfigurableServletWebServerFactory configurableServletWebServerFactory) {/*** 修改应用上下文路径以及 Tomcat 端口*/configurableServletWebServerFactory.setContextPath("/cat");configurableServletWebServerFactory.setPort(8084);}};}
}

Spring Boot 2.0.3 修改 Servlet 容器(服务器)配置相关推荐

  1. Spring Security with Spring Boot 2.0:使用Servlet堆栈的简单身份验证

    Spring安全性是一个很好的框架,可节省开发人员的大量时间和精力. 此外,它还具有足够的灵活性,可以自定义并满足您的需求. 随着spring的发展,spring安全性也使得在项目中设置安全性变得更加 ...

  2. Spring Boot切换其他嵌入式的Servlet容器

    Spring Boot默认支持: Tomcat(默认使用) <dependency><groupId>org.springframework.boot</groupId& ...

  3. 基于Spring Boot 2.0的IoT应用集成和使用CSE实践

    本文通过一个IoT的应用展现在Spring Boot 2.0中集成和使用CSE.IoT应用原来使用Spring Boot 2.0开发,通过少量的步骤集成CSE,然后展现了集成后带来了哪些新特性,以及中 ...

  4. Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析

    在Spring Boot 2.0中推出了Relaxed Binding 2.0,对原有的属性绑定功能做了非常多的改进以帮助我们更容易的在Spring应用中加载和读取配置信息.下面本文就来说说Sprin ...

  5. Java Spring Boot 2.0实战Docker容器与架构原理,视频与课件,基于Linux环境...

    Java Spring Boot 2.0实战Docker容器Linux与架构原理 内容摘要:Docker是最流行的开源容器引擎,Go语言开发,在互联网大规模集群.云计算.微服务等架构中广泛使用.本次课 ...

  6. 【译】Spring Boot 2.0 官方迁移指南

    前提 希望本文档将帮助您把应用程序迁移到 Spring Boot 2.0. 在你开始之前 首先,Spring Boot 2.0 需要 Java 8 或更高版本.不再支持 Java 6 和 7 了. 在 ...

  7. Spring Boot 2.0正式发布,升还是不升呢?

    Spring帝国 Spring几乎是每一位Java开发人员都耳熟能详的开发框架,不论您是一名初出茅庐的程序员还是经验丰富的老司机,都会对其有一定的了解或使用经验.在现代企业级应用架构中,Spring技 ...

  8. Spring Boot 2.0 新特性

    作者:贺卓凡 原文:https://mp.weixin.qq.com/s/EWmuzsgHueHcSB0WH-3AQw 以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8 ...

  9. Spring Boot 2.0官方文档之 Actuator

    https://blog.csdn.net/alinyua/article/details/80009435 前言:本文翻译自Spring Boot 2.0.1.RELEASE官方文档,该Spring ...

  10. Spring Boot 2.0 新特性和发展方向

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

最新文章

  1. forkjoin rxjs_如何通过吃披萨来理解RxJS运算符:zip,forkJoin和Combine
  2. 【Python之旅】第五篇(一):Python Socket通信原理
  3. 服务器系统linux怎么安装教程,CentOS 8.0.1905 linux服务器系统安装与配置图解教程...
  4. altium designer 不自动清除回路 清除功能打开失效
  5. 深入浅出:移动端(Android 和 iOS)数据采集埋点 SDK
  6. FOSS历史回顾:三代开源人的故事
  7. Matlab里evalin和assignin的用法
  8. Android BroadcastReceiver,广播与进程通讯,APK安装广播,获取已安装列表
  9. 探究Lucene计算权重的过程
  10. 想看程序员的成长课这本书
  11. 64位CentOS 6.4下安装wine
  12. 【图像加密】基于matlab混沌算法图像加密解密【含Matlab源码 1218期】
  13. doapk+java环境_JD-GUI使用方法JD-GUIjava反编译工具下载(支持X64位的系统) v1.5 最新版 - java反编译工具中文下载_数码资源网...
  14. BaiduPan百度网盘不限速教程
  15. Centos7 自动使用葵花8号卫星图片作为桌面壁纸
  16. 腾讯校招都会问些什么?| 五面腾讯(Java岗)经历分享
  17. 手机如何快速转换图片格式?改图片格式手机如何操作?
  18. 传智 刘意 2015年Java基础视频-深入浅出精华版 笔记 day24~day26(2016年4月26日13:11:30)
  19. Vscode中报错 CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
  20. android 随手记代码,随手记之Android网络调试简要记录

热门文章

  1. 用 JSON和userData 更全面的模拟 localStorage
  2. 强化学习(reinforcement learning)教程(后面是翻译)
  3. 拓端tecdat|r语言中使用Bioconductor 分析芯片数据
  4. 在SQL Server中建立主键外键的关系
  5. php未定义常量破解,如何使PHP未定义的常量注意到错误
  6. CopyQq program山寨QQ项目
  7. 怎样保证linux内核安全性,技术|如何做好 Linux 内核安全处理
  8. 图卷积网络详细介绍(三)
  9. CNN数值初始化——xavier
  10. pyspark报错问题 Exception in thread main java.lang.UnsupportedClassVersionError 成功解决