zoukankan      html  css  js  c++  java
  • SpringBoot2使用Jetty容器(替换默认Tomcat)

    前言

    默认情况下,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.
    

    同样,我们可以通过JettyEmbeddedServletContainerFactory bean以编程的方式去配置这些参数

    @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参数: 创建ConfigurableServletWebServerFactory Bean

    @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;
    }
    

    原文文链

    Site4J


    @Author      风一样的码农
    @HomePageUrl http://www.cnblogs.com/chenpi/
    @Copyright      转载请注明出处,谢谢~
     
    分类: Spring Boot
  • 相关阅读:
    Spark内核
    Scala(十一)泛型、项目案例
    离线数仓(一)
    SparkSparkCore(二)
    SharePoint 2013 Preview相关软件及必备组件下载地址
    SharePoint 2010 文档库AllItems.aspx页面出现乱码,打开即提示下载
    [SharePoint 2010]System.IO.FileLoadException: 找到的程序集清单定义与程序集引用不匹配
    '添加解决方案'这一部署步骤中发生错误:"未能提取解决方案中的 cab 文件"
    SharePoint 2010 BackupSPSite 备份网站集时报错 异常来自 HRESULT:0x80131904
    JSON介绍
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/12059399.html
Copyright © 2011-2022 走看看