SpringCloud-7-Spring Boot使用Jetty容器

4.8 Spring Boot使用Jetty容器
  • 使用Jetty去替代Tomcat?对于Tomcat和Jetty,Spring Boot分别提供了对应的starter,以便尽可能的简化我们的开发过程
  • 在pom.xml文件中添加spring-boot-starter-jetty依赖,同时要排除spring-boot-starter-web默认的spring-boot-starter-tomcat依赖
<dependencies>
	<!--Spring Boot Web-->
    <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>
    <!--jetty-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>
  • 若是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.yml配置文件里配置相关参数,去覆盖Jetty默认使用的运行参数
server:
  port: 80
  servlet:
    context-path: /app
#Jetty specific properties
  jetty:
    threads:
      acceptors: -1                   #Number of acceptor threads to use. When the value is -1, the default, the number of acceptors is derived from the operating environment.
      selectors: -1                   #Number of selector threads to use. When the value is -1, the default, thenumber of selectors is derived from the operating environment.
    max-http-form-post-size: 200000   #Maximum size of the form content in any HTTP post request.
  • 测试沿用上述服务消费者启动jetty容器
    在这里插入图片描述
  • 然后访问http://127.0.0.1/app/consumer/dept/get/1,jetty成功启动
    在这里插入图片描述
  • SpringBoot版本RELEASE GA,SNAPSHOT,PRE区别

RELEASE GA:General Availability,正式发布的版本,官方推荐使用此版本。

SNAPSHOT: 快照版,可以稳定使用,且仍在继续改进版本。

PRE: 预览版,内部测试版. 主要是给开发人员和测试人员测试和找BUG用的,不建议使用。

  • 在Spring boot 2.0.0版以前可以注册JettyEmbeddedServletContainerFactory bean来配置这些参数
@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
    JettyEmbeddedServletContainerFactory jettyContainer = new JettyEmbeddedServletContainerFactory();
    jettyContainer.setPort(80);
    jettyContainer.setContextPath("/app");
    return jettyContainer;
}
  • 在Spring boot 2.0.0.RELEASE版本之后,应使用 ConfigurableServletWebServerFactoryJettyServletWebServerFactory类去配置Jetty参数:
@Bean
public ConfigurableServletWebServerFactory  configurableServletWebServerFactory(){
	//JettyServletWebServerFactory是ConfigurableServletWebServerFactory的实现类
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory(); 
    factory.setPort(80);
    factory.setContextPath("/app");
    factory.setAcceptors(-1);
    factory.setSelectors(-1);
    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
    return factory;
}
下一篇:SpringCloud-8-Eureka:服务注册与发现组件介绍
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值