SpringBoot:@HystrixCommand不工作

时间:2017-03-13 15:29:04

标签: java spring-boot spring-cloud hystrix spring-cloud-netflix

使用Eureka / Ribbon识别时,不会调用@HystrixCommand指定的回退方法。使用RestTemplate。

的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());
    }
}

执行时,它在抛出IllegalStateException后无法访问DISCOVERY-CLIENT并且永远不会访问回退方法,请告诉我是否必须配置其他任何内容。

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)

1 个答案:

答案 0 :(得分:1)

@HystrixCommand与AOP切入点一起使用,您的方法为private。将其可见性更改为公开。

相关问题