Spring Boot – Jetty配置
2018-09-24 19:33
316 查看
前言
默认情况下,Spring Boot会使用内置的tomcat容器去运行应用程序,但偶尔我们也会考虑使用Jetty去替代Tomcat;
对于Tomcat和Jetty,Spring Boot分别提供了对应的starter,以便尽可能的简化我们的开发过程;
当我们想使用Jetty的时候,可以参考以下步骤来使用。
添加spring-boot-starter-jetty依赖
我们需要更新
pom.xml文件,添加
spring-boot-starter-jetty依赖,同时我们需要排除
spring-boot-starter-web默认的
spring-boot-starter-tomcat依赖,如下所示:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
如果我们的工程是使用Gradle构建的话,可以使用以下方式达到同样的效果:
configurations { compile.exclude module: "spring-boot-starter-tomcat" } dependencies { compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT") compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT") }
配置Jetty参数
我们可以在application.properties配置文件里配置相关参数,去覆盖Jetty默认使用的运行参数:
application.properties
server.port=8080 server.servlet.context-path=/home ####Jetty specific properties######## server.jetty.acceptors= # Number of acceptor threads to use. 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.
同样,我们可以通过
JettyEmbeddedServletContainerFactorybean以编程的方式去配置这些参数
@Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory(); jettyContainer.setPort(9000); jettyContainer.setContextPath("/home"); return jettyContainer; }
Spring boot 2.0.0.RELEASE版本之后的更新
以上代码针对的是Spring Boot Snapshot版本,在Spring boot 2.0.0.RELEASE版本之后,我们应该使用
ConfigurableServletWebServerFactory和
JettyServletWebServerFactory类去配置Jetty参数:
创建
ConfigurableServletWebServerFactoryBean
@Bean public ConfigurableServletWebServerFactory webServerFactory() { JettyServletWebServerFactory factory = new JettyServletWebServerFactory(); factory.setPort(9000); factory.setContextPath("/myapp"); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); return factory; }
原文文链
相关文章推荐
- SpringBoot初始教程之Tomcat、Jetty优化以及Https配置(九)
- 服务器 配置SSL for nginx && springboot(jetty)
- 详解SpringBoot初始教程之Tomcat、Https配置以及Jetty优化
- Spring-boot 配置Jetty 容器
- SpringBoot初始教程之Tomcat、Jetty优化以及Https配置(九)
- Spring Boot属性配置文件详解
- Spring-Boot-多种配置注入方式
- 加入spring-boot后导致maven的profile多环境配置失效的问题解决
- spring boot 项目中hanlp的配置(可增加自定义词典)
- SpringBoot的WebSocket基于STOMP协议自动广播推送配置
- Spring Boot 配置ContextPath
- 【SpringBoot教程五】:SpringBoot+LogBack日志配置
- [深入剖析Spring Boot]启动、事件通知与配置加载原理
- Spring Boot配置随机数
- Spring Boot系列教程六:日志输出配置log4j2
- springboot,修改配置文件不起作用
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- spring boot druid连接池监控配置
- spring-boot-starter-logging logback配置之<configuration><logger>标签详解
- springboot 集成elasticsearh的简单配置