码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-05 01:13:38      阅读:725      评论:0      收藏:0      [点我收藏+]

标签:ret   text   star   日志格式   throw   configure   mon   val   bst   

  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种方式

标签:ret   text   star   日志格式   throw   configure   mon   val   bst   

原文地址:https://www.cnblogs.com/javasl/p/11966660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!