溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring cloud中FeignClientFactoryBean的原理和作用是什么

發(fā)布時(shí)間:2021-07-01 10:48:22 來(lái)源:億速云 閱讀:567 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“spring cloud中FeignClientFactoryBean的原理和作用是什么”,在日常操作中,相信很多人在spring cloud中FeignClientFactoryBean的原理和作用是什么問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”spring cloud中FeignClientFactoryBean的原理和作用是什么”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

本文主要研究一下spring cloud的FeignClientFactoryBean

FeignClientFactoryBean

spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/FeignClientFactoryBean.java

class FeignClientFactoryBean
		implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {

	/***********************************
	 * WARNING! Nothing in this class should be @Autowired. It causes NPEs because of some
	 * lifecycle race condition.
	 ***********************************/

	private Class<?> type;

	private String name;

	private String url;

	private String contextId;

	private String path;

	private boolean decode404;

	private ApplicationContext applicationContext;

	private Class<?> fallback = void.class;

	private Class<?> fallbackFactory = void.class;

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.hasText(this.contextId, "Context id must be set");
		Assert.hasText(this.name, "Name must be set");
	}

	@Override
	public Object getObject() throws Exception {
		return getTarget();
	}

	@Override
	public Class<?> getObjectType() {
		return this.type;
	}

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

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.applicationContext = context;
	}

	<T> T getTarget() {
		FeignContext context = this.applicationContext.getBean(FeignContext.class);
		Feign.Builder builder = feign(context);

		if (!StringUtils.hasText(this.url)) {
			if (!this.name.startsWith("http")) {
				this.url = "http://" + this.name;
			}
			else {
				this.url = this.name;
			}
			this.url += cleanPath();
			return (T) loadBalance(builder, context,
					new HardCodedTarget<>(this.type, this.name, this.url));
		}
		if (StringUtils.hasText(this.url) && !this.url.startsWith("http")) {
			this.url = "http://" + this.url;
		}
		String url = this.url + cleanPath();
		Client client = getOptional(context, Client.class);
		if (client != null) {
			if (client instanceof LoadBalancerFeignClient) {
				// not load balancing because we have a url,
				// but ribbon is on the classpath, so unwrap
				client = ((LoadBalancerFeignClient) client).getDelegate();
			}
			builder.client(client);
		}
		Targeter targeter = get(context, Targeter.class);
		return (T) targeter.target(this, builder, context,
				new HardCodedTarget<>(this.type, this.name, url));
	}

	private String cleanPath() {
		String path = this.path.trim();
		if (StringUtils.hasLength(path)) {
			if (!path.startsWith("/")) {
				path = "/" + path;
			}
			if (path.endsWith("/")) {
				path = path.substring(0, path.length() - 1);
			}
		}
		return path;
	}

	//......

}
  • FeignClientFactoryBean實(shí)現(xiàn)了FactoryBean的getObject、getObjectType、isSingleton方法;實(shí)現(xiàn)了InitializingBean的afterPropertiesSet方法;實(shí)現(xiàn)了ApplicationContextAware的setApplicationContext方法

  • getObject調(diào)用的是getTarget方法,它從applicationContext取出FeignContext,然后構(gòu)造Feign.Builder并設(shè)置了logger、encoder、decoder、contract,之后通過configureFeign根據(jù)FeignClientProperties來(lái)進(jìn)一步配置Feign.Builder的retryer、errorDecoder、request.Options、requestInterceptors、queryMapEncoder、decode404

  • 初步配置完Feign.Builder之后再判斷是否需要loadBalance,如果需要?jiǎng)t通過loadBalance方法來(lái)設(shè)置,不需要?jiǎng)t在Client是LoadBalancerFeignClient的時(shí)候進(jìn)行unwrap

FeignClientProperties

spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/FeignClientProperties.java

@ConfigurationProperties("feign.client")
public class FeignClientProperties {

	private boolean defaultToProperties = true;

	private String defaultConfig = "default";

	private Map<String, FeignClientConfiguration> config = new HashMap<>();

	public boolean isDefaultToProperties() {
		return this.defaultToProperties;
	}

	public void setDefaultToProperties(boolean defaultToProperties) {
		this.defaultToProperties = defaultToProperties;
	}

	public String getDefaultConfig() {
		return this.defaultConfig;
	}

	public void setDefaultConfig(String defaultConfig) {
		this.defaultConfig = defaultConfig;
	}

	public Map<String, FeignClientConfiguration> getConfig() {
		return this.config;
	}

	public void setConfig(Map<String, FeignClientConfiguration> config) {
		this.config = config;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		FeignClientProperties that = (FeignClientProperties) o;
		return this.defaultToProperties == that.defaultToProperties
				&& Objects.equals(this.defaultConfig, that.defaultConfig)
				&& Objects.equals(this.config, that.config);
	}

	@Override
	public int hashCode() {
		return Objects.hash(this.defaultToProperties, this.defaultConfig, this.config);
	}

