• 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依赖
    
    	
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 若是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")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 配置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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 测试沿用上述服务消费者启动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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 在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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    下一篇:SpringCloud-8-Eureka:服务注册与发现组件介绍
  • 相关阅读:
    基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
    Spring Boot + EasyExcel导入导出,简直太好用了!
    【问题解决】Android JDK版本不匹配导致崩溃踩坑记录
    C++ -------- 智能指针
    java-php-net-python-图书馆选择计算机毕业设计程序
    QWidget | Qt::WindowType 枚举的取值及意义&QFlags 模板类详解
    学生学徒作品分享——金融大模型-房屋租金价格影响因素分析与预测
    Windows平台下私有云盘搭建
    【RHCE】作业:配置NFS开发目录&配置DNS正向解析域名
    安全实战 | 怎么用零信任防范弱密码?
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/126821385