当前位置:网站首页>E-commerce spike project harvest (2)

E-commerce spike project harvest (2)

2022-08-10 15:33:00 westbrook gorilla

tomcatContainer configuration optimization

tomcat连接池

The connection pool has two of the most important configurations:最小连接数和最大连接数,它们控制着从连接池中获取连接的流程:
        如果当前连接数小于最小连接数,则创建新的连接处理数据库请求;
        如果连接池中有空闲连接则复用空闲连接;
        如果空闲池中没有连接并且当前连接数小于最大连接数,则创建新的连接处理请求;
        如果当前连接数已经大于等于最大连接数,Then wait for the old connection according to the time set in the configuration
        可用; 如果等待超过了这个设定时间则向用户抛出错误.

池化技术,减少频繁创建数据库连接的性能损耗:

in the high concurrency stage,The overhead of creating threads frequently can also be significant, 使用线程池预先创建线程;
JDK 1.5 中引入的 ThreadPoolExecutor 就是一种线程池的实现,It's important for two
的参数:coreThreadCount 和 maxThreadCount,These two parameters control the execution of the thread pool程:
如果线程池中的线程数少于 coreThreadCount 时,处理新的任务时会创建新的线程;
如果线程数大于 coreThreadCount 则把任务丢到一个队列里面,Executed by the currently idle thread
行;当队列中的任务堆积满了的时候,则继续创建线程,直到达到 maxThreadCount;
当线程数达到 maxTheadCount 时还有新的任务提交,Then they have to be thrown away.

这个任务处理流程看似简单,实际上有很多坑,Be careful when using it. 

首先, JDK 实现的这个线程池优先把任务放入队列暂存起来,而不是创建更多的线程,它
Comparison applies to execution CPU 密集型的任务,也就是需要执行大量 CPU 运算的任务.这是为什么呢?因为执行CPU 密集型的任务时 CPU 比较繁忙,因此只需要创建和 CPU Nuclei equivalent lines
Cheng is fine,多了反而会造成线程上下文切换,降低任务执行效率.So when the current number of threads exceeds the core
heart thread count,线程池不会增加线程,而是放在队列里等待核心线程空闲下来.
但是,平时开发的 Web 系统通常都有大量的 IO 操作(系统是IO密集型),比方说查询数据库、查询缓存等等.任务在执行 IO 操作的时候 CPU 就空闲了下来,这时如果增加执行任务的线程数而不是把任务暂存在队列中,就可以在单位时间内执行更多的任务,大大提高了任务执行的吞吐量.所以Tomcat 使用的线程池就不是 JDK 原生的线程池,而是做了一些改造,当线程数超过 coreThreadCount 之后会优先创建线程,直到线程数到达maxThreadCount,这样就比较适合于 Web 系统大量 IO 操作的场景了.
其次,The accumulation of queues used in the thread pool is also an important indicator to monitor,对于实时性要求比较高的任务来说,这个指标尤为关键.
In actual projects, there have been tasks that have been thrown to the thread pool,长时间都没有被执行的诡异问题.最初,The developers thought it was code Bug 导致的,后来经过排查发现,是因为线程池的
coreThreadCount 和 maxThreadCount 设置的比较小,Causes the task to have a large heap in the thread pool
积,在调大了这两个参数之后问题就解决了.跳出这个坑之后,Then put the queue of the important thread pool as any
stack volume,as an important monitoring indicator.
最后,If you use thread pools, please remember not to use unbounded queues(即没有设置固定大小的队列).也
Xu will feel after using the unbounded queue,任务就永远不会被丢弃,只要任务对实时性要求不高,反正
Sooner or later, there will be a day of consumption.但是,大量的任务堆积会占用大量的内存空间,Once the memory space is full
will be triggered frequently Full GC,造成服务不可用,可能一次 GC 引起的宕机,起因就是系统中的一个线程池使用了无界队列.

Why be righttomcat配置进行优化

Find capacity issues

serverThe number of concurrent threads on the side cannot go up,lead to the wholetpscapacity does not go up,After causing the client to be refused connection,Various problems occurred

查看SpringBoot配置:

要对tomcatContainer configuration optimization,The first thing to know is where the container configuration is;

spring-configuration-metadata.json文件下,View the configuration of each node

内嵌的tomcat默认的启动端口8080

tomcatThe default supported maximum number of threads is 200

其他默认参数 

最小线程数:tomcatThe minimum number of threads that can be created to handle requests

最大线程数:tomcat能创建来处理请求的最大线程数

acceptCount:最大等待队列数 ,请求并发大于tomcat线程池的处理能力,则被放入等待队列等待被处理.

