前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊nacos client的ConfigFilterChainManager

聊聊nacos client的ConfigFilterChainManager

作者头像
code4it
发布2019-10-22 16:56:01
5850
发布2019-10-22 16:56:01
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下nacos client的ConfigFilterChainManager

IConfigFilterChain

nacos-1.1.3/api/src/main/java/com/alibaba/nacos/api/config/filter/IConfigFilterChain.java

代码语言:javascript
复制
public interface IConfigFilterChain {
    /**
     * Filter aciton
     *
     * @param request  request
     * @param response response
     * @throws NacosException NacosException
     */
    void doFilter(IConfigRequest request, IConfigResponse response) throws NacosException;

}
  • IConfigFilterChain接口定义了doFilter方法

ConfigFilterChainManager

nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java

代码语言:javascript
复制
public class ConfigFilterChainManager implements IConfigFilterChain {

    private List<IConfigFilter> filters = Lists.newArrayList();

    public synchronized ConfigFilterChainManager addFilter(IConfigFilter filter) {
        // 根据order大小顺序插入
        int i = 0;
        while (i < this.filters.size()) {
            IConfigFilter currentValue = this.filters.get(i);
            if (currentValue.getFilterName().equals(filter.getFilterName())) {
                break;
            }
            if (filter.getOrder() >= currentValue.getOrder() && i < this.filters.size()) {
                i++;
            } else {
                this.filters.add(i, filter);
                break;
            }
        }

        if (i == this.filters.size()) {
            this.filters.add(i, filter);
        }
        return this;
    }

    @Override
    public void doFilter(IConfigRequest request, IConfigResponse response) throws NacosException {
        new VirtualFilterChain(this.filters).doFilter(request, response);
    }

    //......
}
  • ConfigFilterChainManager实现了IConfigFilterChain接口,其doFilter方法使用filters创建VirtualFilterChain,然后执行其doFilter方法;它提供了addFilter方法,可以根据filter的order顺序添加到filters中

VirtualFilterChain

nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java

代码语言:javascript
复制
public class ConfigFilterChainManager implements IConfigFilterChain {

//......

    private static class VirtualFilterChain implements IConfigFilterChain {

        private final List<? extends IConfigFilter> additionalFilters;

        private int currentPosition = 0;

        public VirtualFilterChain(List<? extends IConfigFilter> additionalFilters) {
            this.additionalFilters = additionalFilters;
        }

        @Override
        public void doFilter(final IConfigRequest request, final IConfigResponse response) throws NacosException {
            if (this.currentPosition != this.additionalFilters.size()) {
                this.currentPosition++;
                IConfigFilter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
                nextFilter.doFilter(request, response, this);
            }
        }
    }

    //......
}
  • VirtualFilterChain实现了IConfigFilterChain接口,它维护了一个currentPosition属性,其doFilter方法会在currentPosition不等于additionalFilters.size()时递增currentPosition,然后取出nextFilter把自己作为参数去执行doFilter

IConfigFilter

nacos-1.1.3/api/src/main/java/com/alibaba/nacos/api/config/filter/IConfigFilter.java

代码语言:javascript
复制
public interface IConfigFilter {
    /**
     * Init
     *
     * @param filterConfig Filter Config
     */
    void init(IFilterConfig filterConfig);

    /**
     * do filter
     *
     * @param request     request
     * @param response    response
     * @param filterChain filter Chain
     * @throws NacosException exception
     */
    void doFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain filterChain)
            throws NacosException;

    /**
     * deploy
     */
    void deploy();

    /**
     * Get order
     *
     * @return order number
     */
    int getOrder();

    /**
     * Get filterName
     *
     * @return filter name
     */
    String getFilterName();

}
  • IConfigFilter接口的doFilter方法的第三个参数为IConfigFilterChain;其实现类执行完自己的处理之后,如果要继续,可以执行filterChain.doFilter来继续交给下面的filter处理

小结

ConfigFilterChainManager实现了IConfigFilterChain接口,其doFilter方法使用filters创建VirtualFilterChain,然后执行其doFilter方法;它提供了addFilter方法,可以根据filter的order顺序添加到filters中

doc

  • ConfigFilterChainManager
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • IConfigFilterChain
  • ConfigFilterChainManager
  • VirtualFilterChain
  • IConfigFilter
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档