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

    ...
  • 相关阅读:
    Activiti6详细教程
    Nginx 与 Tomcat : 413 Request Entity Too Large(请求实体太大)
    windows下安装pytorch
    vim编辑器常用操作
    el-dialog 里面的组件不刷新问题
    el-dialog 里面的 el-form 重置表单问题
    ElementUI树形表格默认展开
    设计模式之 工厂方法模式
    linux创建一个proc代码示例
    redis的安装及使用
  • 原文地址:https://www.cnblogs.com/javage/p/9493280.html
Copyright © 2011-2022 走看看