Scope(org.springframework.beans.factory.config.Scope)是Spring 2.0開始就有的核心的概念spring
RefreshScope(org.springframework.cloud.context.scope.refresh)是spring cloud提供的一種特殊的scope實現,用來實現配置、實例熱加載。緩存
Scope -> GenericScope -> RefreshScope微信
Scope與ApplicationContext生命週期app
AbstractBeanFactory#doGetBean建立Bean實例ide
protected <T> T doGetBean(...){ final RootBeanDefinition mbd = ... if (mbd.isSingleton()) { ... } else if (mbd.isPrototype()) ... } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {...}); ... } ... }
Singleton和Prototype是硬編碼的,並非Scope子類。 post
Scope其實是自定義擴展的接口,Scope Bean實例交由Scope本身建立,例如SessionScope是從Session中獲取實例的,ThreadScope是從ThreadLocal中獲取的,而RefreshScope是在內建緩存中獲取的。ui
@Scope 對象的實例化this
@RefreshScope 是scopeName="refresh"的 @Scope編碼
... @Scope("refresh") public @interface RefreshScope { ... }
@Scope 的註冊 AnnotatedBeanDefinitionReader#registerBeanspa
public void registerBean(...){ ... ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd); abd.setScope(scopeMetadata.getScopeName()); ... definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); }
讀取@Scope元數據, AnnotationScopeMetadataResolver#resolveScopeMetadata
public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) { AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor( annDef.getMetadata(), Scope.class); if (attributes != null) { metadata.setScopeName(attributes.getString("value")); ScopedProxyMode proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = this.defaultProxyMode; } metadata.setScopedProxyMode(proxyMode); } }
Scope實例對象經過ScopedProxyFactoryBean建立,其中經過AOP使其實現ScopedObject接口,這裏再也不展開。
RefreshScope註冊
RefreshAutoConfiguration#RefreshScopeConfiguration
@Component @ConditionalOnMissingBean(RefreshScope.class) protected static class RefreshScopeConfiguration implements BeanDefinitionRegistryPostProcessor{ ... registry.registerBeanDefinition("refreshScope", BeanDefinitionBuilder.genericBeanDefinition(RefreshScope.class) .setRole(BeanDefinition.ROLE_INFRASTRUCTURE) .getBeanDefinition()); ... }
RefreshScope extends GenericScope, 大部分邏輯在 GenericScope 中。
GenericScope#postProcessBeanFactory 中向AbstractBeanFactory註冊本身
public class GenericScope implements Scope, BeanFactoryPostProcessor...{ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.registerScope(this.name/*refresh*/, this/*RefreshScope*/); ... } }
RefreshScope 刷新過程
入口在ContextRefresher#refresh
refresh() { Map<String, Object> before = ①extract( this.context.getEnvironment().getPropertySources()); ②addConfigFilesToEnvironment(); Set<String> keys = ④changes(before, ③extract(this.context.getEnvironment().getPropertySources())).keySet(); this.context.⑤publishEvent(new EnvironmentChangeEvent(keys)); this.scope.⑥refreshAll(); }
①提取標準參數(SYSTEM,JNDI,SERVLET)以外全部參數變量
②把原來的Environment裏的參數放到一個新建的Spring Context容器下從新加載,完事以後關閉新容器
③提起更新過的參數(排除標準參數)
④比較出變動項
⑤發佈環境變動事件,接收:EnvironmentChangeListener/LoggingRebinder
⑥RefreshScope用新的環境參數從新生成Bean,從新生成的過程很簡單,清除refreshscope緩存幷銷燬Bean,下次就會從新從BeanFactory獲取一個新的實例(該實例使用新的配置)
RefreshScope#refreshAll
public void refreshAll() { <b>super.destroy();</b> this.context.publishEvent(new RefreshScopeRefreshedEvent()); }
GenericScope#destroy
public void destroy() { ... Collection<BeanLifecycleWrapper> wrappers = <b>this.cache.clear()</b>; for (BeanLifecycleWrapper wrapper : wrappers) { <b>wrapper.destroy();</b> } }
Spring Cloud Bus 如何觸發 Refresh
BusAutoConfiguration#BusRefreshConfiguration 發佈一個RefreshBusEndpoint
@Configuration @ConditionalOnClass({ Endpoint.class, RefreshScope.class }) protected static class BusRefreshConfiguration { @Configuration @ConditionalOnBean(ContextRefresher.class) @ConditionalOnProperty(value = "endpoints.spring.cloud.bus.refresh.enabled", matchIfMissing = true) protected static class BusRefreshEndpointConfiguration { @Bean public RefreshBusEndpoint refreshBusEndpoint(ApplicationContext context, BusProperties bus) { return new RefreshBusEndpoint(context, bus.getId()); } } }
RefreshBusEndpoint 會從http端口觸發廣播RefreshRemoteApplicationEvent事件
@Endpoint(id = "bus-refresh") public class RefreshBusEndpoint extends AbstractBusEndpoint { public void busRefresh() { publish(new RefreshRemoteApplicationEvent(this, getInstanceId(), null)); } }
BusAutoConfiguration#refreshListener 負責接收事件(全部配置bus的節點)
@Bean @ConditionalOnProperty(value = "spring.cloud.bus.refresh.enabled", matchIfMissing = true) @ConditionalOnBean(ContextRefresher.class) public RefreshListener refreshListener(ContextRefresher contextRefresher) { return new RefreshListener(contextRefresher); }
RefreshListener#onApplicationEvent 觸發 ContextRefresher
public void onApplicationEvent(RefreshRemoteApplicationEvent event) { Set<String> keys = contextRefresher.refresh(); }
大部分須要更新的服務須要打上@RefreshScope, EurekaClient是如何配置更新的
EurekaClientAutoConfiguration#RefreshableEurekaClientConfiguration
@Configuration @ConditionalOnRefreshScope protected static class RefreshableEurekaClientConfiguration{ @Bean @RefreshScope public EurekaClient eurekaClient(...) { return new CloudEurekaClient(manager, config, this.optionalArgs, this.context); } @Bean @RefreshScope public ApplicationInfoManager eurekaApplicationInfoManager(...) { ... return new ApplicationInfoManager(config, instanceInfo); } }
做者:黃大海
https://www.jianshu.com/p/188...
關注微信公衆號:Java技術棧,Spring Cloud 乾貨第一時間推送。在公衆號後臺回覆:cloud,還能獲取棧長整理的 Spring Cloud 系列教程,都是實戰乾貨。