Build a hystrix of a Spring Cloud project from scratch (3)

Integrate hystrix

  1. Add dependency
  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
  1. Open annotation

Add @EnableFeignClients to the main class

  1. Open in configuration file (E and F versions are closed by default)

feign.hystrix.enabled=true

  1. Test code
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserApi userApi;

    @RequestMapping("/getUserById")
    public User getUserById(@RequestParam(name = "id") Integer id){
        System.out.println("调用方法");
        if(id==1){
            int i=1/0;
        }
        User user = new User();
        user.setId(1);
        user.setName("小明");
        return user;
    }

    @RequestMapping("/test")
    public User test(Integer id){
        User user = userApi.getUserById(id);
        return user;
    }
}

Test:
call http: // localhost: 8003 / user / test? Id = 2

Attachment:
hystrix circuit breaker three-state conversion diagram

  1. Regular arrangement

//全局超时时间
ribbon.ReadTimeout=2000  
ribbon.ConnectTimeout=2000
 
//一个rolling window内最小的请求数。如果设为20,那么当一个rolling window的时间内收到19个请求,即使请求都失败,也不会触发circuit break。默认20
hystrix.command.default.circuitBreaker.requestVolumeThreshold=20

//触发短路的时间值,当该值设为5000时,则当触发circuit break后的5000毫秒内都会拒绝request
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds 

Guess you like

Origin www.cnblogs.com/javammc/p/12682784.html