天天看點

(五)springboot項目實戰jetty優化

1 介紹

springboot預設配置的是tomcat容器,tomcat是一個重量級容器,今天我來說下我搭建的springboot項目配置的是jetty容器,jetty是一個輕量級的容器。那麼下面來說下springboot應該怎麼引用jetty容器呢?應該怎麼優化呢?

2 這裡需要注意一下由于springboot web項目在建立的時候會引入spring-boot-starter-web包,這個包裡是預設啟動tomcat配置的,是以我們在引入jetty容器前,先剔除掉tomcat的預設配置吧,看下面的pom.xml配置吧

pom.xml配置

<!-- 整體的pom配置我就不再這裡粘了 -->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- 去除預設tomcat配置 -->
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
                <!-- 去除預設日志配置 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!-- 引入jetty -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
           

3 pom檔案中需要的jar包我們引用好了那麼下面開啟我們jetty啟動配置之旅吧!

package com.fy.agent.api.config.jetty;

import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : lqf
 * @description : jetty自動配置
 * @date : Create in 14:58 2018/5/8
 */
@Configuration
public class JettyConfig {

    @Bean
    public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(
            JettyServerCustomizer jettyServerCustomizer) {
        JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
        factory.addServerCustomizers(jettyServerCustomizer);
        return factory;
    }


    @Bean
    public JettyServerCustomizer jettyServerCustomizer() {
        return server -> {
            threadPool(server);
            accessLog(server);
        };
    }

    private void threadPool(Server server){
        // Tweak the connection config used by Jetty to handle incoming HTTP
        // connections
        final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
        //預設最大線程連接配接數200
        threadPool.setMaxThreads();
        //預設最小線程連接配接數8
        threadPool.setMinThreads();
        //預設線程最大空閑時間60000ms
        threadPool.setIdleTimeout();
    }
    //jetty啟動日志
    private void accessLog(Server server) {
        NCSARequestLog requestLog = new NCSARequestLog("logs/jetty-yyyy_mm_dd.request.log");
        requestLog.setAppend(true);
        requestLog.setExtended(false);
        requestLog.setLogTimeZone("GMT+08");
        requestLog.setLogLatency(true);
        requestLog.setRetainDays();
        server.setRequestLog(requestLog);
    }
}
           

上面的代碼就是我項目中的jetty配置,這個配置你可以放在啟動類中也可以單獨寫出一個jettyConfig啟動類,通過@Configuration将此類标記成配置類在項目啟動的時候就會去加載這個類中的資訊了。個人習慣喜歡将不同的配置統一放到一個配置包中。