spring boot设置Gzip

it2024-03-12  85

随着数据量的陡增,系统服务有很多接口就爆露出问题了,F12查看耗时,发现有地方前端数据下载就耗时一点几秒,一看数据量2M,吓死个人啊,于是就想到了Gzip。

为了减少数据在网络中的传输量,从而减少传输时长,增加用户体验,浏览器大都是支持Gzip压缩技术的,http的请求头 Accept-Encoding:gzip, deflate 就表示这次请求可以接受Gzip压缩后的数据,图片不要进行压缩,因为图片完全可以在项目开发中使用压缩后的图片。压缩会有一定的CPU性能损耗。

下面介绍几种 Gzip压缩方式

1.SpringBoot开启Gzip压缩

在application.properties中加入如下配置:

server: compression: #开启gzip压缩,返回内容大于2k的才会进行压缩 enabled: true mime-types: application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain min-response-size: 2048

压缩前:25.3kb,50.0kb,37.5kb,5.1kb,34.7kb 压缩后:6.4kb,11.7kb,8.3kb,1.3kb,34.7kb 压缩后可看到文件有4倍左右的差距,能大大减少网络传输量,页面加载速度加快

2.Tomcat开启Gzip压缩

tomcat中使用gzip需要进行配置,在server.xml中,在Connector标签中加入如下属性

 

compression="on"  compressionMinSize="2048"  compressableMimeType="text/html,text/css,text/javascript"

3.Nginx开启Gzip压缩

gzip on; gzip_min_length 1k; gzip_buffers 4 16k; #gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary off;

重载nginx即可 第1行:开启Gzip 第2行:不压缩临界值,大于1K的才压缩,一般不用改 第3行:buffer,不用改 第4行:用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1 第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧 第6行:进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了 第7行:跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧

4.GZIPOutputStream,GZIPInputStream压缩与解压

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.codec.binary.StringUtils;   public class GZIPUtils {       public static final String GZIP_ENCODE_UTF_8 = "UTF-8";     public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";       /**      * 字符串压缩为GZIP字节数组      * @param str      * @return      */     public static byte[] compress(String str) {         return compress(str, GZIP_ENCODE_UTF_8);     }       /**      * 字符串压缩为GZIP字节数组      * @param str      * @param encoding      * @return      */     public static byte[] compress(String str, String encoding) {         if (str == null || str.length() == 0) {             return null;         }         ByteArrayOutputStream out = new ByteArrayOutputStream();         GZIPOutputStream gzip;         try {             gzip = new GZIPOutputStream(out);             gzip.write(str.getBytes(encoding));             gzip.close();         } catch (IOException e) {             e.printStackTrace();         }         return out.toByteArray();     }       /**      * GZIP解压缩      * @param bytes      * @return      */     public static byte[] uncompress(byte[] bytes) {         if (bytes == null || bytes.length == 0) {             return null;         }         ByteArrayOutputStream out = new ByteArrayOutputStream();         ByteArrayInputStream in = new ByteArrayInputStream(bytes);         try {             GZIPInputStream ungzip = new GZIPInputStream(in);             byte[] buffer = new byte[256];             int n;             while ((n = ungzip.read(buffer)) >= 0) {                 out.write(buffer, 0, n);             }         } catch (IOException e) {             e.printStackTrace();         }         return out.toByteArray();     }       /**      * 解压并返回String      * @param bytes      * @return      */     public static String uncompressToString(byte[] bytes) {         return uncompressToString(bytes, GZIP_ENCODE_UTF_8);     }       /**      * 解压      * @param bytes      * @param encoding      * @return      */     public static String uncompressToString(byte[] bytes, String encoding) {         if (bytes == null || bytes.length == 0) {             return null;         }         ByteArrayOutputStream out = new ByteArrayOutputStream();         ByteArrayInputStream in = new ByteArrayInputStream(bytes);         try {             GZIPInputStream ungzip = new GZIPInputStream(in);             byte[] buffer = new byte[256];             int n;             while ((n = ungzip.read(buffer)) >= 0) {                 out.write(buffer, 0, n);             }             return out.toString(encoding);         } catch (IOException e) {             e.printStackTrace();         }         return null;     }       public static void main(String[] args) {         String str = "%5B%7B%22lastUpdateTime%22%3A%222011-10-28+9%3A39%3A41%22%2C%22smsList%22%3A%5B%7B%22liveState%22%3A%221";         System.out.println("原长度:" + str.length());         System.out.println("压缩后字符串:" + GZIPUtils.compress(str).toString().length());         System.out.println("解压缩后字符串:" + StringUtils.newStringUtf8(GZIPUtils.uncompress(GZIPUtils.compress(str))));         System.out.println("解压缩后字符串:" + GZIPUtils.uncompressToString(GZIPUtils.compress(str)));     } }

最新回复(0)