zoukankan      html  css  js  c++  java
  • spring cloud 的hystrix 熔断器 和feign 调用的使用

    1, 添加依赖

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

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.1.3.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>


    2,yml 文件开启熔断
    feign:
    hystrix:
    enabled: true


    3,启动类上的注解
    @EnableHystrix
    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class DemoApplication {

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

    }

    4,feign 的接口
    @FeignClient(name = "testInfo", fallback= RemoteHelloCallBack.class)
    public interface RemoteHello {
    @RequestMapping(value = "/hello/error", method = RequestMethod.GET)
    public String getError();

    @RequestMapping(value = "/hello/get", method = RequestMethod.GET)
    public String getRemote();

    }


    5,hystrix 的使用
    @Component
    public class RemoteHelloCallBack implements RemoteHello {
    @Override
    public String getError() {
    return "RemoteError is block";
    }

    @Override
    public String getRemote() {
    return "get Remote hello is block";
    }
    }

    6, 控制层简单的调用

    @RestController
    public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
    return "helle consul";
    }
    @Autowired
    RemoteHello hello;
    @RequestMapping("/getRemote")
    public String getRemote() {
    return hello.getRemote();
    }

    @RequestMapping("/geterror")
    public String geterror() {
    return hello.getError();
    //return "this is error message..";
    }

    }



    
    




  • 相关阅读:
    【BZOJ】4671: 异或图
    【LOJ】#2035. 「SDOI2016」征途
    【UOJ】#37. 【清华集训2014】主旋律
    【LOJ】#2320. 「清华集训 2017」生成树计数
    【LOJ】#2290. 「THUWC 2017」随机二分图
    【LOJ】#2291. 「THUSC 2016」补退选
    【LOJ】 #2545. 「JXOI2018」守卫
    【LOJ】#2292. 「THUSC 2016」成绩单
    【LOJ】#2562. 「SDOI2018」战略游戏
    《linux 内核全然剖析》sched.c sched.h 代码分析笔记
  • 原文地址:https://www.cnblogs.com/bruce1992/p/13871892.html
Copyright © 2011-2022 走看看