springboot tomcat embedded container 3 can be modified in a manner by tomcat.
(1) Modify application.properties file attributes, such as:
server.port=8081 server.address=127.0.0.1 server.tomcat.accesslog.enabled=true server.tomcat.accesslog.directory=d\:/springboot/logs
Port: 8081
Binding IP: The above configuration can only enter localhost in the browser: 127.0.0.1 8081 or visit
Start tomcat access log
tomcat access log path: D: \ springboot \ logs \ access_log.2019-12-05.log, (2019-12-05 running time)
Other configurations can look at the source code: org.springframework.boot.autoconfigure.web.ServerProperties
(2) achieve WebServerFactoryCustomizer interface and spring fitted into a container, as follows:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot_web</artifactId> <version>1.0.0</version> <name>springboot_web</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
MyWebServerFactoryCustomizer.java
package com.edu.spring.springboot; import java.io.File; import org.apache.catalina.connector.Connector; import org.apache.catalina.valves.AccessLogValve; import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.stereotype.Component; @Component public class MyWebServerFactoryCustomizer implementsWebServerFactoryCustomizer <TomcatServletWebServerFactory> { @Override public void Customize (TomcatServletWebServerFactory Factory) { factory.setPort ( 8888); // Tomcat port factory.setBaseDirectory ( new new File ( "D: / TEMP / Tomcat")); // cache path factory. addContextValves (getLogAccessLogValue ()); // set the log factory.addConnectorCustomizers ( new new MyTomcatConnectorCustomizer ()); // Tomcat custom access connections } Private AccessLogValve getLogAccessLogValue () { AccessLogValve log = new newAccessLogValve (); log.setDirectory ( "D: / TEMP / logs"); // log path log.setEnabled ( to true ); // Enable Logging log.setPattern ( "Common"); // Input log format log.setPrefix ( "springboot-Access-log"); // log name log.setSuffix ( "TXT."); // log suffix return log; } } class MyTomcatConnectorCustomizer the implements TomcatConnectorCustomizer { @Override public void Customize (Connector Connector) { Http11NioProtocol Protocal =(Http11NioProtocol) connector.getProtocolHandler (); protocal.setMaxConnections ( 20000); // Maximum number of connections protocal.setMaxThreads (500); // maximum number of threads } }
UserController.java
package com.edu.spring.springboot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping("/user/home") public String home(){ System.out.println("============home============="); return "user home1"; } }
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
Start your browser and enter: http://192.168.43.53:8888/user/home
(3) create TomcatServletWebServerFactory of bean, as follows:
WebServerConfiguration.java
package com.edu.spring.springboot; import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.apache.catalina.valves.AccessLogValve; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory; import org.springframework.context.annotation.Bean; importorg.springframework.http.HttpStatus; @SpringBootConfiguration public class WebServerConfiguration { @Bean public AbstractServletWebServerFactory createServletWebServerFactory () { TomcatServletWebServerFactory Factory = new new TomcatServletWebServerFactory (); factory.setPort ( 10008); // Tomcat port factory.addErrorPages ( new new the ErrorPage (HttpStatus. NOT_FOUND, "/ 404.html")); // set the status code corresponds to an error page factory.addContextValves (getLogAccessLogValue ()); // set the log factory.addInitializers ( new newMyServletContext ()); // perform initialization container tomcat return Factory; } Private AccessLogValve getLogAccessLogValue () { AccessLogValve log = new new AccessLogValve (); log.setDirectory ( "D: / TEMP / logs"); // log path log. setEnabled ( to true ); // enable logging log.setPattern ( "the Common"); // input log formats log.setPrefix ( "springboot-Access-log"); // log name log.setSuffix ( "txt."); // log suffix return log; } } class MyServletContext implements ServletContextInitializer{ @Override public void onStartup(ServletContext servletContext) throws ServletException { // TODO Auto-generated method stub System.out.println("------------servletContext start------------"); } }
Start your browser and enter: HTTP: // localhost: 10008 / the User / Home