如何配置Spring Boot项目禁用Web服务及Tomcat网络暴露功能?
如何创建无Web服务暴露的Spring Boot项目
当然可以!完全能打造一个只处理Kafka消息、SQL查询和WebFlux客户端请求,不暴露任何REST API也不启动Tomcat的Spring Boot项目。下面是具体的实现步骤:
1. 精简项目依赖,移除Web容器相关包
Spring Boot默认如果引入spring-boot-starter-web会自动启动Tomcat,所以我们要避免引入这个依赖,改用核心starter加上你需要的功能依赖:
Maven 配置示例(pom.xml)
<dependencies> <!-- 核心Spring Boot基础依赖,不含任何Web容器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Kafka消息处理依赖 --> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <!-- SQL查询支持(这里用Spring Data JPA,你也可以用JDBC模板) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 数据库驱动,根据你的数据库选择,比如MySQL --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- WebFlux客户端(仅保留客户端功能,排除内置Netty服务器) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-reactor-netty</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
Gradle 配置示例(build.gradle)
dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.kafka:spring-kafka' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.mysql:mysql-connector-j' // 引入WebFlux客户端,排除服务器依赖 implementation('org.springframework.boot:spring-boot-starter-webflux') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-reactor-netty' } }
2. 禁用Web服务器自动配置(双重保险)
如果担心不小心引入了Web相关依赖导致服务器启动,可以在主启动类上明确排除Servlet和Reactive类型的Web服务器自动配置:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerAutoConfiguration; @SpringBootApplication(exclude = { ServletWebServerAutoConfiguration.class, ReactiveWebServerAutoConfiguration.class }) public class NonWebSpringBootApp { public static void main(String[] args) { SpringApplication.run(NonWebSpringBootApp.class, args); } }
3. 验证配置是否生效
启动项目后,观察日志:
- 不会出现
Tomcat started on port(s): ...或者Netty started on port(s): ...这类服务器启动的日志 - 尝试用浏览器或curl访问任何端口,都会提示无法连接,说明没有Web服务在运行
4. 编写业务逻辑
现在你可以正常添加所需的功能代码了,比如:
Kafka消息监听示例
import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class KafkaMessageHandler { @KafkaListener(topics = "your-topic", groupId = "your-group-id") public void processMessage(String message) { System.out.println("Received Kafka message: " + message); // 这里可以执行SQL查询或者调用WebFlux客户端请求 } }
WebFlux客户端请求示例
import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @Service public class RemoteApiService { private final WebClient webClient; public RemoteApiService(WebClient.Builder webClientBuilder) { this.webClient = webClientBuilder.baseUrl("https://api.example.com").build(); } public Mono<String> fetchRemoteData() { return webClient.get() .uri("/endpoint") .retrieve() .bodyToMono(String.class); } }
SQL查询示例(Spring Data JPA)
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { User findByEmail(String email); }
这样你的项目就能完美运行:处理Kafka消息、执行SQL操作、发起WebFlux客户端请求,全程不会暴露任何Web服务端口。
内容的提问来源于stack exchange,提问作者Peter Penzov




