简体   繁体   中英

JAVA SPRING BOOT - Swap content of environment variables at runtime

I built an application with java spring boot where I make a request via restTemplate for another microservice.

@Value ("$ {url.api.otherMicroservice}")
private String hostOtherMicroServico;



@Override
public Content findById (String id) {
    RestTemplate restTemplate = new RestTemplate ();
    final HttpEntity <String> httpRequest = new HttpEntity <> (null);
    return restTemplate.exchange (hostOtherMicroService + id, HttpMethod.GET, 
        httpRequest, new ParameterizedTypeReference <Content> () {}).getBody();
}

This content is in my application.properties statically. And the first thing that came to mind was to create an environment variable.

url.api.otherMicroservice = $ {URL_API_OUTROMICROSERVICO: https://application-domain/api/contents/}

Today my application is in kubernetes and I publish it in jenkins. I would like to know what are the alternatives you use to make it possible at runtime to change the information being passed as a variable to call the endpoint.

I am not able to add a comment...

Given your unique challenge that your endpoint changes... You have only a couple of choices...

Either refresh the endpoint from Db or whichever source is providing you with a new endpoint

Or (as other commenter says) utilize something like spring-cloud-config to do the same assuming you can do that in your environment...

You can implement a 'refresh' REST endpoint with spring-cloud-config to refresh your values conditionally...

Here is a resource for quick ref

https://www.devglan.com/spring-cloud/refresh-property-config-runtime

I hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM