REST调用失败时,不会调用Hystrix后备方法

时间:2018-08-09 18:07:50

标签: spring-boot spring-cloud-netflix hystrix circuit-breaker

这是我的主班

@SpringBootApplication
@EnableCircuitBreaker
@ComponentScan("com.springboot.practise")
public class SpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class, args);
    }
}

我的控制器类:

@RestController
@RequestMapping(value="/SpringBootSample")
public class SpringBootController {
    @Autowired
    SpringBootService springBootService;
    @RequestMapping(value="/ping",method = RequestMethod.GET)
    public String ping(){
        return springBootService.ping();
    }
}

我的服务等级:

@Service
public class SpringBootService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod="fallbackForPing")
    public String ping(){
        String response = restTemplate
                .exchange("http://localhost:9080/IPromoterApp"
                , HttpMethod.GET
                , null
                , new ParameterizedTypeReference<String>() {
            }).getBody();
     return "Ping successful!! ";
    }
    @SuppressWarnings("unused")
    public String fallbackForPing(){
        System.out.println("Inside fallbackForPing");
        return "fallbackForPing";
    }
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

我提供了一些随机URL,该URL不存在以测试后备方法。但是不知道为什么后备方法没有被调用。总是低于输出:

{
    "timestamp": 1533837641768,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.web.client.HttpClientErrorException",
    "message": "404 Not Found",
    "path": "/SpringBootSample/ping"
}

有人请引导我。

0 个答案:

没有答案