如何配置Spring Boot Tomcat
1.概述
Spring Boot Web应用程序默认包含预配置的嵌入式Web服务器。但在某些情况下,我们要修改默认配置以满足自定义要求。
在本教程中,我们将介绍通过application.properties文件配置Tomcat嵌入式服务器的一些常见用例。
2.常见的嵌入式Tomcat配置
2.1. 服务器地址和端口
我们希望更改的最常见配置是端口号:
server.port = 80
如果我们不提供server.port 参数,则默认设置为8080。
在某些情况下,我们可能希望设置服务器应绑定的网络地址。换句话说,我们定义一个服务器将监听的IP地址:
server.address = my_custom_ip
默认情况下,该值设置为0.0.0.0,允许通过所有IPv4地址进行连接。设置另一个值,例如localhost -127.0.0.1 - 将使服务器更具选择性。
2.2. 错误处理
默认情况下,Spring Boot提供标准错误网页。此页面称为Whitelabel。它默认启用,但如果我们不想显示任何错误信息,我们可以禁用它:
server.error.whitelabel.enabled = false
Whitelabel的默认路径是*/error*。可以通过设置server.error.path参数来自定义它:
server.error.path = /user-error
还可以设置属性,以确定显示有关错误的信息。例如,我们可以包含错误消息和堆栈跟踪:
server.error.include-exception= true
server.error.include-stacktrace= always
我们的教程Exception Message Handling for REST和[Customize Whitelabel Error Page](www.baeldung.com/ spring-boot-custom-error-page) 详细解释有关Spring Boot中处理错误的更多信息。
2.3. 服务器连接
当在低资源容器上运行时,我们可能希望减少CPU和内存负载。一种方法是限制应用程序可以同时处理的请求数量。相反,我们可以增加此值以使用更多可用资源来获得更好的性能。
在Spring Boot中,我们可以定义Tomcat工作线程的最大数量:
server.tomcat.max-threads= 200
配置Web服务器时,设置服务器连接超时也可能很有用。这表示服务器在连接关闭之前等待客户端发出请求的最长时间:
server.connection-timeout= 5s
我们还可以定义请求头的最大大小:
server.max-http-header-size= 8KB
请求正文的最大大小:
server.tomcat.max-swallow-size= 2MB
或者整个POST请求的最大大小:
server.tomcat.max-http-post-size= 2MB
2.4. SSL
要在我们的Spring Boot应用程序中启用SSL支持,我们需要将server.ssl.enabled属性设置为true,并定义SSL协议:
server.ssl.enabled = true
server.ssl.protocol = TLS
我们要配置保存证书密钥库的密码,类型和路径:
server.ssl.key-store-password=my_password
server.ssl.key-store-type=keystore_type
server.ssl.key-store=keystore-path
我们还必须定义标识密钥库中密钥的别名:
server.ssl.key-alias=tomcat
有关SSL配置的更多信息,请访问:HTTPS using self-signed certificate in Spring Boot。
2.5. Tomcat服务器访问日志
在尝试统计页面命中数,用户会话活动等时,Tomcat访问日志非常有用。
要启用访问日志,只需设置:
server.tomcat.accesslog.enabled = true
我们还应该配置其他参数,例如附加到日志文件的目录名,前缀,后缀和日期格式:
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
3. 结论
在本教程中,我们学习了一些常见的Tomcat嵌入式服务器配置。要查看更多可能的配置,请访问官方页面: Spring Boot application properties docs。
与往常一样,这些示例的源代码可以在GitHub上找到。
作者:baeldung
译者:Leesen
如何配置Spring Boot Tomcat的更多相关文章
- Spring Boot Tomcat配置详解
参数配置容器 server.xx开头的是所有servlet容器通用的配置,server.tomcat.xx开头的是tomcat特有的参数,其它类似. 所有参数绑定配置类:org.springframe ...
- 配置spring boot 内置tomcat的accessLog日志
#配置内置tomcat的访问日志server.tomcat.accesslog.buffered=trueserver.tomcat.accesslog.directory=/home/hygw/lo ...
- spring boot tomcat 打本地包成war,通过Tomcat启动时出现问题: ZipException: error in opening zip file
一个第三方公司提供spring boot 项目,直接启动是ok的, 但是打包成war,通过Tomcat启动,就出现 ZipException: error in opening zip file: 2 ...
- Spring Boot tomcat
定制内嵌 Tomcat 设置内嵌Tomcat的端口 Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口.如果,我们需要修改Tomcat的端口,我们可以在 src/main/ ...
- spring boot tomcat 部署
前几天springboot项目部署到linux中,整个过程就是个坑啊.踩坑的过程中也学到了许多.spring boot 项目部署时由于其内置了tomcat和jdk,而且还都是8. 所以部署的话就分为两 ...
- 在IDEA中配置spring boot项目的热更新
在我使用IDEA的过程中,我发现Spring Boot项目本来自带的一个热部署工具无法使用,这里在参考各家博客后给出解决方案: 修改POM 首先POM文件需要包含spring boot的热部署工具,m ...
- RabbitMQ进阶使用-延时队列的配置(Spring Boot)
依赖 MAVEN配置pom.xml <dependency> <groupId>org.springframework.boot</groupId> <art ...
- Linux系统CentOS 7配置Spring Boot运行环境
从阿里云新买的一台Linux服务器,用来部署SpringBoot应用,由于之前一直使用Debian版本,环境配置有所不同,也较为繁琐,本文主要介绍CentOS下配置SpringBoot环境的过程 新建 ...
- vs code 配置spring boot开发环境
一.环境变量 jdk环境变量一键设置 管理員运行 - 一支小白 - 博客园https://www.cnblogs.com/startnow/p/7416533.html 二.安装插件 1.Java E ...
随机推荐
- 球形空间产生器sphere(bzoj 1013)
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...
- hdu 1867 A+B again for you
A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Android Win7 上使用cmd生成Jks
Android Win7 上使用cmd生成Jks cmd 命令 C:\Program Files\Java\jre1.8.0_111\bin>keytool -genkeypair -alias ...
- [LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...
- 本机开发Native Development:Invalid path for NDK (转)
打开window菜单下的preference选项.选择Android,Native Development(本地开发) 选择你的NDK安装目录.但是,这个插件目前仅支持ndk的r4和r5版本,更高版本 ...
- Day 22 Object_oriented_programming 3
isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象,如果是返回True 1 class F ...
- CodeForces - 316E3 Summer Homework
Discription By the age of three Smart Beaver mastered all arithmetic operations and got this summer ...
- windows 下使用github客户端报错:Failed to publish this branch
在windows系统下使用github客户端同步的时候报错“Failed to publish this branch”,查找原因,发现结果是安装vscode的时候没有检查到git,然后安装git后库 ...
- 翻译BonoboService官网的安装教程
This page covers simple Bonobo Git Server installation. Be sure to check prerequisites page before i ...
- Start Developing iOS Apps Today
view types - view常见类型