zoukankan      html  css  js  c++  java
  • Hystrix的使用实例***

    Hystrix实现服务降级

      实现服务降级的条件:1、线程池已满 2、超时

    一、使用@HystrixCommand(fallbackMethod = "备选方法名")在服务使用方添加

      备选方法写在Controller类中,  违背了类单一原则, 不推荐

    1、导入Hystrix依赖

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

    2、在启动类中,需要添加 @EnableCircuitBreaker 注解

    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    @EnableCircuitBreaker
    public class SpringCloudUserApplication {

    3、对controller接口的修改

    @GetMapping("hello/{word}")
    @HystrixCommand(fallbackMethod = "saytextHello")
    public String sayHello(@PathVariable("word")String word)  throws  Exception{
        return masterService.sayHello(word); //通过Feign来接口式来调用
    }
    //使用熔断器
    @ResponseBody
    public String saytextHello(@PathVariable("word")String word)  throws  Exception{    
    return "连接失败";
    }

      @HystrixCommand 表示该接口开启 hystrix 熔断机制,如果出现问题,就去调用 fallbackMethod 属性指定的processQueryByIdHystrix方法,那么往下看,就能看到processQueryByIdHystrix方法,我们返回了和上面接口一样的数据结构,只不过都是我们自己搞的默认值而已。

    测试:

    开启注册中心

    开启服务提供方,服务使用方

    ① 正常启动访问,获取服务提供方数据

    ② 关闭服务提供方,手动宕机,开启熔断机制获取备选方法中的值

    开发中,我们的微服务可能会因为网络问题,我们会把这超时时候调大一点,我们可以在springBoot的配置文件中进行修改:

    #feign调用默认是1000毫秒=1秒   应该设置成更长时间1000 * 60 * 5  = 5分钟 
    #hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=300000 #hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=300000 #hystrix.command.default.circuitBreaker.forceClosed=true #hystrix.command.default.execution.timeout.enabled=false #请求处理的超时时间 ribbon.ReadTimeout=300000 ribbon.SocketTimeout=300000 #请求连接的超时时间 ribbon.ConnectTimeout: 30000

    二、在服务消费方使用Feign整合Hystrix (推荐)***

      专门写一个存放备选方法的类,备份类要求实现 Fegin的接口

    1、导入Hystrix依赖

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

    2、在启动类中,需要添加 @EnableCircuitBreaker 注解

    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    @EnableCircuitBreaker
    public class SpringCloudUserApplication {

    3、在application.yml配置文件中添加如下配置

    #允许Feign客户端使用Hystrix
    feign:
     hystrix: 
      enabled: true

    4、controller接口的的方法正常编写

    @GetMapping("hello/{word}")
    public String sayHello(@PathVariable("word")String word)  throws  Exception{
        return masterService.sayHello(word); //调用Fegin接口中的服务提供方的数据
    }

    5、新建一个备选类,要求:备份类要求实现 Fegin的接口

    Fegin 的客户端类:

    @FeignClient(value="springCould-master",fallback = MasterServiceFallBackFactory.class)
    //@RequestMapping("master") 服务提供方的窄化请求在此处不能这样写,要写在getMapper()中
    public interface MasterService {
        @GetMapping("master/sayHello/{word}")
        public String sayHello(@PathVariable("word")String word)  throws  Exception;
    }

    @RequestMapping("master")  出现了重复的url,绑定了重复url

    注意:不要在FeignClient接口上使用@RequestMapping绑定一个url前缀

    备选类:

    @Component //需要将这个类交给spring管理
    public class MasterServiceFallBackFactory implements MasterService {
        @Override
        @ResponseBody
        public String sayHello(String word) throws Exception {
            
            return "没有数据哦";
        }

    通过使用 Hystrix,我们能方便的防止雪崩效应,同时使系统具有自动降级和自动恢复服务的效果。

  • 相关阅读:
    JavaScript 深度克隆 JSON 对象
    NetBeans IDE 6.7.1 with JavaFX Now Available for Download!
    NetBeans 时事通讯(刊号 # 65 Jul 21, 2009)
    来自雨林木风的Linux发行版: Ylmf Linux
    JavaScript 深度克隆 JSON 对象
    STL vector vs list function comparison:
    手把手教你把Vim改装成一个IDE编程环境(图文)
    Using Visual Leak Detector
    疯狂的编程世界_IT新闻_博客园
    分享好段子:
  • 原文地址:https://www.cnblogs.com/64Byte/p/13292987.html
Copyright © 2011-2022 走看看