第十九天:浪迹天涯网上商城(1.0版本)--浪迹天涯商城运营后台整合Feign,开启hystrix

1、在Feign中开启对Hystrix的支持

如下,在配置文件中开启:
第十九天:浪迹天涯网上商城(1.0版本)--浪迹天涯商城运营后台整合Feign,开启hystrix

2、定义特定服务的Hystrix的配置

新建一个配置类,例如:ItemRemoteServiceFeignHystrixConfig

package com.niepengfei.langjitianya.backend.remote.item.config;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.niepengfei.langjitianya.backend.remote.item.ItemRemoteService;
import feign.Feign;
import feign.Logger;
import feign.Target;
import feign.hystrix.HystrixFeign;
import feign.hystrix.SetterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * @author Jack
 */
@Configuration
public class ItemRemoteServiceFeignHystrixConfig {

    @Resource
    private ItemRemoteServiceHystrixProperties properties;

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public Feign.Builder feignHystrixBuilder() {
        //对应的参数和属性可以在com.netflix.hystrix.HystrixCommandProperties这个类中找到
        return HystrixFeign.builder().setterFactory(new SetterFactory() {
            @Override
            public HystrixCommand.Setter create(Target<?> target, Method method) {
                return HystrixCommand.Setter
                        .withGroupKey(HystrixCommandGroupKey.Factory.asKey(ItemRemoteService.class.getSimpleName()))// 控制 ItemRemoteService 下,所有方法的Hystrix Configuration
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(properties.getWithCircuitBreakerRequestVolumeThreshold())
                                .withCircuitBreakerErrorThresholdPercentage(properties.getWithCircuitBreakerErrorThresholdPercentage())
                                .withCircuitBreakerSleepWindowInMilliseconds(properties.getWithCircuitBreakerSleepWindowInMilliseconds())
                                .withMetricsRollingStatisticalWindowInMilliseconds(properties.getWithMetricsRollingStatisticalWindowInMilliseconds())
                                .withMetricsRollingStatisticalWindowBuckets(properties.getWithMetricsRollingStatisticalWindowBuckets())
                                .withExecutionTimeoutInMilliseconds(properties.getWithExecutionTimeoutInMilliseconds())
                                .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
                        );
            }
        });
    }
}

在这里我们需要注意以下几点:
第一:ItemRemoteServiceHystrixProperties是我们自己创建的属性类,这个属性类的属性来源spring cloud config server , 这样方便属性的更改。该类的定义如下:
第十九天:浪迹天涯网上商城(1.0版本)--浪迹天涯商城运营后台整合Feign,开启hystrix
第二:withGroupKey(HystrixCommandGroupKey.Factory.asKey(ItemRemoteService.class.getSimpleName()))代码的解释如下:
控制 ItemRemoteService 下,所有方法的Hystrix Configuration,意思就是说,里面的配置,默认会ItemRemoteService 接口下的所有方法都是有效的。

3、在接口中指定Hystrix的配置

第十九天:浪迹天涯网上商城(1.0版本)--浪迹天涯商城运营后台整合Feign,开启hystrix

4、Debug方式启动观察

启动的时候,这个方法按照道理来说,会被调用两次。因为我们的ItemRemoteService只有两个方法,如果有N个方法的话,就会被调用N次。
第一次调用的是getItem,目的是给getItem方法设置Hystrix参数。
第十九天:浪迹天涯网上商城(1.0版本)--浪迹天涯商城运营后台整合Feign,开启hystrix
第二次调用的是getItemList,目的是给getItemList方法设置Hystrix参数。
第十九天:浪迹天涯网上商城(1.0版本)--浪迹天涯商城运营后台整合Feign,开启hystrix