0

I've been looking for a way to change the url my microservice is depending on from one environment to another, they told me that I should use spring cloud, and there is currently an application.yml that is used for deployment in openshift, but my knowledge of Spring Cloud is limited and I don't know how to inject the value of the application.yml URL into my java program.

String uri = "http://127.0.0.1:8080/route";

I am looking to change this variable depending on whether it is local, or in development

Why? For communication between microservices

private RestTemplate call = new RestTemplate();
Arrays.asList(call.getForObject(uri, Object[].class));

In local need: "http://127.0.0.1:8080/route";

In dev need : "http://www.myurl.com/route"

I hope I explained myself well

Thanks.

Armine
  • 1,555
  • 2
  • 23
  • 37
A L
  • 365
  • 2
  • 14
  • Are you using Openshift for Java Deployment? If so, please refer to this: https://docs.openshift.com/dedicated/3/dev_guide/environment_variables.html – Bryan Jan 10 '20 at 08:29
  • Yes, it is not my choice, and for deployments I need a Config file to work in Openshift – A L Jan 10 '20 at 08:31
  • @Bryan mmm i need a enviroment for java, not for Pods, that configuration is done. – A L Jan 10 '20 at 08:33
  • 2
    Are you familiar with environment variable? That's how you can change settings between local, dev, prod. You can also look at spring profile (https://www.baeldung.com/spring-profiles) – Ken de Guzman Jan 10 '20 at 09:43
  • @KendeGuzman Yes, but i dont know how can automate my microservice for use dev profile or local profile, i saw can change profile with a maven command, but i dont know if is the best solution. I see this post [link](https://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties), i think is a one solution – A L Jan 10 '20 at 10:11
  • 1
    Use Spring profiles. When running local use the local profile in your environment use another. Just specify the profile when starting the service using `--spring.profiles.active=prod` and make sure that there is an `application-prod.properties`. Please don't confuse maven profiles and spring profiles, those are different things!. – M. Deinum Jan 10 '20 at 10:41
  • Other option is if you are using maven. You can use maven filters in order to build the artifact for the environment you need – Angelo Immediata Jan 10 '20 at 12:11

2 Answers2

-1

Ok, i think have the solution for my problem. As I am using Openshift for deployments, and the local configuration for something else, I have used Spring Cloud with a configuration server, making it use one configuration locally, and in development another one overwriting the local configuration.

This solved my problem, thanks to other guys to answer with any solutions.

A L
  • 365
  • 2
  • 14
-2

As "M. Deinum" and "Ken de Guzman" have already mentioned, use the "Spring Profiles" approach.

See here the docs, which explain the functionality:

olibur
  • 325
  • 3
  • 13