```java
@RestController
public class DeptController {
@Autowired
private DeptService service = null;
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id) {
Dept dept = this.service.get(id);
if (null == dept) {
throw new RuntimeException("该ID:" + id + "没有没有对应的信息");
}
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id) {
return new Dept().setDeptno(id).setDname("该ID:" + id + "没有没有对应的信息,null--@HystrixCommand")
.setDb_source("no this database in MySQL");
}
}
```
* 在启动类上添加注解
```java
@SpringBootApplication
@EnableEurekaClient //本服务启动后自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
@EnableCircuitBreaker //对Hystrix熔断机制的支持
public class Provider_Dept_Hystrix_8004 {
public static void main(String[] args) {
SpringApplication.run(Provider_Dept_Hystrix_8004.class);
}
}