目录
一、问题
二、原因
1、@Scope
2、RefreshScope 的实现原理
3、总结
三、解决方案
1、 RefreshScopeRefreshedEvent(公认最简单)
2、比较复杂的
3、使用EnvironmentChangeEvent
四、注意
一、问题
最近在运维项目的时候,出现了一个问题,在一个定时处理数据的类(TaskSchedule)里面,有用到配置文件(bootstrap.properties)中的信息,所以使用@Value()来获取配置信息,但使用@RefreshScope刷新配置信息后,发现定时任务不执行了,代码如下
@Component
@RefreshScope
public class TaskSchedule{
@Value("${distribute.source.sqlserver:false}")
Boolean readFromSqlserver;
@Scheduled(cron = "0/30 * * * * ?")
public void distribute() {
....
}
本来以为它会直接拿到刷新后的信息,但从结果看却不是,那就定位原因了,要定位原因,首先要知道 @RefreshScope的执行流程,那就只能看源码了
二、原因
大概看了一下,实现@RefreshScope 动态刷新的就需要以下几个:
• @ Scope
• @RefreshScope
• RefreshScope
• GenericScope
• Scope
• ContextRefresher
1、@Scope
一句话,@RefreshScope 能实现动态刷新全仰仗着@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是单例的。(默认)
* prototype 表示该bean是多例的,即每次使用该bean时都会新建一个对象。
* request 在一次http请求中,一个bean对应一个实例。
* session 在一个httpSession中,一个bean对应一个实例
*/
@AliasFor("value")
String scopeName() default "";
/**
* DEFAULT 不使用代理。(默认)
* NO 不使用代理,等价于DEFAULT。
* INTERFACES 使用基于接口的代理(jdk dynamic proxy)。
* TARGET_CLASS 使用基于类的代理(cglib)。
*/
ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
}
通过代码我们可以清晰的看到两个主要属性value 和 proxyMode,value就不多说了,大家平时经常用看看注解就可以。proxyMode 这个就有意思了,而这个就是@RefreshScope 实现的本质了。
我们需要关心的就是ScopedProxyMode.TARGET_CLASS 这个属性,当ScopedProxyMode 为TARGET_CLASS 的时候会给当前创建的bean 生成一个代理对象,会通过代理对象来访问,每次访问都会创建一个新的对象。
理解起来可能比较晦涩,那先来看下实现再回头来看这句话。
2、RefreshScope 的实现原理
- 先来看下@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 ,其内部就一个属性默认 ScopedProxyMode.TARGET_CLASS。知道了是通过Spring Scope 来实现的那就简单了,我们来看下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); 这个方法帮助我们来创建一个新的bean ,也就是说,@RefreshScope 在调用 刷新的时候会使用此方法来给我们创建新的对象,这样就可以通过spring 的装配机制将属性重新注入了,也就实现了所谓的动态刷新。
- 那它究竟是怎么处理老的对象,又怎么除法创建新的对象呢?
在开头我提过几个重要的类,而其中 RefreshScope extends GenericScope, GenericScope implements Scope。
所以通过查看代码,是GenericScope 实现了 Scope 最重要的 get(String name, ObjectFactory<?> objectFactory) 方法,在GenericScope 里面 包装了一个内部类 BeanLifecycleWrapperCache 来对加了 @RefreshScope 从而创建的对象进行缓存,使其在不刷新时获取的都是同一个对象。(这里你可以把 BeanLifecycleWrapperCache 想象成为一个大Map 缓存了所有@RefreshScope 标注的对象)
知道了对象是缓存的,所以在进行动态刷新的时候,只需要清除缓存,重新创建就好了。 来看代码,眼见为实,只留下关键方法:
// ContextRefresher 外面使用它来进行方法调用 ============================== 我是分割线
public synchronized Set<String> refresh() {
Set<String> keys = refreshEnvironment();
this.scope.refreshAll();
return keys;
}
// RefreshScope 内部代码 ============================== 我是分割线
@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 里的方法 ============================== 我是分割线
//进行对象获取,如果没有就创建并放入缓存
@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;
}
}
//进行缓存的数据清理
@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 就是外层调用方法用的,GenericScope 里面的 get 方法负责对象的创建和缓存,destroy 方法负责再刷新时缓存的清理工作。当然spring n内部还进行很多其他有趣的处理,有兴趣的同学可以详细看一下。
3、总结
综上所述,来总结下@RefreshScope 实现流程
- 需要动态刷新的类标注@RefreshScope 注解
- @RefreshScope 注解标注了@Scope 注解,并默认了ScopedProxyMode.TARGET_CLASS; 属性,此属性的功能就是在创建一个代理,在每次调用的时候都用它来调用GenericScope get 方法来获取对象
- 如属性发生变更会调用 ContextRefresher refresh() -》RefreshScope refreshAll() 进行缓存清理方法调用,并发送刷新事件通知 -》 GenericScope 真正的 清理方法destroy() 实现清理缓存
- 在下一次使用对象的时候,会调用GenericScope get(String name, ObjectFactory<?> objectFactory) 方法创建一个新的对象,并存入缓存中,此时新对象因为Spring 的装配机制就是新的属性了
三、解决方案
RefreshScopeRefreshedEvent(公认最简单)
@Component
@RefreshScope
public class TaskSchedule implements ApplicationListener<RefreshScopeRefreshedEvent> {
@Value("${distribute.source.sqlserver:false}")
Boolean readFromSqlserver;
@Scheduled(cron = "0/30 * * * * ?")
public void distribute() {
....
}
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent refreshScopeRefreshedEvent) {}
}
使用 RefreshScopeRefreshedEvent 从 config配置服务器成功获取并覆盖值
2、比较复杂的
/**
* Listener of Spring's lifecycle to revive Scheduler beans, when spring's
* scope is refreshed.
* <p>
* Spring is able to restart beans, when we change their properties. Such a
* beans marked with RefreshScope annotation. To make it work, spring creates
* <b>lazy</b> proxies and push them instead of real object. The issue with
* scope refresh is that right after refresh in order for such a lazy proxy
* to be actually instantiated again someone has to call for any method of it.
* <p>
* It creates a tricky case with Schedulers, because there is no bean, which
* directly call anything on any Scheduler. Scheduler lifecycle is to start
* few threads upon instantiation and schedule tasks. No other bean needs
* anything from them.
* <p>
* To overcome this, we had to create artificial method on Schedulers and call
* them, when there is a scope refresh event. This actually instantiates.
*/
@RequiredArgsConstructor
public class RefreshScopeListener implements ApplicationListener<RefreshScopeRefreshedEvent> {
private final List<RefreshScheduler> refreshSchedulers;
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
refreshSchedulers.forEach(RefreshScheduler::materializeAfterRefresh);
}
}
所以,我们定义了一个接口,它没有做任何特别的事情,但允许我们调用一个刷新的作业。
public interface RefreshScheduler {
/**
* Used after refresh context for scheduler bean initialization
*/
default void materializeAfterRefresh() {
}
}
这是实际工作,其参数from.properties
可以刷新。
public class AJob implements RefreshScheduler {
@Scheduled(cron = "${from.properties}")
public void aTask() {
// do something useful
}
}
当然 AJob bean 必须在 @Configuration 中用 @RefreshScope 标记
@Configuration
@EnableScheduling
public class SchedulingConfiguration {
@Bean
@RefreshScope
public AJob aJob() {
return new AJob();
}
}
3、使用EnvironmentChangeEvent
这个没有进行研究,如果有遇到相同问题的朋友可以研究一下,咱们再交流
四、注意
@RefreshScope 不能修饰在 @Scheduled、listener、Timmer等类中
配置刷新后会卸载类,并重新实例化类(如果类中存在计数等情况需要注意)
参考:
https://stackoverflow.com/questions/50440468/refreshscope-stops-scheduled-task/56397421#56397421