欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring cloud 4 - Hystrix断熔器

程序员文章站 2022-06-05 08:49:44
...

为了防止雪崩效应,需要增加断熔器

jar

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

#

@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private EurakeService eurakeService;

    @RequestMapping(value="find",method=RequestMethod.GET)
    @ResponseBody
    @HystrixCommand(fallbackMethod="fallBackFind")
    //当访问这个方法失败的时候、访问fallBackFind方法、但是两个方法返回值传参必须一样
    public List<Dept> find(){
        List<Dept> list = eurakeService.findAll();
        System.out.println(list.size());
        return list;
    }

    public List<Dept> fallBackFind(){
        List<Dept> list = new ArrayList<Dept>();
        Dept dept = new Dept();
        dept.setDeptno(0);
        dept.setDname("测试部门");
        dept.setLoc("武汉");
        list.add(dept);
        return list;
    }
}

启动文件

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableDiscoveryClient
public class StartProject {
    public static void main(String[] args) {
        SpringApplication.run(StartProject.class, args);
    }
}
相关标签: 技术分享