您好,登錄后才能下訂單哦!
這篇文章主要介紹“nacos client中ConfigFilterChainManager的原理和應用”,在日常操作中,相信很多人在nacos client中ConfigFilterChainManager的原理和應用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nacos client中ConfigFilterChainManager的原理和應用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
本文主要研究一下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 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中
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
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的原理和應用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。