springcloud 让feginclient 一部分支持hystrix,一部分不支持,的实现

1.假如我们的应用中有多个Feign Cleint对象,想让其中一部分支持Hystrix,一部分不支持。要实现这个功能,就要为单个FeignClient禁用Hystrix,就需要在自定义配置文件中修改其Feign.Builder参数,即修改Feign的构造类。

2.默认情况下,Feign是整合了Hystrix的,我们可以打开Spring Cloud中Feign组件的FeignClientsConfiguretion类查看其有关Budiler的配置项。如果我们不需要FeignClient支持Hystrix,在自定义的配置类中,将Budiler定义为Feign自己不支持Hystrix的构造类即可。

3.在配置文件中设置feign.hystrix.enabled参数,是设置全局的Feign是否支持

4.“Scope”就是Spring定义bean的生命周期的参数,常用的有singleton和prototype两种,其中:
(1)singleton:为“单一实例”的意思,一个容器中只存在一个实例,所有对该类型bean的依赖都引用这一单一实例。
(2)prototype:容器在接受到该类型的对象的请求的时候,会每次都重新生成一个新的对象给请求方
很显然,Feign对于Builder的Bean的管理,是希望其接受到该类型对象的请求时,每次都生成新对象

5.禁用单个fein使用hystrix:

/**
 * 禁用单个fein使用hystrix
 * @return
 */
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
    return Feign.builder();
}

在项目ms-hystrix-consumer进行进行修改:

1.controller:

package com.ljf.weifuwu.springcloud.hystrix.controller;

import com.ljf.weifuwu.springcloud.hystrix.model.HystrixUser;
import com.ljf.weifuwu.springcloud.hystrix.service.HystrixUserFegin;
import com.ljf.weifuwu.springcloud.hystrix.service.HystrixUserFegin2;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import feign.Param;
import feign.RequestLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class HystrixUserController {
    @Autowired
    private HystrixUserFegin huf;
    @Autowired
    private HystrixUserFegin2 huf2;
    /**
     * 这个是get请求
     * @param id
     * @return
     */
    @GetMapping("/hystrix-consumer/{id}")
    public HystrixUser queryInfoById(@PathVariable Long id) {
        System.out.println("hystrix的get请求!!!!!!!!!!1"+id);
        HystrixUser u= this.huf.getSingleUser(id);
        System.out.println("u:"+u.getName());
        return u;
    }
    /**
     * 这个是get请求
     * @param id
     * @return
     */
    @GetMapping("/hystrix-consumer2/{id}")
    public HystrixUser queryInfoById2(@PathVariable Long id) {
        System.out.println("hystrix的getu2请求!!!!!!!!!!1"+id);
        HystrixUser u= this.huf2.getSingleUser2(id);
        System.out.println("u2:"+u.getName());
        return u;
    }

}

2.fegin接口:

package com.ljf.weifuwu.springcloud.hystrix.service;

import com.ljf.weifuwu.springcloud.hystrix.model.HystrixUser;
import config.HystrixUserConfig;
import config.HystrixUserConfig2;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.netflix.feign.FeignClient;

/**
 * 当@FeignClient有name和url还有configuration时,取值为url的地址,name只是为一个名称而已(无意义)
  * 当@FeignClient只有name和configuration时,name的取值为eureka中的application项目的名称即虚拟地址
 */
@FeignClient(name="ms-eureka-provider2",url = "http://localhost:7901/",configuration = HystrixUserConfig2.class,fallbackFactory = HystrixFallBackFactory2.class)
public interface HystrixUserFegin2 {
    @RequestLine("GET /eureka-provider/{id}")   //使用fegin的特用注解,确保 HystrixUserConfig这个自定义的配置类进行了配置
    public HystrixUser getSingleUser2(@Param("id") Long id);
}

3.实现类:

package com.ljf.weifuwu.springcloud.hystrix.service;

import com.ljf.weifuwu.springcloud.hystrix.model.HystrixUser;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HystrixFallBackFactory2 implements FallbackFactory<HystrixUserFegin2> {
    //日志对象
    private static final Logger LOGGER = LoggerFactory.getLogger(HystrixFallBackFactory2.class);
    @Override
    public HystrixUserFegin2 create(final Throwable throwable) {
        return new HystrixUserFegin2(){
            /**
             * 针对此方法,做的超时异常处理
             * @param id
             * @return
             */
            public HystrixUser getSingleUser2(Long id) {
                // 日志最好放在各个fallback方法中,而不要直接放在create方法中。
                // 否则在引用启动时,就会打印该日志
                HystrixFallBackFactory2.LOGGER.info("fallback; reason was: ", throwable);
                System.out.println("超时发生错误的.....使用fegin-hystrix-facotry2进行处理");
                HystrixUser u=new HystrixUser();
                u.setId(00l);
                u.setName("fegin-hystrix-factory2-超时错误");
                return u;
            }
        };
    }

}

4.config配置类:

package config;

import feign.Contract;
import feign.Feign;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class HystrixUserConfig2 {
    /**
     * 作用是让fegin不支持springmvc的注解,只能使用Feign自己的注解,而不能使用Spring MVC的注解了。
     * @return
     */

    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();//只识别Feign的注释,不再识别Spring MVC的HTTP注释。
    }

    /**
     * 禁用单个fein使用hystrix
     * @return
     */
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();
    }

}

4.启动服务:ms-eureka-center(8761)、ms-eureka-provider(9701)、ms-hystrix-consumer(8005)

5.访问:

6.停掉ms-eureka-provider(9701)服务提供者:再访问

结论:该http://localhost:8005/hystrix-consumer/1  请求 超时后,执行了hystrix编写的逻辑处理,该http://localhost:8005/hystrix-consumer2/1 请求超时后,因为禁用了默认的hystrix,直接页面报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值