• 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:服务注册与发现组件介绍
  • 相关阅读:
    国际站星级评定,自养号测评补单方式提升星级的优势与具体要求
    给一家银行做的数据中台系统架构方案书(DAMM)招投标用,虽然有内定潜规则,但是方案都是要的,不一定就是价格低就能中标,毕竟是上百万以上的单子
    自定义Dynamics 365实施和发布业务解决方案 - 6. Azure集成
    【Kafka源码分析】二、服务端Server
    神经网络简介
    OneNote 教程,如何在 OneNote 中设置笔记格式?
    猿创征文 | 大学生应该知道的开发工具
    前端面试(4)—— DOM事件的总结
    MBA-day25 最值问题-应用题
    开源治理:安全的关键
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/126821385