springcloud-Hystrix断路器

1.依赖

        <!--引入hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

 2.服务端

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ProductservicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductservicesApplication.class, args);
    }

}

 

    @RequestMapping("/product/findAll")
    @HystrixCommand(fallbackMethod = "findAllFM")
    public Map findAll() {
        int a = 1 / 0;
        Map map = new HashMap();
        map.put("111", "苹果手机");
        map.put("222", "苹果笔记本");
        map.put("333", "端口" + port);
        return map;
    }

    public Map findAllFM() {
        Map map = new HashMap();
        map.put(-1, "服务异常,服务端开启断路器");
        return map;
    }

 3.客户端

feign.hystrix.enabled=true

 

@FeignClient(value = "productservices",fallback = ProductClientFB.class)
public interface ProductClient {
    @RequestMapping("/product/findAll")
    public Map findAll();
}

 

@Component
public class ProductClientFB implements ProductClient {
    @Override
    public Map findAll() {
        Map map = new HashMap();
        map.put(-1, "服务异常,客户端开启断路器");
        return map;
    }
}

 

posted @ 2020-08-12 11:11  陶海军  阅读(133)  评论(0编辑  收藏  举报