這篇文章主要介紹“nacos client中ConfigFilterChainManager的原理和應(yīng)用”,在日常操作中,相信很多人在nacos client中ConfigFilterChainManager的原理和應(yīng)用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”nacos client中ConfigFilterChainManager的原理和應(yīng)用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比橋東網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式橋東網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋橋東地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
本文主要研究一下nacos client的ConfigFilterChainManager
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方法
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) { // 根據(jù)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實(shí)現(xiàn)了IConfigFilterChain接口,其doFilter方法使用filters創(chuàng)建VirtualFilterChain,然后執(zhí)行其doFilter方法;它提供了addFilter方法,可以根據(jù)filter的order順序添加到filters中
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實(shí)現(xiàn)了IConfigFilterChain接口,它維護(hù)了一個(gè)currentPosition屬性,其doFilter方法會(huì)在currentPosition不等于additionalFilters.size()時(shí)遞增currentPosition,然后取出nextFilter把自己作為參數(shù)去執(zhí)行doFilter
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方法的第三個(gè)參數(shù)為IConfigFilterChain;其實(shí)現(xiàn)類執(zhí)行完自己的處理之后,如果要繼續(xù),可以執(zhí)行filterChain.doFilter來(lái)繼續(xù)交給下面的filter處理
ConfigFilterChainManager實(shí)現(xiàn)了IConfigFilterChain接口,其doFilter方法使用filters創(chuàng)建VirtualFilterChain,然后執(zhí)行其doFilter方法;它提供了addFilter方法,可以根據(jù)filter的order順序添加到filters中
到此,關(guān)于“nacos client中ConfigFilterChainManager的原理和應(yīng)用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!