I have the following services:
Is there a way to get Client-service's ip so I can make requests to it from the Finder-service.
I know there is a way to find InstanceInfo from EurekaServer and I was thinking of making a controller in eureka server where you pass service id and get service's instance ip. This way Finder-service would only need to know service id and eureka ip which it knows because it is registered there.
Is there another solution which is cleaner than this?
To use client discovery you first need to enable by adding either @EnableEurekaClient
or @EnableDiscoveryClient
to your @SpringBootApplication
annotated class (or your specialized @Configuration
).
Next to make use of the resolution of the actual service instance to use you need to create a RestTemplate
which is load-balanced. This will add an interceptor which translates the service name into the ip-address / DNS name to send the request to (if multiple instances are found it will distribute the load between those instances).
To create a load balanced RestTemplate
add the @LoadBalanced
annotation to the configured RestTemplate
.
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder rtb) {
return rtb.build();
}
Now when doing a request through the RestTemplate
it will resolve the service name client-service
to the actual service instance to use. Without you having to do anything.