How to Congifure Dynamic port in spring boot Microservice

 STEP 1; POJO Class

public class ExchangeValue {


private Long id;

private String from;

private String to;

private BigDecimal conversionMultiple;

private int port;

public ExchangeValue() {

}


public ExchangeValue(Long id, String from, String to, BigDecimal conversionMultiple) {

super();

this.id = id;

this.from = from;

this.to = to;

this.conversionMultiple = conversionMultiple;

}


public Long getId() {

return id;

}


public String getFrom() {

return from;

}


public String getTo() {

return to;

}


public BigDecimal getConversionMultiple() {

return conversionMultiple;

}


public int getPort() {

return port;

}


        public void setPort(int port) {


this.port = port;

}

}


STEP 2; Controller Class


@RestController

public class CurrencyExchangeController {

@Autowired

private Environment environment;


@GetMapping("currency-exchange/from/{from}/to/{to}")

public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to) {

ExchangeValue exchangeValue = new ExchangeValue(1L, from, to, BigDecimal.valueOf(81));

exchangeValue.setPort(Integer.parseInt(environment.getProperty("local.server.port")));

return exchangeValue;

}

}


STEP 3: Main Class


@SpringBootApplication

public class CurrencyExchangeServiceApplication {


public static void main(String[] args) {

SpringApplication.run(CurrencyExchangeServiceApplication.class, args);

}


}


STEP 4: Application properties file

spring.application.name=currency-exchange-service

server.port=8000


This is the local port information but if we provide port number while

running as VM argument than it will be overrise like given below






Comments

Popular posts from this blog

Microservice with Java Spring Cloud

STEP 10 Configure Porfile for Limit service

STEP 9 Connect Limit service to Spring Cloud Config Server