美文网首页
SpringCloud组件:Feign整合Hystrix实现熔断

SpringCloud组件:Feign整合Hystrix实现熔断

作者: 淡若S悠然 | 来源:发表于2019-03-01 11:13 被阅读0次

Hystrix简述

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。在微服务架构中,一个请求需要调用多个服务是非常常见的。

  • 雪崩效应
    多个微服务之间进行复杂的通信时,如果有一个服务出现问题,就会引发雪崩效应,导致整个系统瘫痪。Spring Cloud Hystrix提供了一个类似于保险丝的作用,当服务不可用的时候,Hystrix打开断路器,不再进行服务通信,从而保证自身服务的可用性。
  • 服务降级
    服务降级就是在系统高并发的情况下,可以将一些边缘服务进行降级(服务暂停),将资源优先供给核心服务的处理。

构建项目

因为熔断只是作用在消费方(服务调用方)这一端,因此我们根据SpringCloud组件:创建你的第一个Feign客户端文章,改动tairan-spring-cloud-feign-apitairan-spring-cloud-feign-consumer两个项目少量代码即可。

因为Feign中已经依赖了Hystrix,所以在Maven配置上不用做任何改动。

关于单独引入Hystrix依赖,网上有的文章说引入的依赖是spring-cloud-starter-hystrix,其实官方不在推荐使用,推荐使用spring-cloud-starter-netflix-hystrix

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

tairan-spring-cloud-feign-api修改

1. 创建HelloServiceHystrix

创建HelloServiceHystrix类,并实现HelloService接口,代码如下:

package com.tairan.chapter.feign.api.hystrix;

import com.tairan.chapter.feign.api.HelloService;
import org.springframework.stereotype.Component;

/**
 * 当HelloService中的Feign调用失败或超时时,会调用该实现类的方法
 * 需要注意的是fallback指定的类一定要添加@Component将其加入到Spring容器
 */
@Component
public class HelloServiceHystrix implements HelloService {
    @Override
    public String getMessage() throws Exception {
        return "Get Message Failed!";
    }
}

注意:fallback指定的类一定要添加@Component将其加入到Spring容器

2. 修改HelloService接口

修改HelloService接口,在FeignClient注解中添加fallback,指定Hystrix熔断异常回调类HelloServiceHystrix,代码如下:

package com.tairan.chapter.feign.api;

import com.tairan.chapter.feign.api.hystrix.HelloServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "tairan-spring-cloud-feign-provider", fallback = HelloServiceHystrix.class)
public interface HelloService {

    @RequestMapping("/hello")
    String getMessage() throws Exception;

}

tairan-spring-cloud-feign-consumer修改

  1. 开启FeignHystrix的支持,在application.yml文件中添加如下配置:
feign:
  # Dalston SR1(待定)之后的版本默认关闭hystrix对feign的支持,如果想要使用fallback功能这里必须启用
  hystrix:
    enabled: true
  1. 入口类修改SpringBootApplication扫描路径,可以扫描到Hystrix熔断类,即tairan-spring-cloud-feign-api中的HelloServiceHystrix类,代码如下所示:
@SpringBootApplication(scanBasePackages = "com.tairan.chapter.feign")
@EnableDiscoveryClient
// 通过@EnableFeignClients注解开启Spring Cloud Feign的支待功能。
@EnableFeignClients("com.tairan.chapter.feign.api")
public class TairanSpringCloudFeignConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TairanSpringCloudFeignConsumerApplication.class, args);
    }

}

运行测试

  1. 启动服务注册中心tairan-spring-cloud-eureka
  2. 启动服务提供方tairan-spring-cloud-feign-provider
  3. 启动服务消费方tairan-spring-cloud-feign-consumer
  4. 访问tairan-spring-cloud-feign-consumer工程的/feign-hello接口,链接:http://localhost:8080/feign-hello,查看访问结果
  5. 关闭服务提供方tairan-spring-cloud-feign-provider
  6. 访问tairan-spring-cloud-feign-consumer工程的/feign-hello接口,链接:http://localhost:8080/feign-hello,查看访问结果

执行4操作后,访问链接http://localhost:8080/feign-hello后,接口返回结果如下所示:


执行5操作后,访问链接http://localhost:8080/feign-hello后,接口返回结果如下所示:
根据返回结果说明熔断成功。

源码位置

本章源码已经上传到淡若悠然,请结合源码进行学习,感谢阅读。

码云地址(本章源码):https://gitee.com/litairan/tairan-spring-cloud

相关文章

网友评论

      本文标题:SpringCloud组件:Feign整合Hystrix实现熔断

      本文链接:https://www.haomeiwen.com/subject/xzoruqtx.html