(023)Spring Boot之修改内嵌tomcat的3种方式

Posted 明月之诗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(023)Spring Boot之修改内嵌tomcat的3种方式相关的知识,希望对你有一定的参考价值。

  springboot内嵌了tomcat容器,可以通过3中方式来修改tomcat。

(1)修改application.properties文件中属性,比如:

server.port=8081
server.address=127.0.0.1
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=d\\:/springboot/logs

  端口:8081

  绑定IP:上面的配置只能在浏览器输入localhost:8081或者127.0.0.1访问

  启动tomcat的访问日志

  tomcat访问日志的路径:D:\\springboot\\logs\\access_log.2019-12-05.log,(2019-12-05是运行时间)

  其他配置可以看源码:org.springframework.boot.autoconfigure.web.ServerProperties

(2)实现WebServerFactoryCustomizer接口,并装配到spring容器中,如下:

  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>
View Code

  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 implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.setPort(8888);//tomcat端口
        factory.setBaseDirectory(new File("d:/temp/tomcat"));//缓存路径
        factory.addContextValves(getLogAccessLogValue());//设置日志
        factory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());//tomcat自定义访问连接
    }
    
    private AccessLogValve getLogAccessLogValue(){
        AccessLogValve log=new AccessLogValve();
        log.setDirectory("d:/temp/logs");//日志路径
        log.setEnabled(true);//启用日志
        log.setPattern("common");//输入日志格式
        log.setPrefix("springboot-access-log");//日志名称
        log.setSuffix(".txt");//日志后缀
        return log;
    }

}

class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer{

    @Override
    public void customize(Connector connector) {
        Http11NioProtocol protocal=(Http11NioProtocol)connector.getProtocolHandler();
        protocal.setMaxConnections(20000);//最大连接数
        protocal.setMaxThreads(500);//最大线程数
    }
    
}
View Code

  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";
    }

}
View Code

  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); 
    }
} 
View Code

  启动,浏览器输入:http://192.168.43.53:8888/user/home

(3)创建TomcatServletWebServerFactory的bean,如下:

  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;
import org.springframework.http.HttpStatus;

@SpringBootConfiguration
public class WebServerConfiguration { 
 
    @Bean 
    public AbstractServletWebServerFactory createServletWebServerFactory(){
        TomcatServletWebServerFactory factory=new TomcatServletWebServerFactory();
        factory.setPort(10008);//tomcat端口
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404.html"));//设置状态码对应的错误页面
        factory.addContextValves(getLogAccessLogValue());//设置日志
        factory.addInitializers(new MyServletContext());//tomcat容器初始化时执行
        return factory; 
    }
    
    private AccessLogValve getLogAccessLogValue(){
        AccessLogValve log=new AccessLogValve();
        log.setDirectory("d:/temp/logs");//日志路径
        log.setEnabled(true);//启用日志
        log.setPattern("common");//输入日志格式
        log.setPrefix("springboot-access-log");//日志名称
        log.setSuffix(".txt");//日志后缀
        return log;
    }
}

class MyServletContext implements ServletContextInitializer{

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("------------servletContext start------------");
    }

    
}
View Code

  启动,浏览器输入:http://localhost:10008/user/home

 

 

以上是关于(023)Spring Boot之修改内嵌tomcat的3种方式的主要内容,如果未能解决你的问题,请参考以下文章

023-Spring Boot 服务的注册和发现

Spring Boot项目打包方式为war

Spring Boot 内嵌容器Undertow取代tomcat

Spring Boot启动过程:Spring Boot内嵌Tomcat启动

spring boot怎么部署到服务器

Spring Boot内嵌Tomcat原理