zoukankan      html  css  js  c++  java
  • Hystrix断路器 熔断器Hystrix的在Ribbon的集成

    Hystrix作用

    资源隔离(限流):包括线程池隔离信号量隔离,限制调用分布式服务的资源使用,某一个调用的服务出现问题不会影响其他服务调用。

    当失败率达到阀值自动触发降级(如因网络故障/超时造成的失败率高),熔断器触发的快速失败会进行快速恢复

    降级机制超时降级、资源不足时(线程或信号量)降级,降级后可以配合降级接口返回托底数据。

    缓存:提供了请求缓存、请求合并实现。

    A.

    1.导包

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

    2.在主配置类上加注解

    @EnableCircuitBreaker

    package cn.jiedada;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    /**
     *@EnableCircuitBreaker:开启Hystrix
     *
     */
    @SpringBootApplication
    @EnableEurekaClient
    @EnableCircuitBreaker
    public class EurekaOrderService3000 {
        public static void main(String[] args) {
            new SpringApplicationBuilder(EurekaOrderService3000.class).web(true).run(args);
        }
    
    }
    View Code

    3.在controller中加标签及方法

    @HystrixCommand(fallbackMethod = "getUserByIdFallbackMethod")

    @HystrixCommand:当没有出现熔断的时候就执行当前方法
    当出现熔断的时候就执行fallbackMethod中的方法

    package cn.jiedada.web.controller;
    
    import cn.jiedada.domain.User;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("/customer")
    public class OrderController {
        @Autowired
        private RestTemplate restTemplate;
    
        @RequestMapping("/")
    
        public String home() {
            return "Hello world";
        }
        /*这个方法是我们去掉user_service_2000中的数据
        所以需要使用restTemplate的类
        @HystrixCommand:当没有出现熔断的时候就执行当前方法
        当出现熔断的时候就执行fallbackMethod中的方法
        * */
        @RequestMapping("/user/{id}")
        @HystrixCommand(fallbackMethod = "getUserByIdFallbackMethod")
        public User getUserById(@PathVariable("id")Long id){
            //拼接字符串
            String url="http://USER-SERVER/provider/user/"+id;
            //通过这个方法调用我们的
            return restTemplate.getForObject(url,User.class);
        }
        public User getUserByIdFallbackMethod(@PathVariable("id")Long id){
            return new User(-1l,"对不起该服务被停用了");
        }
    
    }
    View Code

     

  • 相关阅读:
    BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
    BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理
    BZOJ-1834 网络扩容 最小费用最大流+最大流+乱搞
    学习笔记 --- 最小费用最大流
    BZOJ-1927 星际竞速 最小费用最大流+拆点+不坑建图
    BZOJ-1070 修车 最小费用最大流+拆点+略坑建图
    BZOJ-1207 打鼹鼠 DP(LIS)
    BZOJ-2756 奇怪的游戏 黑白染色+最大流+当前弧优化+二分判断+分类讨论
    BZOJ-1189 紧急疏散evacuate BFS预处理+最大流+二分判定+神建模!!
    BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11928301.html
Copyright © 2011-2022 走看看