maxIdleTime:最大空闲时间,超过这个空闲时间,且线程数大于最小空闲数的,都会被回收   

maxSpareTHreads:最大空闲线程数,在最大空闲时间内活跃过,但现在处于空闲,If the idle time is greater than the maximum idle time,则回收,小于则继续存活,等待被调度.

minSpareTHreads:最小空闲线程数,无论如何都会存活的最小线程数

实际开发中,Generally, the best value is found in continuous real-world environmental tests;All systems are based on different hardware capabilities、Adjust the environment,Then continue to test and verify,没有固定的方案.

定制化内嵌tomcat开发

配置

keepAliveTimeOut:多少毫秒后不响应的断开keepalive

maxKeepAliveRequests:多少次请求后keepalive断开失效

关于keepAlive

在使用Jmeter的HttpIt is enabled by default in requestsKeepAlive的,Http的KeepAliveThe request is when the client sends to the serverHttp请求的时候,If you bringKeepAlive的请求头,则表明HttpThe client wants to establish a relationship with the serverKeepAlive的连接,The usefulness of this connection is to say,After sending the corresponding response to the server,The server should not disconnect immediately,Instead, wait for an attempt to multiplex the connection.

This solution is used to fixHttp的一个响应,无状态,Disconnect every time,A time-consuming problem caused by new connections.

But if each web page request is opened, a long connection is maintained with the server,The number of connections on the server will soon be used up,Hence the earliestHttp1.0When there is no designKeepAlive的请求的,但是现在的Http1.1加上KeepAlive请求,The goal is more and more mobile devices,Even some very complex web page interactions,It needs to be in the process of user browsing,Frequently send requests to the server,因此,建立一个KeepAlive连接,Not for stress testing purposes,But there are real performance benefits in application scenarios,无论是客户端还是服务端,In the interaction of doing some network communication,There is no need to create a new connection every time,断开连接,耗费Tcp/IpConnection time,And just need to send data.

但是这样的设计也会带来一些问题,If our server is rightKeepAliveoperation without any restrictions
1.Connect does nothing,不做任何响应,Then this connection is a fee connection to the server
2.There are some malicious exploits by attackersKeepAliveThe connection is sent to the serverDDOS的攻击,The connection corresponding to the server will only become a backdoor for attackers to attack,因此,为了安全,We need customizationTomcat开发

//当spring容器内没有TomcatEmbeddedServletContainerFactory这个bean时,会把bean加载进spring容器
@Configuration
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        //使用对应工厂类提供给我们的接口定制化我们的tomcat connector
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol= (Http11NioProtocol) connector.getProtocolHandler();
                //定制KeepAliveTimeout,设置30秒内没有请求则服务器自动断开keepalive连接
                protocol.setKeepAliveTimeout(30000);
                //当客户端发送超过10000个请求则自动断开keepalive连接
                protocol.setMaxKeepAliveRequests(10000);
            }
        });
    }
}

keepalivefor and frontnginxA long link relationship is maintained between agents;Avoid needing to create a new link every time it is proxied,damage performance.

Find capacity issues

响应时间变长,TPS上不去

单Web容器上限

线程数量:4核CPU 8G内存单进程调用线程数800-1000,1000以上后即花费巨大的时间在CPU调度上

等待队列长度:队列做缓冲池用,但也不能无限长,消耗内存,出队入队也耗CPU

Mysql数据库QPS容量问题

大量的CPU耗费在mysqlthe query above

主键查询:千万级别数据 = 1-10毫秒
唯一索引查询:千万级别数据 = 10-100毫秒
非唯一索引查询:千万级别数据 = 100-1000毫秒
无索引:百万条数据 = 1000毫秒+

非插入更新删除操作:同查询

注:A query is equivalent to a full table scan

The efficiency improvement of insert operation mainly includes3个方面

  1. Use bulk insertsql语句,而不要用forInsert one by one in a loop

  2. Wrap all insert statements with transactions,Instead of starting a transaction for every query

  3. When inserting, try to ensure that the order of the inserted items is inserted in the increasing order of the index,This avoids frequent index adjustments

  4. The total amount of data inserted should not be less than the sizeinnodb_log_buffer_sizeThe size of this option,Exceeding this size will result in frequent disk memory switching.Transaction commits can be inserted in batches

JMeterIn the process of cloud pressure measurement, there will inevitably be network consumption of public network bandwidth,How to avoid the corresponding situation?

将 JMeter Running on another cloud machine,通过私网 IP Connect to stress test.

  

原网站

版权声明
本文为[westbrook gorilla]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101459003155.html