spring整合feign和hystrix实现RPC熔断降级和监控

背景

在spring应用中,原始的openfeign生成的代理对象是没有直接在spring容器中注入的,要使用openfeign的原生使用方式如下所示。

这种方式,对一个有追求的程序员来说很不优雅,本文来解决将feign整合到spring中的问题,实现feign的优雅使用。

解决上一个问题,之后我们还问自己一个问题,feign的作用是http的rpc调用,但是http的调用如何优雅的实现服务熔断和降级呢?很明显用hystrix,这里又有问题了,hystrix和feign分别生成的动态代理对象是不兼容的,feign是需要手动创建代理对象,而hystrix可以通过spring创建或者通过command api来创建,如何将 feign和hystrix一起整合到spring中呢?

带着这两个问题我们继续。

feign

首先feign的介绍可以参考上一篇博文:添feign的基本使用和源码解析,讲解了feign的使用和工作流程和源码解析。
回顾一下feign使用,Feign.builder() .target()会创建GitHub类的动态代理对象GitHubProxy即github实例

public class MyApp {
   
  public static void main(String[] args) {
   
    GitHub github = Feign.builder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");
    
  /**
  *@Param注解的参数会按照名字填充{owner}{repo}
  HousonCao/Hystrix
  */
    github.contributors("HousonCao", "Hystrix");
  }
}

hystrix

hystric使用和原理介绍参考:
https://mp.csdn.net/console/editor/html/111477766

spring整合feign和hystrix

本文重点阐述如何将第三方框架集成到spring,以spring集成hystrix-feign扩展为例展示spring集成三方框架的流程。

首先说明一下需求:

feign在springboot中是可以和feign和hystrix一起使用的,但是在spring中需要自己集成到spring,我们基于

和他的扩展

实现spring整合feign和hystrix,达到声明式的http RPC调用,同时支持RxJava响应式编程的目标。我们思考一下:mybatis是如何实现和spring集成的呢,我们要做的和mybatis很类似,甚至可以照葫芦画瓢。

public void setHttpClient(OkHttpClientService httpClient) {
    this.httpClient = httpClient;
}

public Class getFallback() {
    return fallback;
}

public void setFallback(Class fallback) {
    this.fallback = fallback;
}

public FeignRegistry getFeignRegistry() {
    return feignRegistry;
}

public void setFeignRegistry(FeignRegistry feignRegistry) {
    this.feignRegistry = feignRegistry;
}

/**
 * {@inheritDoc}
 */
@Override
public T getObject() throws Exception {

    if (feignRegistry.getMapper(hystrixFeignInterface) == null) {
        notNull(this.hystrixFeignInterface, "Property 'mapperInterface' is required");
        // create feign hystrix client instance
        LOGGER.info("创建feign客户端参数:Uri:{}",uri);
        Object feignClient = TraceHystrixFeign.builder()
                .setterFactory(setterFactory)
                .encoder(encoder)
                .decoder(decoder)
                .logger(logger)
                .logLevel(Level.FULL)
                .retryer(new NoRetryer())
                .client(new OkHttpClient(httpClient.getmOkHttpClient()))
                .options(new Options(httpClient.getConnectTimeout(), httpClient.getReadTimeout()))
                .target(hystrixFeignInterface, uri, new FallbackFactory() {

                    @Override
                    public Object create(Throwable cause) {
                        try {
                            return BeanUtils.instantiateClass(fallback.getConstructor(Throwable.class), cause);
                        } catch (NoSuchMethodException e) {
                            LOGGER.warn("创建fallback失败",e);
                            throw new RuntimeException(e);
                        }
                    }
                });

        // add feign hystrix interface target instance to set
        feignRegistry.addMapper(hystrixFeignInterface, feignClient);
    }

    // get from set
    return (T) feignRegistry.getMapper(hystrixFeignInterface);


}

/**
 * {@inheritDoc}
 */
@Override
public Class<T> getObjectType() {
    return this.hystrixFeignInterface;
}

/**
 * {@inheritDoc}
 */
@Override
public boolean isSingleton() {
    return true;
}

public Class<T> getHystrixFeignInterface() {
    return hystrixFeignInterface;
}

