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

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

  • 相关阅读:
    你要的SSM(Spring+Springmvc+Mybatis)小项目来了!!!
    王爽《汇编语言》(第三版)实验10解析
    java1.8安装及环境变量配置
    王爽《汇编语言》(第三版)实验9解析
    王爽《汇编语言》(第三版)实验8解析(超详细)
    2020软件工程作业06
    鸽子开发组——冲刺日志(第四天)
    String 类中常用方法
    mysql
    array_merge和加号+的区别
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/9973952.html
Copyright © 2020-2023  润新知