3
I have a service structure as follows: server (Eureka), immovable (Eureka-client) and client (Eureka-client).
The customer service makes a request for immovable service, using feign. I own the Loadbalancer configured on both sides (property and client). If all services are running, the calls and Loadbalancer work correctly. However, I started doing the Circuitbreaker part (Hystrix).
I just uploaded the server service (Eureka) and the client service, to test the Circuitbreaker. When the customer service went to make a request for the property service, the fallback was activated correctly, however, the exception launched was different than expected. The exception I received was:
java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: imovel-service
I was hoping to get a 400+ exception. What I’m doing wrong?
server (Eureka Server)
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
server application.yml
server:
port: ${PORT:8080}
spring:
application:
name: eureka-service
boot:
admin:
url: http://localhost:8090
eureka:
instance:
hostname: localhost
leaseRenewalIntervalInSeconds: 10
client:
registerWithEureka: false
fetchRegistry: true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:
security:
enabled: false
CUSTOMER SERVICE:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@RibbonClient(name = "ribbon-configuration", configuration = RibbonConfiguration.class)
public class ClienteApplication {
public static void main(String[] args) {
SpringApplication.run(ClienteApplication.class, args);
}
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
@Configuration
public class FeignConfiguration {
int FIVE_SECONDS = 5000; // milliseconds
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public Request.Options options() {
return new Request.Options(FIVE_SECONDS, FIVE_SECONDS);
}
}
@FeignClient(value = "imovel-service", fallbackFactory = ImovelClientFallBackFactory.class)
public interface ImovelClient {
@GetMapping("/imovel")
List<Imovel> findAll();
@GetMapping("/imovel/{id}")
Imovel find(@PathVariable("id") Integer id);
}
@Component
public class ImovelClientFallBackFactory implements FallbackFactory<ImovelClient> {
@Override
public ImovelClient create(Throwable throwable) {
return new ImovelClientFallBack(throwable);
}
public class ImovelClientFallBack implements ImovelClient {
private final Throwable cause;
public ImovelClientFallBack(Throwable cause) {
this.cause = cause;
}
@Override
public List<Imovel> findAll() {
if (cause instanceof FeignException && ((FeignException) cause).status() == 404) {
throw new RuntimeException("Serviço indisponível!");
}
return null;
}
@Override
public Imovel find(Integer id) {
if (cause instanceof FeignException && ((FeignException) cause).status() == 404) { --aqui não vem 404, vem a exceção que comentei logo acima.
throw new RuntimeException("Serviço indisponível!");
}
return null;
}
}
}
application.yml of the CLIENT
server:
port: ${PORT:8082}
#LOCALIZAÇÃO DO EUREKA SERVER
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8080/eureka}
instance:
preferIpAddress: true
spring:
application:
name: cliente-service
#CONFIGURAÇÃO DE FALLBACK DO FEIGN
feign:
hystrix:
enabled: true
imovel-service:
ribbon:
MaxAutoRetries: 3
retryableStatusCodes: 404
The IMOVEL service has the same settings except application.yml
server:
port: ${PORT:8081}
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8080/eureka}
instance:
preferIpAddress: true
spring:
application:
name: imovel-service
Because I’m getting the exception of Loadbalancer and not 404?
Could someone help?!
– Cristiano Bombazar
This is the way that the client feign handles the balancing of the requests in conjunction with Eureka, that is, the client feign has fetched in Eureka the immovable-service application and has not found it. Therefore, the error returned is according to the functioning of the framework. You will not receive 404 because the Eureka API exists and you have asked for a service that exists, but you have no instance available. Therefore, received the exception that found no endpoint available.
– Rafael Manzoni