spring-bootmicroservicesspring-cloud-gateway

spring cloud gateway mvc always does automatic port assignment


Automatic port assignment is done even though I specify the port number in the API Gateway application

I am using spring cloud gateway mvc as api gateway. Although I specify server:port: 9090 in the app.yml file, I see that the port number changes automatically when I run the application. I think this problem is caused by spring cloud gateway mvc because when I make server.port=9090 in other services, that service is started on port 9090 without any problem. The only difference between this service and other services is that this service has 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


Solution

  • I don't know what the problem was, but tomcat was starting itself on port 0 (random) even though I specified the server port in the application file.

    I was able to solve the problem programmatically by creating a configuration class that sets the port number.

    @Configuration
    public class TomcatConfig {
    
        @Value("${server.port:8080}")
        private int serverPort;
    
        @Bean
        public ConfigurableServletWebServerFactory webServerFactory() {
            TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
            factory.setPort(serverPort);
            return factory;
        }
    }