015-spring Boot customization and optimization of inline tomcat

Source: Internet
Author: User

One, embedded web container

See http://www.cnblogs.com/bjlhx/p/8372584.html

See the source code to provide the following three kinds:

    

Second, customized optimization tomcat2.1, configuration file configuration

  Configuring Tomcat with Application.properties

Port configuration: server.port=10001
Address configuration: server.adress=192.168.1.1

See the Tomcat variables through serverproperties and enter to configure as follows

To enable logging configuration:

Server.tomcat.accesslog.enabled=trueserver.tomcat.accesslog.directory=d:/logs
Principle Analysis

Org.springframework.boot.autoconfigure.web.ServerProperties viewing in the Spring-boot-autoconfigure-1.5.9.release.jar package

You can view the properties shown above

2.2. Configure one by code mode-Implement Embeddedservletcontainercustomizer interface

embeddedservletcontainercustomizer→ three specific default implementations

    

1 "Code Style customization

Define a class Myembeddedservletcontainercustomizer implement Embeddedservletcontainercustomizer interface, you can

Set up a basic configuration

@Component  Public class Implements Embeddedservletcontainercustomizer {    @Override    publicvoid  Customize ( Configurableembeddedservletcontainer container) {        System.out.println (Container.getclass ());         =(tomcatembeddedservletcontainerfactory) container;        Factory.setport (8181);        Factory.setbasedirectory (new File ("D:/tmp/tomcat"));}    }
View Code

Setting up logs

 PackageCom.lhx.spring.springboot_web;ImportJava.io.File;ImportOrg.apache.catalina.valves.AccessLogValve;ImportOrg.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;ImportOrg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;Importorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;Importorg.springframework.stereotype.Component; @Component Public classMyembeddedservletcontainercustomizerImplementsEmbeddedservletcontainercustomizer {@Override Public voidCustomize (Configurableembeddedservletcontainer container) {System.out.println (Container.getclass ()); Tomcatembeddedservletcontainerfactory Factory=(tomcatembeddedservletcontainerfactory) container; Factory.setport (8181); Factory.setbasedirectory (NewFile ("D:/tmp/tomcat"));    Factory.addcontextvalves (Getaccesslogvalve ()); }        Privateaccesslogvalve getaccesslogvalve () {accesslogvalve log=NewAccesslogvalve (); Log.setdirectory ("D:/tmp/logs"); Log.setenabled (true); Log.setpattern ("Common"); Log.setprefix ("Spring-boot-lhx-access-log"); Log.setsuffix (". txt"); returnlog; }}
View Code

Setting the number of connections configuration, maximum thread

 PackageCom.lhx.spring.springboot_web;ImportJava.io.File;ImportOrg.apache.catalina.connector.Connector;ImportOrg.apache.catalina.valves.AccessLogValve;ImportOrg.apache.coyote.http11.Http11NioProtocol;ImportOrg.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;ImportOrg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;ImportOrg.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;Importorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;Importorg.springframework.stereotype.Component; @Component Public classMyembeddedservletcontainercustomizerImplementsEmbeddedservletcontainercustomizer {@Override Public voidCustomize (Configurableembeddedservletcontainer container) {System.out.println (Container.getclass ()); Tomcatembeddedservletcontainerfactory Factory=(tomcatembeddedservletcontainerfactory) container; Factory.setport (8181); Factory.setbasedirectory (NewFile ("D:/tmp/tomcat"));                Factory.addcontextvalves (Getaccesslogvalve ()); Factory.addconnectorcustomizers (Newmytomcatconnectorcustomizers ()); }        Privateaccesslogvalve getaccesslogvalve () {accesslogvalve log=NewAccesslogvalve (); Log.setdirectory ("D:/tmp/logs"); Log.setenabled (true); Log.setpattern ("Common"); Log.setprefix ("Spring-boot-lhx-access-log"); Log.setsuffix (". txt"); returnlog; }        }classMytomcatconnectorcustomizersImplementstomcatconnectorcustomizer{@Override Public voidCustomize (Connector Connector) {Http11nioprotocol Protocol=(Http11nioprotocol) Connector.getprotocolhandler (); Protocol.setmaxconnections (2000); Protocol.setmaxthreads (500); }    }
View Code2.5. Configure by Code mode two-default configuration class mode
 PackageCom.lhx.spring.springboot_web;Importorg.springframework.boot.SpringBootConfiguration;Importorg.springframework.boot.context.embedded.EmbeddedServletContainerFactory;Importorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;ImportOrg.springframework.boot.web.servlet.ErrorPage;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.http.HttpStatus; @SpringBootConfiguration Public classwebservletconfiguration {@Bean Publicembeddedservletcontainerfactory createembeddedservletcontainerfactory () {Tomcatembeddedservletcontainerfacto Ry Factory=Newtomcatembeddedservletcontainerfactory (); Factory.setport (1008); Factory.adderrorpages (NewErrorPage (Httpstatus.not_found, "/404.html")); returnFactory; }}
View Code

Add initialization

        Factory.addinitializers (ServletContext),{            System.out.println ("----------ServletContext Startup----------");        });

You can get the ServletContext information at this time, you can add filter, set attribute, etc.

Source Address: Https://github.com/bjlhx15/spring-boot.git

015-spring Boot Custom and optimized inline tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.