自定义 Web 容器

如果你的项目并发量比较高,想要修改最大线程数、最大连接数等配置信息,可以通过自定义Web 容器的方式,代码如下所示。

@SpringBootApplication(proxyBeanMethods = false)

public class App implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    public static void main(String[] args) {

        SpringApplication.run(PetClinicApplication.class, args);

    }

    @Override

    public void customize(ConfigurableServletWebServerFactory factory) {

        TomcatServletWebServerFactory f = (TomcatServletWebServerFactory) factory;

        f.setProtocol("org.apache.coyote.http11.Http11Nio2Protocol");



        f.addConnectorCustomizers(c -> {

            Http11NioProtocol protocol = (Http11NioProtocol) c.getProtocolHandler();

            protocol.setMaxConnections(200);

            protocol.setMaxThreads(200);

            protocol.setSelectorTimeout(3000);

            protocol.setSessionTimeout(3000);

            protocol.setConnectionTimeout(3000);

        });

    }

}

  

注意上面的代码,我们设置了它的协议为 org.apache.coyote.http11.Http11Nio2Protocol,意思就是开启了 Nio2。这个参数在 Tomcat 8.0之后才有,开启之后会增加一部分性能。
对比如下(测试项目代码见 spring-petclinic-main):https://gitee.com/xjjdog/tuning-lagou-res/tree/master/tuning-020/spring-petclinic-main

默认。

[root@localhost wrk2-master]# ./wrk -t2 -c100 -d30s -R2000 http://172.16.1.57:8080/owners?lastName=

Running 30s test @ http://172.16.1.57:8080/owners?lastName=

  2 threads and 100 connections

  Thread calibration: mean lat.: 4588.131ms, rate sampling interval: 16277ms

  Thread calibration: mean lat.: 4647.927ms, rate sampling interval: 16285ms

  Thread Stats   Avg      Stdev     Max   +/- Stdev

    Latency    16.49s     4.98s   27.34s    63.90%

    Req/Sec   106.50      1.50   108.00    100.00%

  6471 requests in 30.03s, 39.31MB read

  Socket errors: connect 0, read 0, write 0, timeout 60

Requests/sec:    215.51

Transfer/sec:      1.31MB

  

[root@localhost wrk2-master]# ./wrk -t2 -c100 -d30s -R2000 http://172.16.1.57:8080/owners?lastName=

Running 30s test @ http://172.16.1.57:8080/owners?lastName=

  2 threads and 100 connections

  Thread calibration: mean lat.: 4358.805ms, rate sampling interval: 15835ms

  Thread calibration: mean lat.: 4622.087ms, rate sampling interval: 16293ms

  Thread Stats   Avg      Stdev     Max   +/- Stdev

    Latency    17.47s     4.98s   26.90s    57.69%

    Req/Sec   125.50      2.50   128.00    100.00%

  7469 requests in 30.04s, 45.38MB read

  Socket errors: connect 0, read 0, write 0, timeout 4

Requests/sec:    248.64

Transfer/sec:      1.51MB

  你甚至可以将 tomcat 替换成 undertow。undertow 也是一个 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>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-undertow</artifactId>

    </dependency>

  

上一篇:http协议学习(一)


下一篇:firewalld-富规则使用内容事项: