gyorou
gyorou

Reputation: 51

In Spring Boot, How to assign the exact same random port to another property in configure

I am running spring boot with grpc-spring-boot-starter as a grpc-server and also configured as an eureka client.

Since I'm trying to launch more instances later, i want to assign a random port for my grpc service so I made grpc.port=0

The problem is, I also want eureka.instance.nonSecurePort to be the exact same port as grpc.port.Here is my configure.

grpc:
  port: 0

eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
  application:
    name: grpc-service
  instance:
    securePortEnabled: false
    nonSecurePort: ${grpc.port}
    instance_id: ${spring.application.name}:${spring.application.instance_id:${random.value}}

The problem is, I always get eureka.instance.nonSecurePort to be 0 which is not what I want. I know the random port is assigned during runtime. However are there anyway to make the grpc.port and eureka.instance.nonSecurePort always the same after spring boot assigned the random port?

Upvotes: 2

Views: 1115

Answers (1)

Jonathan JOhx
Jonathan JOhx

Reputation: 5968

You can put random port following an interval by following way:

grpc:
  port: ${random.int[9000,9900]}

eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
  application:
    name: grpc-service
  instance:
    securePortEnabled: false
    nonSecurePort: ${grpc.port}

Upvotes: 4

Related Questions