Eu possuo uma estrutura de serviços da seguinte maneira: server (Eureka), imovel (eureka-client) e cliente (eureka-client).
O seviço de cliente faz uma requisição para o serviço de imovel, utilizando o feign. Eu possuo o LoadBalancer configurado nos dois lados (imóvel e cliente). Se todos os serviços estão em execução, as chamadass e o LoadBalancer funcionam corretamente. Porém, comecei a fazer a parte do Circuitbreaker (Hystrix).
Eu subi apenas o serviço do server (eureka) e o serviço do cliente, para testar o CircuitBreaker. Quando o serviço de cliente foi fazer uma requisição para o serviço de imóvel, o fallback foi ativado corretamente, porém, a exceção lançada foi diferente da do esperado. A exceção que recebi foi:
java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: imovel-service
Eu esperava receber uma exceção do tipo 400+. O que estou fazendo de errado?
server (Eureka Server)
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
application.yml do server
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
Serviço CLIENTE:
@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 do CLIENTE
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
O serviço IMOVEL tem as mesmas configurações, exceto o application.yml
server:
port: ${PORT:8081}
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8080/eureka}
instance:
preferIpAddress: true
spring:
application:
name: imovel-service
Porque estou recebendo a exceção de LoadBalancer e não de 404?