zoukankan      html  css  js  c++  java
  • eureka_hystrix学习_1

    目前了解到的熔断机制实现方式有两种

    1.加载ribbon时实现的

    a.添加相关依赖

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

    b.在Application上添加注解@EnableHystrix和@EnableHystrixDashboard

    c.Service层,在我们需要进行熔断处理的方法上添加@HystrixCommand,制定fallbackMethod来对错误进行处理。如:

        @HystrixCommand(fallbackMethod = "hiError")
        public String hiService(String name){
            return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
        }
    
        public String hiError(String name){
            return "hi,"+name+",sorry,error!";
        }

    2.加载feign时实现的

    a.添加依赖

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

    b.在yml配置文件中添加feign.hystrix.enabled: true 来启动熔断机制

    c.在Application上添加注解@EnableCircuitBreaker

    d.在@FeignClient注解的属性中添加fallback指定错误处理类

    e.定义错误处理类实现@FeignClient,每个方法的错误处理都可以进行操作。

    注意:在我看来,第二种比较适合,他将错误处理分离出来了

  • 相关阅读:
    HashMap源码分析和应用实例的介绍
    Map不同具体实现类的比较和应用场景的分析
    Set集合架构和常用实现类的源码分析以及实例应用
    深入理解JVM(7)——类加载器
    深入理解JVM(5)——HotSpot垃圾收集器详解
    PoolManager 简单使用
    HDU4786_Fibonacci Tree
    UVA11653_Buses
    UVA11625_Lines of Containers
    HDU3507_Print Article
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/9973952.html
Copyright © 2011-2022 走看看