1.引入依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>


2.使用
package com.example.demo;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app")
public class AppController {

    @RequestMapping("/get/{id}")
    @HystrixCommand(fallbackMethod = "getFallBack",  commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
    })
    public  String get(@PathVariable("id") long id) throws  Exception  {

     // throw   new Exception("error");
 Thread.sleep(id);
        return  "get";
    }

    public String getFallBack(@PathVariable("id") long id) {
        return "getFallBack";

    }
}

 

相关文章:

  • 2021-04-21
  • 2021-12-05
  • 2021-06-11
  • 2021-07-31
  • 2022-12-23
  • 2021-09-16
  • 2021-09-03
猜你喜欢
  • 2022-12-23
  • 2021-07-19
  • 2021-04-20
  • 2021-05-15
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案