1. Add dependence
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</dependency>
2. Modify the startup class
Consumer side, start the class to add @EnableCircuitBreaker
annotations
3. The method of adding annotations
consumerMedium mannerControllerFloor
@RestController
public class MyController {
@Autowired
private HelloService hs;
@RequestMapping("/test")
//fallbackMethod的参数为失败时的调用方法名(两个方法的参数相同)
@HystrixCommand(fallbackMethod = "fallback")
public String test(String name) {
return hs.hello(name);
}
//失败时调用此方法
public String fallback(String name) {
return "失败了--->" + name;
}
}
consumerMedium mannerServiceFloor
@FeignClient("eureka-provider")
public interface HelloService {
@RequestMapping("/hello")
public String hello(@RequestParam String name);
}
providerMedium mannerControllerFloor
@RequestMapping("/hello")
public String hello(String name) {
return "提供者1号--->你好," + name;
}
4. Test
4.1 startup registry, providers, consumers
4.2 Consumer access
4.3 Close providers
4.4 Consumer access again
Page displays the return value of access failure