sentinel feign 源码分析

sentinal 对feign的支持需要设置feign.sentinel.enabled=true,这个要求可以从代码SentinelFeignAutoConfiguration看到

@Configuration
@ConditionalOnClass({ SphU.class, Feign.class })
public class SentinelFeignAutoConfiguration {
	@Bean
	@Scope("prototype") 
	@ConditionalOnMissingBean
	@ConditionalOnProperty(name = "feign.sentinel.enabled")
	public Feign.Builder feignSentinelBuilder() {
		return SentinelFeign.builder();
	}

}

feign默认使用hystrix,如何替换为sentinel的关键代码是自定义Feign.Builder,feign 本身有如下代码:

class HystrixTargeter implements Targeter {

	@Override
	public  T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context,
						Target.HardCodedTarget target) {
		if (!(feign instanceof feign.hystrix.HystrixFeign.Builder)) {
			return feign.target(target);
		}
		

但不是hystrix.HystrixFeign.Builder,调用自定义的target函数,在sentinel模式,最终转为调用
class SentinelFeign 内置public static final class Builder的public Feign build()函数。

调用feign.builder bean的关键函数如下(feign 代码)

class FeignClientFactoryBean implements FactoryBean, InitializingBean,
		ApplicationContextAware
		
protected  T get(FeignContext context, Class type) {
		T instance = context.getInstance(this.name, type);
		if (instance == null) {
			throw new IllegalStateException("No bean found of type " + type + " for "
					+ this.name);
		}
		return instance;
	}
 
  

总结下sentinel的关键类如下:

  1. SentinelFeignAutoConfiguration ,返回Feign.Builder
  2. SentinelInvocationHandler,实际调用执行
  3. SentinelDataSourceHandler,规则读取(数据源管理)
  4. SentinelFeign,构造调用handler实例,内部关键类Builder

sentinel feign 源码分析_第1张图片

你可能感兴趣的:(Ali,Sentinel,sentinel,feign)