	/**
	 * Feign client configuration.
	 */
	public static class FeignClientConfiguration {

		private Logger.Level loggerLevel;

		private Integer connectTimeout;

		private Integer readTimeout;

		private Class<Retryer> retryer;

		private Class<ErrorDecoder> errorDecoder;

		private List<Class<RequestInterceptor>> requestInterceptors;

		private Boolean decode404;

		private Class<Decoder> decoder;

		private Class<Encoder> encoder;

		private Class<Contract> contract;

		public Logger.Level getLoggerLevel() {
			return this.loggerLevel;
		}

		public void setLoggerLevel(Logger.Level loggerLevel) {
			this.loggerLevel = loggerLevel;
		}

		public Integer getConnectTimeout() {
			return this.connectTimeout;
		}

		public void setConnectTimeout(Integer connectTimeout) {
			this.connectTimeout = connectTimeout;
		}

		public Integer getReadTimeout() {
			return this.readTimeout;
		}

		public void setReadTimeout(Integer readTimeout) {
			this.readTimeout = readTimeout;
		}

		public Class<Retryer> getRetryer() {
			return this.retryer;
		}

		public void setRetryer(Class<Retryer> retryer) {
			this.retryer = retryer;
		}

		public Class<ErrorDecoder> getErrorDecoder() {
			return this.errorDecoder;
		}

		public void setErrorDecoder(Class<ErrorDecoder> errorDecoder) {
			this.errorDecoder = errorDecoder;
		}

		public List<Class<RequestInterceptor>> getRequestInterceptors() {
			return this.requestInterceptors;
		}

		public void setRequestInterceptors(
				List<Class<RequestInterceptor>> requestInterceptors) {
			this.requestInterceptors = requestInterceptors;
		}

		public Boolean getDecode404() {
			return this.decode404;
		}

		public void setDecode404(Boolean decode404) {
			this.decode404 = decode404;
		}

		public Class<Decoder> getDecoder() {
			return this.decoder;
		}

		public void setDecoder(Class<Decoder> decoder) {
			this.decoder = decoder;
		}

		public Class<Encoder> getEncoder() {
			return this.encoder;
		}

		public void setEncoder(Class<Encoder> encoder) {
			this.encoder = encoder;
		}

		public Class<Contract> getContract() {
			return this.contract;
		}

		public void setContract(Class<Contract> contract) {
			this.contract = contract;
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) {
				return true;
			}
			if (o == null || getClass() != o.getClass()) {
				return false;
			}
			FeignClientConfiguration that = (FeignClientConfiguration) o;
			return this.loggerLevel == that.loggerLevel
					&& Objects.equals(this.connectTimeout, that.connectTimeout)
					&& Objects.equals(this.readTimeout, that.readTimeout)
					&& Objects.equals(this.retryer, that.retryer)
					&& Objects.equals(this.errorDecoder, that.errorDecoder)
					&& Objects.equals(this.requestInterceptors, that.requestInterceptors)
					&& Objects.equals(this.decode404, that.decode404)
					&& Objects.equals(this.encoder, that.encoder)
					&& Objects.equals(this.decoder, that.decoder)
					&& Objects.equals(this.contract, that.contract);
		}

		@Override
		public int hashCode() {
			return Objects.hash(this.loggerLevel, this.connectTimeout, this.readTimeout,
					this.retryer, this.errorDecoder, this.requestInterceptors,
					this.decode404, this.encoder, this.decoder, this.contract);
		}

	}

}
  • FeignClientProperties有個(gè)Map結(jié)構(gòu)的config,key是feign client的名稱,默認(rèn)是default,value是FeignClientConfiguration;FeignClientConfiguration包含了loggerLevel、connectTimeout、readTimeout、retryer、errorDecoder、requestInterceptors、decode404、decoder、encoder、contract屬性

小結(jié)

  • FeignClientFactoryBean實(shí)現(xiàn)了FactoryBean的getObject、getObjectType、isSingleton方法;實(shí)現(xiàn)了InitializingBean的afterPropertiesSet方法;實(shí)現(xiàn)了ApplicationContextAware的setApplicationContext方法

  • getObject調(diào)用的是getTarget方法,它從applicationContext取出FeignContext,然后構(gòu)造Feign.Builder并設(shè)置了logger、encoder、decoder、contract,之后通過configureFeign根據(jù)FeignClientProperties來(lái)進(jìn)一步配置Feign.Builder的retryer、errorDecoder、request.Options、requestInterceptors、queryMapEncoder、decode404

  • 初步配置完Feign.Builder之后再判斷是否需要loadBalance,如果需要?jiǎng)t通過loadBalance方法來(lái)設(shè)置,不需要?jiǎng)t在Client是LoadBalancerFeignClient的時(shí)候進(jìn)行unwrap

到此,關(guān)于“spring cloud中FeignClientFactoryBean的原理和作用是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI