使用Spring Boot配置Undertow网络服务器

x33g5p2x  于2022-09-30 转载在 Spring  
字(1.8k)|赞(0)|评价(0)|浏览(991)

默认情况下,Spring boot 使用 Tomcat 作为嵌入式 Web 服务器。 但是,如果您需要某些特定功能,还有其他可用的 Web 服务器。 在本教程中,我们将学习如何使用 Undertow 作为 Web 服务器。

添加 spring-boot-starter-undertow 依赖

您将需要更新 pom.xml 并为 spring-boot-starter-undertow 添加依赖项。 此外,您需要排除默认添加的 spring-boot-starter-tomcat 依赖项,如下所示:

<?xml version="1.0" encoding="UTF-8"?><project>
   <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-undertow</artifactId>
       
   </dependency>
</project>

如果您使用 Gradle 作为构建工具,则可以使用以下方法获得相同的结果:

configurations { 
    compile.exclude module: "spring-boot-starter-tomcat" 
} 
dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web:2.1.0.RELEASE") 
    compile("org.springframework.boot:spring-boot-starter-undertow:2.1.0.RELEASE") 
}

以编程方式配置 Undertow

您还可以通过 UndertowEmbeddedServletContainerFactory 以编程方式启动嵌入式 Undertow Web 服务器:

@Bean
  public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
    UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
    factory.addBuilderCustomizers(
        new UndertowBuilderCustomizer() {
          @Override
          public void customize(io.undertow.Undertow.Builder builder) {
            builder.addHttpListener(8080, "0.0.0.0");
          }
        });
    return factory;
  }

至于默认的 Web Server,您可以通过 application.properties 文件为 Undertow 配置自定义设置:

server.undertow.accesslog.enabled=true 
server.undertow.accesslog.dir=target/logs 
server.undertow.accesslog.pattern=combined 
server.compression.enabled=true 
server.compression.min-response-size=1

相关文章

微信公众号

最新文章

更多