zoukankan      html  css  js  c++  java
  • hystrix学习

    hystrix是一个用于处理分布式系统延迟和容错的开源库,在系统中,如出现超时,异常等,hystrix能够保证三个依赖出问题的情况下,不会导致整个服务失败,避免级联故障,提高了分布式的弹性。

    加入依赖:

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

    HystrixCommand(fallbackMethod = "失败后调用的方法")

    @Autowired
        private TestClientService testServcie;
    
        @RequestMapping(value = "person/get/{id}",method = RequestMethod.GET)
        @HystrixCommand(fallbackMethod="error_get")
        public Person get(@PathVariable("id")Long id){
    
        }
     
    
       public Person error_get(@PathVariable("id")Long id){
              return "信息"
       }

    启动类上要加@EnableCircuitBreaker //hystrix熔断机制的支持


    服务降级:

      使用HystrixCommand有太多冗余,所以我们要在接口上实现

    @Component
    public class TestClientServiceFallbackFactory implements FallbackFactory<TestClientService> {
        @Override
        public TestClientService create(Throwable throwable) {
            return new TestClientService() {
                @Override
                public Person get(Person person) {
                    return "错误信息";
                }
            };
        }
    }
    TestClientService:
    @FeignClient(value = "MICROSERVICECLOUD",fallbackFactory = TestClientServiceFallbackFactory.class)
    public interface TestClientService {
    
        @RequestMapping(value = "/person/get/{id}")
        public Person get(@PathVariable("id") Long id);
    
    }

    feign的项目里要加上

    feign:
      hystrix:
        enabled: true

    HystrixDashboard

    添加注解

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

    在启动类上加

    @EnableHystrixDashboard

    ...
  • 相关阅读:
    wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)
    32:从1到n整数中1出现的次数
    31:连续子数组的最大和
    30:最小的K个数
    29:数组中出现次数超过一半的数字
    28:字符串的排列
    27:二叉搜索树与双向链表
    26:复杂链表的复制
    25:二叉树中和为某一个定值的路径
    24:二叉搜索树的后序遍历序列
  • 原文地址:https://www.cnblogs.com/javage/p/9493280.html
Copyright © 2011-2022 走看看