You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Cloud Gateway MVC指定端口后仍自动分配端口问题咨询

Spring Cloud Gateway MVC 端口自动变更问题排查与解决

问题描述

使用Spring Cloud Gateway MVC作为API网关,在application.yml中配置server.port=9090,但应用启动时端口自动变更为随机端口(如日志中的40141)。其他服务配置相同端口可正常启动,仅该网关服务出现此问题,怀疑由Spring Cloud Gateway MVC引起。

应用配置(application.yml)

server:
    port: 9090

spring:
  application:
    name: AuthService
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  datasource:
    username: root
    password: password
    url: jdbc:mysql://localhost:3306/user
    driver-class-name: com.mysql.cj.jdbc.Driver
  cloud:
    discovery:
      enabled: true
    gateway:
      mvc:
        routes: 
        -   id: CommentService
            uri: lb://CommentService
            predicates:
            - Path=/v1/comment/**
        -   id: DmService
            uri: lb://DmService
            predicates:
            - Path=/v1/dm/**
        -   id: LikeService
            uri: lb://LikeService
            predicates:
            - Path=/v1/like/**
        -   id: PostService
            uri: lb://PostService
            predicates:
            - Path=/v1/post/**
        -   id: FollowService
            uri: lb://FollowService
            predicates:
            - Path=/v1/following/**
        -   id: BlockService
            uri: lb://BlockService
            predicates:
            - Path=/v1/block/**
  web:
    resources:
      static-locations: file:/socimedia-static/         
  
logging:
  level:
    org:
      springframework:
        cloud:
          gateway: DEBUG
        security: DEBUG
        web: DEBUG

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka
  instance:
    prefer-ip-address: true

启动日志片段

2024-09-01T15:23:55.382+03:00  INFO 14149 --- [AuthService] [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2024-09-01T15:23:55.501+03:00  INFO 14149 --- [AuthService] [  restartedMain] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
2024-09-01T15:23:55.734+03:00  WARN 14149 --- [AuthService] [  restartedMain] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2024-09-01T15:23:55.809+03:00  INFO 14149 --- [AuthService] [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 40141 (http) with context path '/'
2024-09-01T15:23:55.812+03:00  INFO 14149 --- [AuthService] [  restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 40141

排查与解决方案

1. 确认端口占用情况

先排除端口被其他进程占用的可能,执行对应命令检查9090端口状态:

  • Windows:netstat -ano | findstr :9090
  • Linux/Mac:lsof -i :9090

若端口未被占用,继续排查其他原因。

2. 强制绑定Eureka注册端口

日志显示Eureka自动更新了端口,可通过配置强制指定注册端口,避免被覆盖:

eureka:
  instance:
    non-secure-port: 9090
    non-secure-port-enabled: true

3. 检查Spring Boot DevTools影响

日志中出现LiveReload server,说明引入了Spring Boot DevTools依赖。DevTools在部分场景下可能导致端口重置,可尝试:

  • 移除spring-boot-devtools依赖
  • 添加配置spring.devtools.restart.enabled=false关闭自动重启

4. 排查配置优先级冲突

检查是否存在以下场景覆盖了server.port配置:

  • 启动命令行参数(如--server.port=xxxx)
  • 环境变量(如SERVER_PORT)
  • 其他配置文件(如application-dev.yml、bootstrap.yml)

5. 追踪端口配置流程

添加更详细的日志,查看端口设置的具体过程:

logging:
  level:
    org.springframework.boot.web.server: DEBUG

启动后通过日志确认是哪个组件修改了端口配置。

6. 验证版本兼容性

Spring Cloud Gateway MVC对版本兼容性要求严格,确保Spring Cloud与Spring Boot版本匹配:

  • Spring Cloud 2023.0.x(Leyton)→ Spring Boot 3.2.x
  • Spring Cloud 2022.0.x(Kilburn)→ Spring Boot 3.1.x
  • Spring Cloud 2021.0.x(Jubilee)→ Spring Boot 2.6.x/2.7.x

版本不兼容可能导致配置失效或自动配置异常。

内容的提问来源于stack exchange,提问作者kkaranalbant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 23:59:51