spring-boot 开启Gzip解决办法

spring boot 加入gizp 

如果采用jar 的方式不是spring boot 项目:只需在配置文件中开启gzip 压缩就可以了

server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true
server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain

但是如果要部署到tomcat,需要如下对tomcat 安装目录下的conf/server.xml 作如下修改:

	   
			   
	 
	 
	 

原理:

spring 以jar 的方式发布是采用内置的tomcat运行的, 内部进行一系列的TomcatConnectorCustomizer,EmbededContainerCustomizer 对象的自定义

@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer servletContainer) {
            ((TomcatEmbeddedServletContainerFactory) servletContainer).addConnectorCustomizers(
                    new TomcatConnectorCustomizer() {
                        @Override
                        public void customize(Connector connector) {
                            AbstractHttp11Protocol httpProtocol = (AbstractHttp11Protocol) connector.getProtocolHandler();
                            httpProtocol.setCompression("on");
                            httpProtocol.setCompressionMinSize(256);
                            String mimeTypes = httpProtocol.getCompressableMimeTypes();
                            String mimeTypesWithJson = mimeTypes + "," + MediaType.APPLICATION_JSON_VALUE;
                            httpProtocol.setCompressableMimeTypes(mimeTypesWithJson);
                        }
                    }
            );
        }
    };
}

如果采用自己安装的tomcat部署, 就要手动开启这些配置就OK了!!!






你可能感兴趣的:(spring-boot 开启Gzip解决办法)