尝试使用Spring Boot中的Jetty,并更改Jetty的配置

在Spring Boot中,可以选择在Tomcat、Jetty和Undertow之间使用内置容器。过去一直使用Tomcat,但是由于切换到Jetty似乎也很简单,所以我想试试看使用Jetty。

请参考以下网站

公式
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html

在pom.xml文件中进行配置

在spring-boot-starter-web中,默认使用Tomcat作为容器。因此,首先排除Tomcat,然后添加Jetty。具体如下:
※Gradle的写法也可在官方文档中找到。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- Tomcatを除外 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Jettyを追加 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

现在可以使用Jetty了。

试着启动Jetty。

让我们像以前一样启动Jetty并检查控制台日志。

2015-12-20 00:04:06.792  INFO 5840 --- [           main] com.example.SpringBootJettyApplication   : Starting SpringBootJettyApplication on N-PC with PID 5840 (C:\Development\sts-bundle\workspace\spring-boot-jetty\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-jetty)
~略~
2015-12-20 00:04:18.721  INFO 5840 --- [           main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2015-12-20 00:04:18.746  INFO 5840 --- [           main] com.example.SpringBootJettyApplication   : Started SpringBootJettyApplication in 14.129 seconds (JVM running for 16.169)

通过端口8080(http/1.1),可以看出Jetty已经启动了。接下来我们将与Tomcat启动时间进行比较。以下是Tomcat启动时的控制台日志。

2015-12-19 23:58:52.347  INFO 9896 --- [           main] com.example.SpringBootJettyApplication   : Starting SpringBootJettyApplication on N-PC with PID 9896 (C:\Development\sts-bundle\workspace\spring-boot-jetty\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-jetty)
~略~
2015-12-19 23:59:09.642  INFO 9896 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-12-19 23:59:09.671  INFO 9896 --- [           main] com.example.SpringBootJettyApplication   : Started SpringBootJettyApplication in 18.602 seconds (JVM running for 21.014)

Jetty使用了14.129秒。
Tomcat则需要18.602秒。

果然Jetty更快。

尝试更改Jetty的配置

在中国还可以通过application.properties(或YML)进行Jetty的配置更改,但也可以通过JettyEmbeddedServletContainerFactory和JettyServerCustomizer的代码来实现灵活的更改。

    • JettyEmbeddedServletContainerFactory

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.html

JettyServerCustomizer

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/embedded/jetty/JettyServerCustomizer.html

我用以下的方式进行了尝试。

创建JettyServerCustomizer的实现类并重写customize方法。可以获取org.eclipse.jetty.server.Server。这个类是Connector和RequestHandler的集合类,用于配置服务器的基本设置。

package com.example.config;

import org.eclipse.jetty.server.Server;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class JettyServerCustomizerImpl implements JettyServerCustomizer {

    @Override
    public void customize(Server server) {
        System.out.println("JettyServer Customize!!");
        server.setStopTimeout(100);
    }
}

接下来,我们需要创建一个实现EmbeddedServletContainerCustomizer接口的类并重写customize方法。
我们可以获取到org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer,它的实际类型是JettyEmbeddedServletContainerFactory。我们可以使用这个类来对容器进行设置。最后,我们可以将上述的JettyServerCustomizerImpl传递给addServerCustomizers方法,从而实现对整个服务器的自定义设置。

package com.example.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.stereotype.Component;

@Component
public class JettyConfig implements EmbeddedServletContainerCustomizer {

    @Autowired
    JettyServerCustomizerImpl jettyServerCustomizerImpl;

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        System.out.println("JettyContainer Configuration!!");
        container.setPort(8888);
        ((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(jettyServerCustomizerImpl);
    }
}

这些类在Jetty启动时运行,并作为容器设置的反映。以下是启动日志。从控制台输出可以看出,上述两个类在启动时被加载。

2015-12-20 20:45:04.504  INFO 9308 --- [           main] com.example.SpringBootJettyApplication   : Starting SpringBootJettyApplication on N-PC with PID 9308 (C:\Development\sts-bundle\workspace\spring-boot-jetty\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-jetty)
2015-12-20 20:45:04.507  INFO 9308 --- [           main] com.example.SpringBootJettyApplication   : No active profile set, falling back to default profiles: default
2015-12-20 20:45:04.578  INFO 9308 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52e677af: startup date [Sun Dec 20 20:45:04 JST 2015]; root of context hierarchy
2015-12-20 20:45:05.766  INFO 9308 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
JettyContainer Configuration!!
2015-12-20 20:45:06.829  INFO 9308 --- [           main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8888
JettyServer Customize!!
2015-12-20 20:45:06.832  INFO 9308 --- [           main] org.eclipse.jetty.server.Server          : jetty-9.2.14.v20151106
2015-12-20 20:45:06.975  INFO 9308 --- [           main] application                              : Initializing Spring embedded WebApplicationContext
2015-12-20 20:45:06.975  INFO 9308 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2400 ms
2015-12-20 20:45:07.626  INFO 9308 --- [           main] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-12-20 20:45:07.629  INFO 9308 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-12-20 20:45:07.629  INFO 9308 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-12-20 20:45:07.629  INFO 9308 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2015-12-20 20:45:07.630  INFO 9308 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2015-12-20 20:45:07.902  INFO 9308 --- [           main] o.e.jetty.server.handler.ContextHandler  : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@6bb7cce7{/,file:/C:/Users/N/AppData/Local/Temp/jetty-docbase.186998930257237945.8888/,AVAILABLE}
2015-12-20 20:45:07.903  INFO 9308 --- [           main] org.eclipse.jetty.server.Server          : Started @5155ms
2015-12-20 20:45:08.201  INFO 9308 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52e677af: startup date [Sun Dec 20 20:45:04 JST 2015]; root of context hierarchy
2015-12-20 20:45:08.315  INFO 9308 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-12-20 20:45:08.317  INFO 9308 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2015-12-20 20:45:08.594  INFO 9308 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-20 20:45:08.595  INFO 9308 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-20 20:45:08.643  INFO 9308 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-20 20:45:08.854  INFO 9308 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-12-20 20:45:08.879  INFO 9308 --- [           main] application                              : Initializing Spring FrameworkServlet 'dispatcherServlet'
2015-12-20 20:45:08.880  INFO 9308 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2015-12-20 20:45:08.898  INFO 9308 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms
2015-12-20 20:45:08.926  INFO 9308 --- [           main] o.eclipse.jetty.server.ServerConnector   : Started ServerConnector@7906578e{HTTP/1.1}{0.0.0.0:8888}
2015-12-20 20:45:08.930  INFO 9308 --- [           main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8888 (http/1.1)
2015-12-20 20:45:08.936  INFO 9308 --- [           main] com.example.SpringBootJettyApplication   : Started SpringBootJettyApplication in 5.441 seconds (JVM running for 6.189)

虽然有一些解释不太清楚的地方,但我能够按照这种方式进行设置。

广告
将在 10 秒后关闭
bannerAds