【问题标题】:How can I change server in spring boot application?如何在 Spring Boot 应用程序中更改服务器?
【发布时间】:2020-11-13 04:06:29
【问题描述】:

我的项目要求是使用除 tomcat 之外的其他服务器?怎么能 我们从 Spring Boot 应用程序中更改嵌入式服务器?

【问题讨论】:

标签: java spring spring-boot tomcat


【解决方案1】:

您需要更新pom.xml,添加spring-boot-starter-jetty 的依赖项。另外,你需要在exclude默认添加spring-boot-starter-tomcat依赖。

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

在gradle中,

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

【讨论】:

    【解决方案2】:

    你必须从 starter 依赖中排除 tomcat:

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

    【讨论】:

      【解决方案3】:
      # Gradle way
      
           implementation 'org.springframework.boot:spring-boot-starter-tomcat'
           implementation 'org.springframework.boot:spring-boot-starter-jetty'
           modules {
            module("org.springframework.boot:spring-boot-starter-tomcat") {
                replacedBy("org.springframework.boot:spring-boot-starter-jetty", "Use Jetty instead of Tomcat")
            }
           }
      
      # Gradle Another Way
      
          compile('org.springframework.boot:spring-boot-starter-webflux') {
              exclude group: 'org.springframework.boot',
              module: 'spring-boot-starter-reactor-netty'
          }
          compile('org.springframework.boot:spring-boot-starter-tomcat')
      

      【讨论】:

        【解决方案4】:

        Spring-Boot 中的默认Embedded Web ServersTomcat,但您可以轻松地将其更改为其他人。

        首先,你需要排除 tomcat:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- Exclude the Tomcat dependency -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        

        然后你可以添加你想要的嵌入式网络服务器:

        <!-- Jetty Embedded Web Server -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency> 
        

        <!-- Undertow Embedded Web Server -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency> 
        

        【讨论】:

          猜你喜欢
          • 2016-12-18
          • 2021-08-20
          • 1970-01-01
          • 1970-01-01
          • 2019-08-06
          • 2015-11-30
          • 2014-02-25
          • 2019-07-03
          • 1970-01-01
          相关资源
          最近更新 更多