nacosclient中ConfigFilterChainManager的原理和应用
这篇文章主要介绍“nacos client中ConfigFilterChainManager的原理和应用”,在日常操作中,相信很多人在nacos client中ConfigFilterChainManager的原理和应用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”nacos client中ConfigFilterChainManager的原理和应用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联建站-专业网站定制、快速模板网站建设、高性价比桥东网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式桥东网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖桥东地区。费用合理售后完善,10年实体公司更值得信赖。
序
本文主要研究一下nacos client的ConfigFilterChainManager
IConfigFilterChain
nacos-1.1.3/api/src/main/java/com/alibaba/nacos/api/config/filter/IConfigFilterChain.java
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
public class ConfigFilterChainManager implements IConfigFilterChain { private Listfilters = 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
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
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中
到此,关于“nacos client中ConfigFilterChainManager的原理和应用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
名称栏目:nacosclient中ConfigFilterChainManager的原理和应用
本文路径:http://azwzsj.com/article/ipchgd.html