Somasundaram Sekar
Somasundaram Sekar

Reputation: 5524

SpringBoot: @HystrixCommand not working

Fallback method specified with @HystrixCommand is not invoked when configured with Eureka/Ribbon aware RestTemplate is used.

pom.xml

....
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
....

@SpringBootApplication config

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableHystrix
public class FileUploadServiceApplication {
   ...
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
  ...
}

@Component
@RefreshScope
public class FileUploadServiceImpl implements FileUploadService {
 ....
@HystrixCommand(fallbackMethod = "fallbackToMessageQ")
    private void notifyComplete(String fileName) {
     this.restTemplate.exchange(
                    "http://DISCOVERY-CLIENT/api/file/process?filename="+fileName, ....)
      ....
    }
    private void fallbackToMessageQ(String fileName, Throwable t) {
        System.out.println("notify threw exception, "+t.getMessage());
        rabbitTemplate.convertAndSend(uploadCompleteExchangeName, "", fileName.getBytes());
    }
}

when executed it ends just after throwing IllegalStateException for unable to access DISCOVERY-CLIENT and never going to access the fallback method, let me know if I have to configure anything else.

java.lang.IllegalStateException: No instances available for DISCOVERY-CLIENT
        at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90)
        at org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor$1.doWithRetry(RetryLoadBalancerInterceptor.java:88)
        at org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor$1.doWithRetry(RetryLoadBalancerInterceptor.java:76)
        at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:286)
        at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:163)

Upvotes: 1

Views: 4247

Answers (1)

spencergibb
spencergibb

Reputation: 25157

@HystrixCommand is used with an AOP pointcut and your method is private. Change its visibility to public.

Upvotes: 1

Related Questions