RefreshScope源码分析

简介: springcloud的git配置中可以使用@RefreshScope + POST [/actuator/refresh]调用actuator的refresh来实现配置的热更新。 @RefreshScope springcloud描述: A Spring @Bean that is marked.

springcloud的git配置中可以使用@RefreshScope + POST [/actuator/refresh]调用actuator的refresh来实现配置的热更新。

@RefreshScope

springcloud描述:

A Spring @Bean that is marked as @RefreshScope will get special treatment when there is a configuration change. This addresses 
the problem of stateful beans that only get their configuration injected when they are initialized. For instance if a DataSource has open connections when the 
database URL is changed via the Environment, we probably want the holders of those connections to be able to complete what they are doing. Then the next time someone borrows a 
connection from the pool he gets one with the new URL.

Refresh scope beans are lazy proxies that initialize when they are used (i.e. when a method is called), and the scope acts as a cache of initialized values. To force a 
bean to re-initialize on the next method call you just need to invalidate its cache entry.

@RefreshScope标记的Bean是延迟加载的,每次访问时都会被强制初始化,这一点在RefreshScope类的doc中也有说明:

A Scope implementation that allows for beans to be refreshed dynamically at runtime
 * (see {@link #refresh(String)} and {@link #refreshAll()}). If a bean is refreshed then
 * the next time the bean is accessed (i.e. a method is executed) a new instance is
 * created. 

refresh scope bean则是在使用时(即调用方法时)初始化的惰性代理,充当已初始化值的缓存。如果要强制被标记bean在下一个方法调用上重新初始化,只需使其缓存项无效即可。

RefreshScope类:

/**
 * <p>
 * A Scope implementation that allows for beans to be refreshed dynamically at runtime
 * (see {@link #refresh(String)} and {@link #refreshAll()}). If a bean is refreshed then
 * the next time the bean is accessed (i.e. a method is executed) a new instance is
 * created. All lifecycle methods are applied to the bean instances, so any destruction
 * callbacks that were registered in the bean factory are called when it is refreshed, and
 * then the initialization callbacks are invoked as normal when the new instance is
 * created. A new bean instance is created from the original bean definition, so any
 * externalized content (property placeholders or expressions in string literals) is
 * re-evaluated when it is created.
 * </p>
 *
 * <p>
 * Note that all beans in this scope are <em>only</em> initialized when first accessed, so
 * the scope forces lazy initialization semantics. The implementation involves creating a
 * proxy for every bean in the scope, so there is a flag
 * {@link #setProxyTargetClass(boolean) proxyTargetClass} which controls the proxy
 * creation, defaulting to JDK dynamic proxies and therefore only exposing the interfaces
 * implemented by a bean. If callers need access to other methods then the flag needs to
 * be set (and CGLib present on the classpath). Because this scope automatically proxies
 * all its beans, there is no need to add <code>&lt;aop:auto-proxy/&gt;</code> to any bean
 * definitions.
 * </p>
 *
 * <p>
 * The scoped proxy approach adopted here has a side benefit that bean instances are
 * automatically {@link Serializable}, and can be sent across the wire as long as the
 * receiver has an identical application context on the other side. To ensure that the two
 * contexts agree that they are identical they have to have the same serialization id. One
 * will be generated automatically by default from the bean names, so two contexts with
 * the same bean names are by default able to exchange beans by name. If you need to
 * override the default id then provide an explicit {@link #setId(String) id} when the
 * Scope is declared.
 * </p>
 *
 * @author Dave Syer
 *
 * @since 3.1
 *
 */
@ManagedResource
public class RefreshScope extends GenericScope
        implements ApplicationContextAware, Ordered {

    private ApplicationContext context;
    private BeanDefinitionRegistry registry;
    private boolean eager = true;
    private int order = Ordered.LOWEST_PRECEDENCE - 100;

    /**
     * Create a scope instance and give it the default name: "refresh".
     */
    public RefreshScope() {
        super.setName("refresh");
    }

    @ManagedOperation(description = "Dispose of the current instance of bean name provided and force a refresh on next method execution.")
    public boolean refresh(String name) {
        if (!name.startsWith(SCOPED_TARGET_PREFIX)) {
            // User wants to refresh the bean with this name but that isn't the one in the
            // cache...
            name = SCOPED_TARGET_PREFIX + name;
        }
        // Ensure lifecycle is finished if bean was disposable
        if (super.destroy(name)) {
            this.context.publishEvent(new RefreshScopeRefreshedEvent(name));
            return true;
        }
        return false;
    }

    @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());
    }
}

