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

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

posted @ 2018-11-17 15:05  eros_token  阅读(213)  评论(0编辑  收藏  举报