SpringCloud(第 017 篇)电影微服务接入Feign,添加fallbackFactory属性来触发请求进行容灾降级...

SpringCloud(第 017 篇)电影微服务接入Feign,添加 fallbackFactory 属性来触发请求进行容灾降级

-

一、大致介绍

1、在一些场景中,简单的触发在 FeignClient 加入 Fallback 属性即可,而另外有一些场景需要访问导致回退触发的原因,那么这个时候可以在 FeignClient 中加入 FallbackFactory 属性即可;
2、而在使用 Fallback 和 FallbackFactory 时候,我仅仅表述个人观点,发现二者混合一起用的话,会发生冲突情况,所以大家用的时候注重考虑一下场景,二者属性用其一即可。

二、实现步骤

2.1 添加 maven 引用包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springms-consumer-movie-feign-with-hystrix-factory</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 客户端发现模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- Java HTTP 客户端开发的工具的模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <!-- Hystrix 模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 添加应用配置文件(springms-consumer-movie-feign-with-hystrix-factorysrcmainresourcesapplication.yml)

spring:
  application:
    name: springms-consumer-movie-feign-with-hystrix-factory
server:
  port: 8115
eureka:
  client:
#    healthcheck:
#      enabled: true
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}


# 解决第一次请求报超时异常的方案,因为 hystrix 的默认超时时间是 1 秒,因此请求超过该时间后,就会出现页面超时显示 :
#
# 这里就介绍大概三种方式来解决超时的问题,解决方案如下:
#
# 第一种方式:将 hystrix 的超时时间设置成 5000 毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#
# 或者:
# 第二种方式:将 hystrix 的超时时间直接禁用掉,这样就没有超时的一说了,因为永远也不会超时了
# hystrix.command.default.execution.timeout.enabled: false
#
# 或者:
# 第三种方式:索性禁用feign的hystrix支持
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持

# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds

2.3 添加实体用户类User(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudentityUser.java)

package com.springms.cloud.entity;

import java.math.BigDecimal;

public class User {

    private Long id;

    private String username;

    private String name;

    private Short age;

    private BigDecimal balance;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return this.age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

}

2.4 添加访问远端用户微服务 Feign 客户端(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignUserFeignHystrixFactoryClient.java)

package com.springms.cloud.feign;

import com.springms.cloud.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * 用户Http请求的客户端。
 *
 * 注解FeignClient的传参:表示的是注册到 Eureka 服务上的模块名称。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@FeignClient(name = "springms-provider-user", /*fallback = HystrixClientFallback.class,*/ fallbackFactory = HystrixClientFallbackFactory.class)
public interface UserFeignHystrixFactoryClient {