当调用refresh方法时会调用super.destroy(String name)方法,父类实现如下:

public class GenericScope implements Scope, BeanFactoryPostProcessor,
        BeanDefinitionRegistryPostProcessor, DisposableBean {
        
    private BeanLifecycleWrapperCache cache = new BeanLifecycleWrapperCache(
            new StandardScopeCache());
            
    /**
     * Destroy the named bean (i.e. flush it from the cache by default).
     *
     * @param name the bean name to flush
     * @return true if the bean was already cached, false otherwise
     */
    protected boolean destroy(String name) {
        BeanLifecycleWrapper wrapper = this.cache.remove(name);
        if (wrapper != null) {
            Lock lock = locks.get(wrapper.getName()).writeLock();
            lock.lock();
            try {
                wrapper.destroy();
            }
            finally {
                lock.unlock();
            }
            this.errors.remove(name);
            return true;
        }
        return false;
    }
    
    @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();
    }
}

    @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 Object remove(String name) {
        BeanLifecycleWrapper value = this.cache.remove(name);
        if (value == null) {
            return null;
        }
        // Someone might have added another object with the same key, but we
        // keep the method contract by removing the
        // value we found anyway
        return value.getBean();
    }

BeanLifecycleWrapperCache用于缓存被标记的bean,当destroy执行时,会从BeanLifecycleWrapperCache中删除或全部清除bean。在调用get方法时会重新new一个对象放入缓存中。

跟踪代码,可得到调用refresh时:

/**
 * @author Dave Syer
 * @author Venil Noronha
 */
@Endpoint(id = "refresh")
public class RefreshEndpoint {

    private ContextRefresher contextRefresher;

    public RefreshEndpoint(ContextRefresher contextRefresher) {
        this.contextRefresher = contextRefresher;
    }

    @WriteOperation
    public Collection<String> refresh() {
        Set<String> keys = contextRefresher.refresh();
        return keys;
    }
}

得到:

public class ContextRefresher {
    public synchronized Set<String> 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(context, keys));
        this.scope.refreshAll();
        return keys;
    }
}

是此处调用了RefreshScope的refreshAll方法。

Endpoint

@Endpoint用于标记为actuator终端,提供应用运行信息:

Identifies a type as being an actuator endpoint that provides information about the
 * running application. Endpoints can be exposed over a variety of technologies including
 * JMX and HTTP.

例如:

/**
 * @author Dave Syer
 * @author Venil Noronha
 */
@Endpoint(id = "refresh")
public class RefreshEndpoint {

    private ContextRefresher contextRefresher;

    public RefreshEndpoint(ContextRefresher contextRefresher) {
        this.contextRefresher = contextRefresher;
    }

    @WriteOperation
    public Collection<String> refresh() {
        Set<String> keys = contextRefresher.refresh();
        return keys;
    }

}

再看@Endpoint注解:

