Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

I built a blog website based on SpringBoot+Spring Security+MyBatis+MySQL+Redis+Thymeleaf. Symptom After the personal cloud server goes online, the server access is slow. Personal server is 1 core 2G, 1M broadband, although the server is low configuration, but through optimizing code, middleware and other means, to improve performance. I will talk about personal website feature development and some performance optimization experience.

This article focuses on other aspects of optimization

First, server optimization

GZIP(GNU- ZIP) is a compression technology. The page size can be reduced by 30% or less after GZIP compression, resulting in a much faster browsing time. GZIP data compression can be used to compress front-end static resources, server resources, etc. After compression, access speed will be increased, configured in the HTTP module of Nginx

#gzip on; Enable Gzip Gzip on; Gzip_min_length 1K; gzip_min_length 1K # set the size of gzip request memory, which applies gzip_buffers in multiples of the block size416k; # is used to identify HTTP versions. Earlier browsers do not support gzip compression, and users will see garbled characters, so this option is added to support earlier versions1.0; # set the gzip compression level, the lower the level, the faster the file compression ratio is smaller, vice versa, the slower the file compression ratio is larger2; Gzip_types text/plain Application /x-javascript text/ CSS application/ XML text/javascript gzip_types text/plain Application /x-javascript application/x-httpd-php image/jpeg image/gif image/png; For Squid, on is added to the Header"Vary: Accept-Encoding"gzip_vary off; # IE6 is not Gzip friendly, disable gzip_disable"MSIE [1-6]\.";
Copy the code

Second, code optimization

After previous optimization experience, such as reducing the interaction with the database, then, similarly, using redis in the system, can also reduce the interaction with redis before the code

/** * get the content of the article in Redis */
public Object getArticleOnRedis(String key, Object field) {
    boolean articleExist = hashRedisServiceImpl.hasHashKey(key, field);
    if (articleExist) {
        return hashRedisServiceImpl.get(key, field);
    }
    return null;
}
Copy the code

Optimized code

/** * get the content of the article in Redis */
public Object getArticleOnRedis(String key, Object field) {
    // Reduce interaction
    return hashRedisServiceImpl.get(key, field);
}
Copy the code

Before obtaining the value, it is necessary to determine whether the key exists or not. If the key exists, it is searched according to the key and returns the value. If it does not, it directly returns null. Although the judgment logic is written in detail, the code is written in this way, but it is more bloated. It simply searches directly from the key and returns directly, reducing the judgment and reducing the interaction with Redis.

3. Enable thymeleaf cache

Thymeleaf is a template engine that has caching enabled, meaning that once a template is loaded it will not be loaded again. Once the system is online, there is no need to keep loading templates, so just open the template engine cache.

# Templates reloading during development
spring.thymeleaf.prefix=file:src/main/resources/templates/
spring.thymeleaf.cache=false

# Static resources reloading during development
spring.resources.static-locations=file:src/main/resources/static/
spring.resources.cache-period=0
Copy the code

Modify the maximum number of active redis connections

# redis configuration spring. Redis. Database =0
spring.redis.port=6379
spring.redis.pool.max-active=8000
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
Copy the code

When I tried to use Jmeter for pressure measurement, too many visits would crush Redis, increase the maximum active number of connection pool, and effectively prevent downtime caused by excessive concurrent number to a certain extent.