public void setHystrixFeignInterface(Class<T> hystrixFeignInterface) {
    this.hystrixFeignInterface = hystrixFeignInterface;
}

}


bean定义完了需要定义注解,让客户端能配置factorybean的属性。这里直接将@hystrixCommand的部分属性搬过来。

```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface HystrixFeign {
    String feignRegistry() default "feignRegistry";

    Class fallbackRef()  ;

    /**
     * feign的请求body编码器
     * @return
     */
    String encoderRef() default "";

    /**
     * feign 响应解码器
     * @return
     */
    String decoderRef() default "";
    String httpClient() default "";

    String uri() default "";
    /**
     * The command group key is used for grouping together commands such as for reporting,
     * alerting, dashboards or team/library ownership.
     * <p/>
     * default => the runtime class name of annotated method
     *
     * @return group key
     */
    String groupKey() default "";

    /**
     * Hystrix command key.
     * <p/>
     * default => the name of annotated method. for example:
     * <code>
     *     ...
     *     @HystrixCommand
     *     public User getUserById(...)
     *     ...
     *     the command name will be: 'getUserById'
     * </code>
     *
     * @return command key
     */
    String commandKey() default "";

    /**
     * The thread-pool key is used to represent a
     * HystrixThreadPool for monitoring, metrics publishing, caching and other such uses.
     *
     * @return thread pool key
     */
    String threadPoolKey() default "";

    /**
     * Specifies a method to process fallback logic.
     * A fallback method should be defined in the same class where is HystrixCommand.
     * Also a fallback method should have same signature to a method which was invoked as hystrix command.
     * for example:
     * <code>
     *      @HystrixCommand(fallbackMethod = "getByIdFallback")
     *      public String getById(String id) {...}
     *
     *      private String getByIdFallback(String id) {...}
     * </code>
     * Also a fallback method can be annotated with {@link HystrixCommand}
     * <p/>
     * default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()}
     *
     * @return method name
     */
    String fallbackMethod() default "";

    /**
     * Specifies command properties.
     *
     * @return command properties
     */
    HystrixProperty[] commandProperties() default {};

    /**
     * Specifies thread pool properties.
     *
     * @return thread pool properties
     */
    HystrixProperty[] threadPoolProperties() default {};

    /**
     * Defines exceptions which should be ignored.
     * Optionally these can be wrapped in HystrixRuntimeException if raiseHystrixExceptions contains RUNTIME_EXCEPTION.
     *
     * @return exceptions to ignore
     */
    Class<? extends Throwable>[] ignoreExceptions() default {};

    /**
     * Specifies the mode that should be used to execute hystrix observable command.
     * For more information see {@link ObservableExecutionMode}.
     *
     * @return observable execution mode
     */
    ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;

    /**
     * When includes RUNTIME_EXCEPTION, any exceptions that are not ignored are wrapped in HystrixRuntimeException.
     *
     * @return exceptions to wrap
     */
    HystrixException[] raiseHystrixExceptions() default {};

    /**
     * Specifies default fallback method for the command. If both {@link #fallbackMethod} and {@link #defaultFallback}
     * methods are specified then specific one is used.
     * note: default fallback method cannot have parameters, return type should be compatible with command return type.
     *
     * @return the name of default fallback method
     */
    String defaultFallback() default "";

}

定义完属性,我们继续

bean扫描

  1. 继承Spring提供的ClassPathBeanDefinitionScanner 进行beandefinition的扫描,实现doScan方法,生成beandefinition,返回beandefinition集合进行注册。类似spring的@component注解需要有个扫描的过程。

    public class ClassPathHystrixFeignScanner extends ClassPathBeanDefinitionScanner {
         
    
        public static final Logger LOGGER = LoggerFactory.getLogger(ClassPathHystrixFeignScanner.class);
    
        private Class<? extends Annotation> annotationClass;
    
    
        private HystrixFeignFactoryBean mapperFactoryBean = new HystrixFeignFactoryBean();
    
        public ClassPathHystrixFeignScanner(BeanDefinitionRegistry registry) {
         
            super(registry, false);
        }
    
    
        public void setAnnotationClass(Class<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值