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..";
    }

    }



    
    




  • 相关阅读:
    获取设备信息
    获取Mac地址
    常用正则
    几个字段相同的纪录去重留下主键最小的一条纪录
    mysql查询数据库中包含某字段(列名)的所有表
    nginx反向代理
    Eureka参数配置项详解
    html5验证自适应
    synchronized对象解析
    多线程进行数据同步
  • 原文地址:https://www.cnblogs.com/bruce1992/p/13871892.html
Copyright © 2011-2022 走看看