/**
 * Identifies a type as being an actuator endpoint that provides information about the
 * running application. Endpoints can be exposed over a variety of technologies including
 * JMX and HTTP.
 * <p>
 * Most {@code @Endpoint} classes will declare one or more
 * {@link ReadOperation @ReadOperation}, {@link WriteOperation @WriteOperation},
 * {@link DeleteOperation @DeleteOperation} annotated methods which will be automatically
 * adapted to the exposing technology (JMX, Spring MVC, Spring WebFlux, Jersey etc.).
 * <p>
 * {@code @Endpoint} represents the lowest common denominator for endpoints and
 * intentionally limits the sorts of operation methods that may be defined in order to
 * support the broadest possible range of exposure technologies. If you need deeper
 * support for a specific technology you can either write an endpoint that is
 * {@link FilteredEndpoint filtered} to a certain technology, or provide
 * {@link EndpointExtension extension} for the broader endpoint.
 *
 * @author Andy Wilkinson
 * @author Phillip Webb
 * @since 2.0.0
 * @see EndpointExtension
 * @see FilteredEndpoint
 * @see EndpointDiscoverer
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Endpoint {

    /**
     * The id of the endpoint.
     * @return the id
     */
    String id() default "";

    /**
     * If the endpoint should be enabled or disabled by default.
     * @return {@code true} if the endpoint is enabled by default
     */
    boolean enableByDefault() default true;

}
目录
相关文章
Java 通过IP获取对应的国家省份城市经纬度(离线文件方案)
一. 除了调用接口查询城市, 还可以通过离线文件查询城市, 使用GeoLite2 City库 二. 离线库下载地址: https://dev.maxmind.com/geoip/geoip2/geolite2/ 点击如下位置下载压缩文件 文件解压后有一个文件名为GeoLite2-City.
|
机器学习/深度学习 传感器 运维
综述:弱监督下的异常检测算法
# 一、前言 文章标题是: Weakly Supervised Anomaly Detection: A Survey 这是一篇针对“弱监督”异常检测的综述。 其中弱监督异常检测 简称为 WSAD - 论文链接:https://arxiv.org/abs/2302.04549 - 代码链接:https://github.com/yzhao062/wsad # 二、问题 针对异常检测问题,其
1086 1
综述:弱监督下的异常检测算法
|
网络安全 数据安全/隐私保护 Windows
windows通过finalshell连接服务器
通过finnalshell连接服务器的流程
windows通过finalshell连接服务器
|
5月前
|
监控 负载均衡 Dubbo
SpringBoot整合Dubbo,构建高性能分布式系统
Dubbo是阿里巴巴开源的一款高性能、轻量级的 Java RPC 框架,主要功能包括:面向接口的远程方法调用、智能负载均衡、服务自动注册与发现、高可用性、运行期流量调度、可视化的服务治理。
374 13
|
存储 分布式计算 大数据
大数据之路:阿里巴巴大数据实践——大数据领域建模综述
数据建模解决数据冗余、资源浪费、一致性缺失及开发低效等核心问题,通过分层设计提升性能10~100倍,优化存储与计算成本,保障数据质量并提升开发效率。相比关系数据库,数据仓库采用维度建模与列式存储,支持高效分析。阿里巴巴采用Kimball模型与分层架构,实现OLAP场景下的高性能计算与实时离线一体化。
1329 0
|
图形学
Unity UGUI拖拽移动
本文介绍了两种UI拖拽实现方式:精准拖拽和克隆拖拽。精准拖拽通过计算鼠标点击点与UI中心的偏移量,使UI跟随鼠标移动,适用于需要精确控制的场景。代码中通过`IBeginDragHandler`、`IDragHandler`和`IEndDragHandler`接口实现拖拽逻辑。克隆拖拽则在拖拽时克隆一个UI对象,使其跟随鼠标移动,适合视觉效果需求较高的场景。代码中同样使用上述接口,并在拖拽结束时销毁克隆对象。具体实现可参考提供的代码示例。
791 10
|
JSON 数据可视化 测试技术
python+requests接口自动化框架的实现
通过以上步骤,我们构建了一个基本的Python+Requests接口自动化测试框架。这个框架具有良好的扩展性,可以根据实际需求进行功能扩展和优化。它不仅能提高测试效率,还能保证接口的稳定性和可靠性,为软件质量提供有力保障。
1134 7
|
前端开发 Java 数据库
Java Web开发全攻略:构建高性能Web应用
【4月更文挑战第2天】Java Web开发围绕Servlet和JSP展开,结合Spring/Spring Boot、Struts2、Hibernate等框架构建高性能应用。数据库选择和优化、前端技术如Angular/React/Vue.js以及CI/CD实践是关键。从电子商务到企业系统,Java Web技术为企业与用户互动提供强大平台。持续学习和适应新技术,以应对Web开发挑战。
753 1
Java Web开发全攻略:构建高性能Web应用
|
缓存 Java 微服务
Spring Cloud中@RefreshScope实现动态刷新的原理
Spring Cloud中@RefreshScope实现动态刷新的原理