    /**
     * 这里有两个坑需要注意:<br/>
     *
     * <li>这里需要设置请求的方式为 RequestMapping 注解,用 GetMapping 注解是运行不成功的,即 GetMapping 不支持。</li>
     * <li>注解 PathVariable 里面需要填充变量的名字,不然也是运行不成功的。</li>
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
    public User findById(@PathVariable("id") Long id);
}

2.5 添加访问远端用户微服务 Fallback 类(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignHystrixClientFallback.java)

package com.springms.cloud.feign;

import com.springms.cloud.entity.User;
import org.springframework.stereotype.Component;

/**
 * Hystrix 客户端回退机制类。
 *
 * 这里加上注解 Component 的目的:就是因为没有这个注解,运行时候会报错,报错会说没有该类的这个实例,所以我们就想到要实例化这个类,因此加了这个注解。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@Component
public class HystrixClientFallback implements UserFeignHystrixFactoryClient {

    @Override
    public User findById(Long id) {

        System.out.println("======== findById Fallback " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());

        User tmpUser = new User();
        tmpUser.setId(0L);
        return tmpUser;
    }
}

2.6 添加访问远端用户微服务 FallbackFactory 类(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignHystrixClientFallbackFactory.java)

package com.springms.cloud.feign;

import com.springms.cloud.entity.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Hystrix 客户端回退机制类。
 *
 * 这里加上注解 Component 的目的:就是因为没有这个注解,运行时候会报错,报错会说没有该类的这个实例,所以我们就想到要实例化这个类,因此加了这个注解。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignHystrixFactoryClient> {

    private static final Logger Logger = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);

    @Override
    public UserFeignHystrixFactoryClient create(Throwable e) {

        Logger.info("fallback; reason was: {}", e.getMessage());
        System.out.println("======== UserFeignHystrixFactoryClient.create " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());

        return new UserFeignWithFallBackFactoryClient(){

            @Override
            public User findById(Long id) {
                System.out.println("======== findById FallBackFactory " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());

                User tmpUser = new User();
                tmpUser.setId(-1L);
                return tmpUser;
            }
        };
    }
}

/****************************************************************************************
 @FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
 protected interface HystrixClient {
     @RequestMapping(method = RequestMethod.GET, value = "/hello")
     Hello iFailSometimes();
 }

 @Component
 static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
     @Override
     public HystrixClient create(Throwable cause) {
        return new HystrixClientWithFallBackFactory() {
             @Override
             public Hello iFailSometimes() {
                return new Hello("fallback; reason was: " + cause.getMessage());
            }
        };
     }
 }
 ****************************************************************************************/

2.7 添加回退处理客户端类(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudfeignUserFeignWithFallBackFactoryClient.java)

package com.springms.cloud.feign;

/**
 * 回退处理客户端。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
public interface UserFeignWithFallBackFactoryClient extends UserFeignHystrixFactoryClient{
}

2.8 添加Web访问层Controller(springms-consumer-movie-feign-with-hystrix-factorysrcmainjavacomspringmscloudcontrollerMovieFeignHystrixFactoryController.java)

package com.springms.cloud.controller;

import com.springms.cloud.entity.User;
import com.springms.cloud.feign.UserFeignHystrixFactoryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MovieFeignHystrixFactoryController {

    @Autowired
    private UserFeignHystrixFactoryClient userFeignHystrixFactoryClient;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        System.out.println("======== findById Controller " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
        return userFeignHystrixFactoryClient.findById(id);
    }
}

2.9 添加电影微服务启动类(springms-consumer-movie-feign-custom-without-hystrixsrcmainjavacomspringmscloudMsConsumerMovieFeignCustomWithoutHystrixApplication.java)

package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/**
 * 电影微服务接入Feign,添加 fallbackFactory 属性来触发请求进行容灾降级。
 *
 * Feign: Java HTTP 客户端开发的工具。
 *
 * 注解 EnableFeignClients 表示该电影微服务已经接入 Feign 模块。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MsConsumerMovieFeignHystrixFactoryApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsConsumerMovieFeignHystrixFactoryApplication.class, args);
        System.out.println("【【【【【【 电影Feign-HystrixFactory微服务 】】】】】】已启动.");
    }
}

三、测试

/****************************************************************************************
 一、电影微服务接入Feign,添加 fallbackFactory 属性来触发请求进行容灾降级(测试正常接入功能):

 1、注解:EnableFeignClients;
 2、编写类 HystrixClientFallbackFactory 回退处理机制类,并给该类加上注解 Component ;加入 FeignClient 注解
     // @FeignClient(name = "springms-provider-user", fallback = HystrixClientFallback.class  )
 3、启动 springms-discovery-eureka 模块服务,启动1个端口;
 4、启动 springms-provider-user 模块服务,启动1个端口;
 5、启动 springms-consumer-movie-feign-with-hystrix-factory 模块服务;
 6、在浏览器输入地址 http://localhost:8115/movie/1 可以看到具体的用户信息(即用户ID != 0 的用户)成功的被打印出来;
 ****************************************************************************************/

/****************************************************************************************
 二、电影FeignHystrix-HystrixFactory微服务接入 HystrixFactory 功能模块(测试断路器功能):

 1、注解:EnableFeignClients;
 2、编写类 HystrixClientFallbackFactory 回退处理机制类,并给该类加上注解 Component,UserFeignHystrixFactoryClient 加上 fallbackFactory 属性;
     // @FeignClient(name = "springms-provider-user", fallback = HystrixClientFallback.class, fallbackFactory = HystrixClientFallbackFactory.class )
 3、启动 springms-discovery-eureka 模块服务,启动1个端口;
 4、启动 springms-provider-user 模块服务,启动1个端口;
 5、启动 springms-consumer-movie-feign-with-hystrix-factory 模块服务;
 6、在浏览器输入地址 http://localhost:8115/movie/1 可以看到具体的用户信息(即用户ID != 0 的用户)成功的被打印出来;

 7、停止 springms-provider-user 模块服务;
 8、在浏览器输入地址http://localhost:8115/movie/1 可以看到用户信息ID = 0 的用户成功的被打印出来,但随着问题也来了;
 9、HystrixClientFallbackFactory 截获的异常却没有被打印出来,本来用户微服务停止的话,请求链接就已经链接超时了,但是为啥异常没有打印出来呢?请看下面第三中测试方法。
 ****************************************************************************************/

/****************************************************************************************
 三、电影FeignHystrix-HystrixFactory微服务接入 HystrixFactory 功能模块(测试断路器功能):

 1、注解:EnableFeignClients;
 2、编写类 HystrixClientFallbackFactory 回退处理机制类,并给该类加上注解 Component,UserFeignHystrixFactoryClient 去掉 fallback 属性,然后加上 fallbackfactory 属性;
     // @FeignClient(name = "springms-provider-user", fallbackFactory = HystrixClientFallbackFactory.class )
 3、启动 springms-discovery-eureka 模块服务,启动1个端口;
 4、启动 springms-provider-user 模块服务,启动1个端口;
 5、启动 springms-consumer-movie-feign-with-hystrix-factory 模块服务;
 6、在浏览器输入地址 http://localhost:8115/movie/1 可以看到具体的用户信息(即用户ID != 0 的用户)成功的被打印出来;

 7、停止 springms-provider-user 模块服务;
 8、在浏览器输入地址http://localhost:8115/movie/1 可以看到用户信息ID = -1 的用户成功的被打印出来,而且异常信息日志也被打印出来了,这就正常了;

 注意:第2步骤:UserFeignHystrixFactoryClient 去掉 fallback 属性,然后加上 fallbackfactory 属性;
       所以这里目前暂时谨记,fallback 和 fallbackfactory 属性会有冲突,所以只要其一就行了;
 ****************************************************************************************/

四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

<think>好的,用户想了解feign.hystrix.FallbackFactory和org.springframework.cloud.openfeign.FallbackFactory之间的区别,以及是否可以互相替换。我需要先回忆一下这两个类的来源和用途。 首先,Feign是Netflix开源的声明式HTTP客户端,后来被Spring Cloud整合为OpenFeign。Hystrix是Netflix的熔断器库,早期Feign整合Hystrix使用的是Netflix的包,也就是feign.hystrix.FallbackFactory。随着Spring Cloud的发展,可能将相关类迁移到自己的包名下,比如org.springframework.cloud.openfeign.FallbackFactory,这样更符合Spring的命名规范,同时可能进行了一些扩展或优化。 接下来,我需要检查这两个接口的定义是否一致。如果它们的包名不同但接口方法相同,那么可能是同一接口的不同版本或不同项目中的实现。查看用户提供的引用,引用[1]提到Feign与Hystrix结合使用,需要添加Hystrix依赖,并在@FeignClient中指定fallback类。引用[3]和[4]则提到Spring Cloud OpenFeign的依赖配置,可能涉及包结构的变化。 然后,考虑版本兼性问题。用户提到从Netflix Feign升级到Spring Cloud OpenFeign时遇到的兼性问题(引用[2]),说明这两个FallbackFactory可能属于不同版本的库。如果用户在使用旧版的Netflix Feign,可能用的是feign.hystrix包下的类,而新版的Spring Cloud OpenFeign可能将其移至自己的包下,并保持相同的方法签名,这样可能可以直接替换,但需注意依赖管理和配置的变化。 另外,检查两个接口的方法。FallbackFactory通常有一个create方法,用于根据异常生成fallback实例。如果两个接口的方法签名相同,那么实现类可能可以通用,但包名不同可能导致编译错误,需要修改导入语句。同时,Spring Cloud可能对FallbackFactory进行了扩展,比如支持更多特性或与Spring上下文集成更好,直接替换可能需要调整相关配置或依赖。 还需要考虑Hystrix在Spring Cloud中的集成情况。引用[1]提到需要添加Hystrix依赖,而引用[4]提到使用Apache HttpClient或OkHttp的配置,可能影响Feign的行为。如果用户升级到新版本,可能Hystrix不再是默认选项,或者需要额外配置,比如引用[2]中的升级问题,这时候FallbackFactory的使用方式可能有变化。 总结:两个FallbackFactory功能相似,但属于不同包,可能对应不同版本的库。在Spring Cloud OpenFeign中,推荐使用org.springframework.cloud.openfeign包下的类,而旧版Netflix Feign使用feign.hystrix包。直接替换需要更改导入语句,并确保依赖版本兼,同时注意Hystrix的配置是否正确。如果方法签名一致,替换是可行的,但需测试是否与新版本的其他组件兼。</think>### Feign Hystrix FallbackFactorySpring Cloud OpenFeign FallbackFactory对比分析 #### 一、类来源与演进关系 1. **`feign.hystrix.FallbackFactory`** 属于Netflix Feign原生库的熔断器接口,用于生成Hystrix降级实现类[^1]。在早期Spring Cloud版本中(如Edgware、Finchley),通过`spring-cloud-starter-feign`依赖引入。 2. **`org.springframework.cloud.openfeign.FallbackFactory`** 是Spring Cloud OpenFeign对原接口的重新封装,用于适配Spring Cloud体系并增强功能(如Bean生命周期管理)[^2]。从Spring Cloud Greenwich版本开始成为标准实现。 #### 二、核心区别对比 | 特性 | feign.hystrix.FallbackFactory | spring.cloud.openfeign.FallbackFactory | |----------------------|-----------------------------------------|-----------------------------------------| | 包路径 | `feign.hystrix` | `org.springframework.cloud.openfeign` | | 依赖要求 | 需单独引入Hystrix依赖[^1] | 集成在OpenFeign核心库中 | | 上下文支持 | 无Spring上下文感知 | 支持Spring Bean注入[^2] | | 熔断器兼性 | 仅支持Hystrix | 支持Hystrix/Sentinel等熔断器 | | 方法签名 | `T create(Throwable cause)` | `T create(Throwable cause)` | #### 三、兼性判断条件 1. **可替换场景** 当同时满足以下条件时可直接替换: ```java // 原代码 import feign.hystrix.FallbackFactory; public class MyFallback implements FallbackFactory<ServiceClient> {...} // 替换后 import org.springframework.cloud.openfeign.FallbackFactory; public class MyFallback implements FallbackFactory<ServiceClient> {...} ``` - 项目已升级到Spring Cloud Hoxton及以上版本[^2] - 使用`spring-cloud-starter-openfeign`依赖(版本≥2.1.0)[^3] - 不需要Hystrix线程隔离特性 2. **不可替换场景** - 仍在使用`@EnableFeignClients`+`@EnableHystrix`组合配置 - 依赖Hystrix仪表盘监控数据 - 使用Feign与Hystrix的深度定制集成 #### 四、迁移验证步骤 1. 依赖配置检查 ```xml <!-- 升级后正确配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.1.3</version> </dependency> ``` 需移除旧的`feign-hystrix`依赖[^4] 2. 配置调整示例 ```yaml # application.yml feign: circuitbreaker: enabled: true # 新版本熔断器开关 ``` 3. 代码改造示例 ```java // 改造前 @FeignClient(name = "service", fallbackFactory = OldFallback.class) // 改造后(需实现新接口) public class NewFallback implements FallbackFactory<ServiceClient> { @Autowired // 支持Spring注入 private CircuitBreakerTracker tracker; } ``` #### 五、版本对应关系参考 | Spring Cloud Version | OpenFeign FallbackFactory 可用性 | Hystrix 兼性 | |----------------------|----------------------------------|---------------------| | Edgware (1.x) | ❌ | ✔️ 原生支持 | | Finchley (2.0.x) | ❌ | ✔️ 需显式配置[^3] | | Greenwich (2.1.x) | ✔️ 实验性支持 | ✔️ 兼模式 | | Hoxton (2.2.x) | ✔️ 正式支持 | ⚠️ 需额外配置 | | 2020.x (3.x) | ✔️ 默认实现 | ❌ 需独立依赖 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值