【发布时间】:2020-11-13 04:06:29
【问题描述】:
我的项目要求是使用除 tomcat 之外的其他服务器?怎么能 我们从 Spring Boot 应用程序中更改嵌入式服务器?
【问题讨论】:
标签: java spring spring-boot tomcat
我的项目要求是使用除 tomcat 之外的其他服务器?怎么能 我们从 Spring Boot 应用程序中更改嵌入式服务器?
【问题讨论】:
标签: java spring spring-boot tomcat
您需要更新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")
}
【讨论】:
你必须从 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 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')
【讨论】:
Spring-Boot 中的默认Embedded Web Servers 是Tomcat,但您可以轻松地将其更改为其他人。
首先,你需要排除 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>
【讨论】: