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,每个方法的错误处理都可以进行操作。

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

  • 相关阅读:
    Elasticsearch 内存配置应用案例
    shell进阶篇之字典和数组结合应用案例
    shell进阶篇之数组应用案例
    nginx的负载均衡
    nginx的反向代理
    ajax的几种使用
    springboot整合Redis
    java的Spring中@Value注解的使用
    Redis的五种数据类型
    冒泡排序
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/9973952.html
Copyright © 2011-2022 走看看