springboot项目中使用jettyweb服务器
Servlet规范支持方面
(1)Jetty的应用更加快速,修改简单,对新的Servlet规范的支持较好。 GAE(谷歌应用引擎已经全面切换为Jetty)
(2)Tomcat目前应用比较广泛,对JavaEE和Servlet的支持更加全面,很多特性会直接集成进来。(中小企业还再在用)
导包 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>
在spring-boot-autoconfigure 这个jar中的org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration类中, 通过Import注解加入了Jetty的支持
@Configuration@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new ServletWebServerFactoryCustomizer(serverProperties);
}
@Bean
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
ServerProperties serverProperties) {
return new TomcatServletWebServerFactoryCustomizer(serverProperties);
}
...
}
常用配置
server.port=8080server.servlet.context-path=/
server.jetty.thread-idle-timeout=60000
server.jetty.connection-idle-timeout=30000
server.jetty.max-http-form-post-size=1000
server.jetty.max-threads=200
server.jetty.min-threads=10
server.jetty.acceptors=-1
server.jetty.selectors=-1
server.jetty.accesslog.enabled=true
server.jetty.accesslog.append=true
server.jetty.accesslog.retention-period=20
server.jetty.accesslog.file-date-format=.yyyy-MM-dd
server.jetty.accesslog.filename= D:/logs/access_log.yyyy_MM_dd.log
配置https访问
server.ssl.port=8443server.ssl.enabled=true
server.ssl.key-store=D:/ssl.keystore
server.ssl.key-store-password=0123789
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=ssl_alias
@Value("${server.ssl.key-store:classpath:ssl.keystore}")private String path;
@Value("${server.ssl.key-store-password:123456}")
private String password;
@Value("${server.port:8443}")
private int port;
@Value("${server.ssl.port:8443}")
private int sslPort;
@Value("${server.jetty.thread-idle-timeout:60000}")
private int timeout;
@Value("${server.jetty.max-threads:200}")
private int maxThreads;
@Value("${server.jetty.max-threads:10}")
private int minThreads;
@Bean
public ServletWebServerFactory servletContainer() {
JettyServletWebServerFactory container = new JettyServletWebServerFactory();
container.addServerCustomizers((Server server) -> {
//配置线程
final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
//默认最大线程连接数
threadPool.setMaxThreads(maxThreads);
//默认最小线程连接数
threadPool.setMinThreads(minThreads);
//默认线程最大空闲时间60000ms
threadPool.setIdleTimeout(timeout);
// 添加HTTP配置
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
// 添加HTTPS配置
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(path);
sslContextFactory.setKeyStorePassword(password);
HttpConfiguration config = new HttpConfiguration();
config.setSecureScheme(HttpScheme.HTTPS.asString());
config.addCustomizer(new SecureRequestCustomizer());
ServerConnector sslConnector = new ServerConnector(
server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(config));
sslConnector.setPort(sslPort);
server.setConnectors(new Connector[]{connector, sslConnector});
});
return container;
}
源码: https://gitee.com/lion123/springboot-jetty-demo
以上是 springboot项目中使用jettyweb服务器 的全部内容, 来源链接: utcz.com/z/514206.html