您好,登錄后才能下訂單哦!
今天小編給大家分享一下SpringCloud @RefreshScope刷新機(jī)制源碼分析的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
用過Spring Cloud的同學(xué)都知道在使用動態(tài)配置刷新的我們要配置一個@RefreshScope 在類上才可以實現(xiàn)對象屬性的的動態(tài)更新,本著知其所以然的態(tài)度,晚上沒事兒又把這個點回顧了一下,下面就來簡單的說下自己的理解。
總覽下,實現(xiàn)@RefreshScope 動態(tài)刷新的就需要以下幾個:
@ Scope
@RefreshScope
RefreshScope
GenericScope
Scope
ContextRefresher
一句話,@RefreshScope 能實現(xiàn)動態(tài)刷新全仰仗著@Scope 這個注解,這是為什么呢?
@Scope 代表了Bean的作用域,我們來看下其中的屬性:
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Scope { /** * Alias for {@link #scopeName}. * @see #scopeName */ @AliasFor("scopeName") String value() default ""; /** * singleton 表示該bean是單例的。(默認(rèn)) * prototype 表示該bean是多例的,即每次使用該bean時都會新建一個對象。 * request 在一次http請求中,一個bean對應(yīng)一個實例。 * session 在一個httpSession中,一個bean對應(yīng)一個實例 */ @AliasFor("value") String scopeName() default ""; /** * DEFAULT 不使用代理。(默認(rèn)) * NO 不使用代理,等價于DEFAULT。 * INTERFACES 使用基于接口的代理(jdk dynamic proxy)。 * TARGET_CLASS 使用基于類的代理(cglib)。 */ ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT; }
通過代碼我們可以清晰的看到兩個主要屬性value 和 proxyMode,value就不多說了,大家平時經(jīng)常用看看注解就可以。proxyMode 這個就有意思了,而這個就是@RefreshScope 實現(xiàn)的本質(zhì)了。
我們需要關(guān)心的就是ScopedProxyMode.TARGET_CLASS 這個屬性,當(dāng)ScopedProxyMode 為TARGET_CLASS 的時候會給當(dāng)前創(chuàng)建的bean 生成一個代理對象,會通過代理對象來訪問,每次訪問都會創(chuàng)建一個新的對象。
理解起來可能比較晦澀,那先來看下實現(xiàn)再回頭來看這句話。
先來看下@RefreshScope
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Scope("refresh") @Documented public @interface RefreshScope { /** * @see Scope#proxyMode() */ ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS; }
2. 可以看出,它使用就是 @Scope ,其內(nèi)部就一個屬性默認(rèn) ScopedProxyMode.TARGET_CLASS。知道了是通過Spring Scope 來實現(xiàn)的那就簡單了,我們來看下Scope 這個接口
public interface Scope { /** * Return the object with the given name from the underlying scope, * {@link org.springframework.beans.factory.ObjectFactory#getObject() creating it} * if not found in the underlying storage mechanism. * <p>This is the central operation of a Scope, and the only operation * that is absolutely required. * @param name the name of the object to retrieve * @param objectFactory the {@link ObjectFactory} to use to create the scoped * object if it is not present in the underlying storage mechanism * @return the desired object (never {@code null}) * @throws IllegalStateException if the underlying scope is not currently active */ Object get(String name, ObjectFactory<?> objectFactory); @Nullable Object remove(String name); void registerDestructionCallback(String name, Runnable callback); @Nullable Object resolveContextualObject(String key); @Nullable String getConversationId(); }
看下接口,我們只看Object get(String name, ObjectFactory<?> objectFactory); 這個方法幫助我們來創(chuàng)建一個新的bean ,也就是說,@RefreshScope 在調(diào)用 刷新的時候會使用此方法來給我們創(chuàng)建新的對象,這樣就可以通過spring 的裝配機(jī)制將屬性重新注入了,也就實現(xiàn)了所謂的動態(tài)刷新。
那它究竟是怎么處理老的對象,又怎么除法創(chuàng)建新的對象呢?
在開頭我提過幾個重要的類,而其中 RefreshScope extends GenericScope, GenericScope implements Scope。
所以通過查看代碼,是GenericScope 實現(xiàn)了 Scope 最重要的 get(String name, ObjectFactory<?> objectFactory) 方法,在GenericScope 里面 包裝了一個內(nèi)部類 BeanLifecycleWrapperCache 來對加了 @RefreshScope 從而創(chuàng)建的對象進(jìn)行緩存,使其在不刷新時獲取的都是同一個對象。(這里你可以把 BeanLifecycleWrapperCache 想象成為一個大Map 緩存了所有@RefreshScope 標(biāo)注的對象)
知道了對象是緩存的,所以在進(jìn)行動態(tài)刷新的時候,只需要清除緩存,重新創(chuàng)建就好了。
來看代碼,眼見為實,只留下關(guān)鍵方法:
// ContextRefresher 外面使用它來進(jìn)行方法調(diào)用 ============================== 我是分割線 public synchronized Set<String> refresh() { Set<String> keys = refreshEnvironment(); this.scope.refreshAll(); return keys; } // RefreshScope 內(nèi)部代碼 ============================== 我是分割線 @ManagedOperation(description = "Dispose of the current instance of all beans in this scope and force a refresh on next method execution.") public void refreshAll() { super.destroy(); this.context.publishEvent(new RefreshScopeRefreshedEvent()); } // GenericScope 里的方法 ============================== 我是分割線 //進(jìn)行對象獲取,如果沒有就創(chuàng)建并放入緩存 @Override public Object get(String name, ObjectFactory<?> objectFactory) { BeanLifecycleWrapper value = this.cache.put(name, new BeanLifecycleWrapper(name, objectFactory)); locks.putIfAbsent(name, new ReentrantReadWriteLock()); try { return value.getBean(); } catch (RuntimeException e) { this.errors.put(name, e); throw e; } } //進(jìn)行緩存的數(shù)據(jù)清理 @Override public void destroy() { List<Throwable> errors = new ArrayList<Throwable>(); Collection<BeanLifecycleWrapper> wrappers = this.cache.clear(); for (BeanLifecycleWrapper wrapper : wrappers) { try { Lock lock = locks.get(wrapper.getName()).writeLock(); lock.lock(); try { wrapper.destroy(); } finally { lock.unlock(); } } catch (RuntimeException e) { errors.add(e); } } if (!errors.isEmpty()) { throw wrapIfNecessary(errors.get(0)); } this.errors.clear(); }
通過觀看源代碼我們得知,我們截取了三個片段所得之,ContextRefresher 就是外層調(diào)用方法用的,GenericScope 里面的 get 方法負(fù)責(zé)對象的創(chuàng)建和緩存,destroy 方法負(fù)責(zé)再刷新時緩存的清理工作。
以上就是“SpringCloud @RefreshScope刷新機(jī)制源碼分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。