1, 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2,yml 文件开启熔断
feign:
hystrix:
enabled: true
3,启动类上的注解
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4,feign 的接口
@FeignClient(name = "testInfo", fallback= RemoteHelloCallBack.class)
public interface RemoteHello {
@RequestMapping(value = "/hello/error", method = RequestMethod.GET)
public String getError();
@RequestMapping(value = "/hello/get", method = RequestMethod.GET)
public String getRemote();
}
5,hystrix 的使用
@Component
public class RemoteHelloCallBack implements RemoteHello {
@Override
public String getError() {
return "RemoteError is block";
}
@Override
public String getRemote() {
return "get Remote hello is block";
}
}
6, 控制层简单的调用
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "helle consul";
}
@Autowired
RemoteHello hello;
@RequestMapping("/getRemote")
public String getRemote() {
return hello.getRemote();
}
@RequestMapping("/geterror")
public String geterror() {
return hello.getError();
//return "this is error message..";
}
}