I implemented request using Spring Cloud Feign client. I tried this:
Feign client:
@FeignClient(name = "mail-service")
public interface EmailClient {
@RequestMapping(method = RequestMethod.POST, value = "/register")
void setUserRegistration(RegisterUserDTO registerUserDTO);
@RequestMapping(method = RequestMethod.POST, value = "/password_reset")
void setUserPasswordReset(PasswordResetDTO passwordResetDTO);
}
Feign configuration:
@Configuration
public class LoadBalancerConfiguration {
@Bean
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return ServiceInstanceListSupplier.builder()
.withBlockingDiscoveryClient()
.withSameInstancePreference()
.withHealthChecks()
.build(context);
}
}
Request DTO:
@Getter
@Setter
public class PasswordResetDTO {
private int id;
}
Controller:
@Autowire
EmailClient emailClient;
@PostMapping("/dummy")
public ResponseEntity<?> test() {
RegisterUserDTO obj = new RegisterUserDTO();
obj.setId(12);
emailClient.setUserRegistration(obj);
return ok().build();
}
Feign configuration:
spring:
cloud:
loadbalancer:
ribbon:
enable: false
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
preferIpAddress: true
POM.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
I started 3 instances of endpoint services which are consumed as endpoint from Feign client in a round-robin order. So far so good, it's working as expected. But when I shutdown for example the first service instance the Feign client is not notified of this change and continues to send requests to the inactive service. Do you know how consumer service can be notified when endpoint service is shut down and out of the list of active services? I suppose that I need to configure some health checks?
Do you know how I can solve this issue?
Source code for testing the issue: https://github.com/rcbandit111/eureka-discovery-poc/tree/master
Ribbon - Load balancer
Out of the box it's providing you with round robin loadbalancing, but you can implement your own
@RibbonClient(even for a specific service) and design your custom loadbalancing for example based on eureka metadata. The loadbalancing happens on the client side.Feign - Http client
With
@FeignClientyou can rapidly develop clients for you other services (or services outside of your infrastructure). It is integrated with ribbon and eureka so you can refer to your services@FeignClient(yourServiceNameInEureka)and what you end up with is a client which loadbalances between the registered instances with your preferred logic. If you are using spring you can use the familiar@RequestMappingannotation to describe the endpoint you are using.I have mentioned below example with
RandomRule. Also can check Github here for the source.ALso, See the Use of Feign to setup High Availability Load Balancer