• 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

     

  • 相关阅读:
    nmap参数原理抓包分析
    powershell脚本执行绕过powershell下脚本执行限制(cmd下执行)以及在cmd下隐藏脚本窗口
    windows创建域共享文件
    利用开机账户登录“轻松访问”创建Windows后门
    win7下建立超级隐藏账户
    利用python搭建Powersploit powershell脚本站点
    windows powershell基础
    Active Directory 域服务安装与测试
    python多线程与多进程--存活主机ping扫描以及爬取股票价格
    Anaconda基础使用
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11928301.html
Copyright © 2020-2023  润新知