SpringBoot-嵌入式容器

qshy
发布于 2022-4-25 22:16
浏览
0收藏

作者 | springboot葵花宝典
来源 | 今日头条

SpringBoot支持嵌入式容器(比如:Apache Tomcat, Eclipse Jetty, RedHat Undertow),默认情况下使用tomcat.

1.修改Tomcat版本

SpringBoot 2.6.3版本默认采用的是*Apache Tomcat/9.0.56版本

日志显示:

INFO 25080 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]

修改了pom.xml

<properties>
    <tomcat.version>9.0.58</tomcat.version>
</properties>

后重新运行

INFO 3840 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.58]

2.修改端口号和web路径

2.1 通过配置文件修改端口号和web路径

2.1.1 application.properties和application.yml的异同

在我们创建SpringBoot项目的时候,会在/main/resources/文件夹下新建application.properties配置文件,但是我们经常会把 application.properties修改成application.yml文件,这两种格式存在区别如下:

格式区别:

  • properties配置文件通过**=**进行赋值,没有层次感
  • yml配置文件通过key:value分层进行赋值,比较有层次感

执行顺序区别:

如果properties配置文件和yml配置文件同时存在,yml配置会先加载并且后加载的properties文件会覆盖yml配置文件

示例:

application.properties配置文件:

server.port=8889

application.yml配置文件:

server:
  port: 8899
  servlet:
    context-path: /springweb

两个文件并存情况下,运行项目结果:

INFO 968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8889 (http) with context path '/springweb'

访问页面http://localhost:8889/springweb/SpringBoot-嵌入式容器-鸿蒙开发者社区

配置相同的情况下application.properties覆盖application.yml

2.1.2通过WebServerFactoryCustomizer进行修改

在SpringBoot1.x的时候使用的是EmbeddedServletContainerCustomizer到SpringBoot2.x后WebServerFactoryCustomizer替代了EmbeddedServletContainerCustomizer。

SpringBoot1.x EmbeddedServletContainerCustomizer

 @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                //设置端口
                container.setPort(8883);
                container.setContextPath("/springweb");
            }
        };
    }

SpringBoot2.x WebServerFactoryCustomizer

package com.learn.springboot.springbootpart2.config;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @program: spring-boot-part2
 * @package: com.learn.springboot.springbootpart2.config
 * @classname: PortConfig
 * @description: 端口和web修改
 * @author: zbb
 */
@Configuration
public class PortConfig {
    
    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer(){
        return  new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
            @Override
            public void customize(ConfigurableServletWebServerFactory factory) {
                //设置web路径
                factory.setContextPath("/webpath");
                //设置端口
                factory.setPort(8888);
            }
        };
    }
}

运行结果为:

SpringBoot-嵌入式容器-鸿蒙开发者社区

 

2.1.3 通过WebServerFactoryCustomizer进行修改

**SpringBoot1.x EmbeddedServletContainerFactory **

  @Bean  
    public EmbeddedServletContainerFactory servletContainer() {  
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();  
        tomcat.setPort(8879);  
        tomcat.setContextPath("/webserver");  
        return tomcat;  
    }  

SpringBoot2.X ServletWebServerFactory

@Bean
    public ServletWebServerFactory  servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() ;
        tomcat.setPort(8879);
        tomcat.setContextPath("/webserver");
        return tomcat;
    }

2.2 通过ServletWebServerFactory类优化Tomcat

**SpringBoot1.x EmbeddedServletContainerFactory **

@Bean  
public EmbeddedServletContainerFactory servletContainer() {  
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {  
        @Override  
        protected void postProcessContext(Context context) {  
           // ...  
        }  
    };   
    return tomcat;  
}  

SpringBoot2.X ServletWebServerFactory

@Bean
    public ServletWebServerFactory  servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                // ...  
            }
        };
        //tomcat.setPort(8879);
        //tomcat.setContextPath("/webserver");
        return tomcat;
    }

2.2.1开启HTTPS

SpringBoot默认端口是8080的HTTP服务,通过配置可以开启HTTPS服务在application.yml文件中配置

server:
  port: 8843
  servlet:
    context-path: /springweb
  ssl:
    key-store: tomcat.keystore
    key-store-password: password
    key-store-type: JKS
    key-alias: tomcat

tomcat.keystore生成步骤:

1):打开cmd输入命令

keytool -genkey -alias tomcat -keyalg RSA -keystore D:\software\file\java\myfirst\tomcat.keystore

SpringBoot-嵌入式容器-鸿蒙开发者社区

2)将tomcat.keystore放到**/spring-boot-part2**文件夹下

运行结果为:SpringBoot-嵌入式容器-鸿蒙开发者社区

SpringBoot不支持通过application.yml同时支持HTTP和HTTPS

可以在application.yml设置HTTPS,HTTP在代码中添加来实现。

@Configuration
public class PortConfig {
    @Bean
    public ServletWebServerFactory  servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(connector());
        return tomcat;
    }
    @Bean
    public Connector connector(){
        Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8888);
        connector.setURIEncoding("UTF-8");
        connector.setMaxPostSize(8192);
        connector.setSecure(false);
        connector.setRedirectPort(8843);
        return connector;
    }
}

这个时候访问http://localhost:8888/springweb就可以自动转到https://localhost:8843/springweb

2.3 SpringBoot的容器变更

修改pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

config配置文件

 @Bean
    public ServletWebServerFactory  jettyservletContainer() {
        JettyServletWebServerFactory container = new JettyServletWebServerFactory();
        container.setPort(8081);
        // Context Path
        container.setContextPath("/jettboot");
        return container;
    }

log日志显示:

 INFO 4560 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 8081 (http/1.1) with context path '/jettboot'

运行http://localhost:8081/jettboot/

收藏
回复
举报
回复
    相关推荐