Spring Cloud Hystrix:fallbackMethod不起作用

时间:2019-10-12 03:56:36

标签: java hystrix

为什么我的fallbackMethod不起作用?

如果我关闭服务提供商,则返回java.net.ConnectException:连接被拒绝:connect

绵方法:

@EnableCircuitBreaker
@SpringBootApplication
public class ConsumerApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }


    /**
     * 更改负载均衡策略
     *
     * @return IRule
     */
    @Bean
    public IRule irule() {
        return new RandomRule();
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

HelloService:

@Service
public class HelloService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallBack")
    public String helloService() {
        System.out.println("run consumer-hello-ribbon");
        return restTemplate.getForEntity("http://PROVIDER-SERVICE/hello", String.class).getBody();
    }

    public String helloFallBack() {
        return "error";
    }
}

HelloController:

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping(value = "/consumer-hello-ribbon")
    public String hello() {
        return helloService.helloService();
    }
}

我该如何解决? 请帮助我,谢谢

0 个答案:

没有答案