I having a bunch of microservices which communicates with each other using RestTemplate
. All the communication of microservices is from API gateway.
I am doing as following,
public List<ServiceInstance> serviceInstancesByApplicationName(String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
//some Other logic
List<ServiceInstance> apigatewaymsInstanceList = discoveryClient.getInstances(apigatewaymsName);
ServiceInstance apigatewaymsInstance = apigatewaymsInstanceList.get(0);
//and
restTemplate.exchange(apigatewaymsInstance.getUri().toString() + plmpayloadprocessmsResource, HttpMethod.POST,
entity, String.class);
But here it appears like a hard code. Is there some another approach I am missing? What could be the best way ?
Likewise, I am asking is there any method available so that I can pass the name of application and eureka return me its full URI no need to do applicationgetInstaceId(0);
You can make use of the EurekaClient#getNextServerFromEureka. You might have to create the URI yourself but that should be trivial.
@Autowired
EurekaClient eurekaClient;
public void executeMethod() {
InstanceInfo loadBalancedInstance = eurekaClient.getNextServerFromEureka("myService", false);
//do work
}