background
In one day, two partners asked me about the on-demand loading mechanism of filter. It's necessary to record it here
About filter
The predecessor of filter in dubbo source code series
Today's filter of dubbo source code series
Analysis
The following attributes exist in the comment about Activate in dubbo
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface Activate { /** * Group Filter conditions. * <br /> * Contains the value given by the group parameter of {@ link extensionloader ා getactivateextension}, the extension is returned. * <br /> * If there is no Group setting, it is not filtered. */ String[] group() default {}; /** * Key Filter conditions. If the parameter Key of the URL containing {@ link extensionloader ා getactivateextension}, the extension is returned. * <p /> * Example: < br / > * The value of annotation < code > @ activate ("cache, validatioin") < / code >, * If the URL parameter of {@ link extensionloader ා getactivateextension} has < code > cache < / code > key, or < code > validatioin < / code > returns the extension. * <br/> * If it is not set, it will not be filtered. */ String[] value() default {}; /** * Sorting information, can not be provided. */ String[] before() default {}; /** * Sorting information, can not be provided. */ String[] after() default {}; /** * Sorting information, can not be provided. */ int order() default 0; }
Among them, before, after and order can be used to adjust the order of filter s
group is used to describe the effective side [provider, consumer]
public static final String PROVIDER = "provider"; public static final String CONSUMER = "consumer";
Then there is a key attribute value left
private boolean isActive(Activate activate, URL url) { String[] keys = activate.value(); if (keys == null || keys.length == 0) { return true; } for (String key : keys) { for (Map.Entry<String, String> entry : url.getParameters().entrySet()) { String k = entry.getKey(); String v = entry.getValue(); if ((k.equals(key) || k.endsWith("." + key)) && ConfigUtils.isNotEmpty(v)) { return true; } } } return false; }
It can be seen that when the value is empty or the size is 0, isActive returns true, which means that the filter will take effect.
If the parameter in the url contains the corresponding value value or. XXX [XXX is the corresponding parameter value], it will also take effect
In other scenarios, isActivate returns false, which will not take effect~
How to set the parameters of the corresponding filter???
protected <T> ServiceBean<T> registerService(RegistryConfig registryConfig, ApplicationConfig applicationConfig, ProviderConfig providerConfig, ProtocolConfig protocolConfig, Dubbo dubbo, Class<T> interfaceClazz, T t) { ServiceBean<T> ref = new ServiceBean<>(); ref.setInterface(interfaceClazz); ref.setRegistry(registryConfig); ref.setApplication(applicationConfig); ref.setProvider(providerConfig); ref.setRef(t); ref.setProtocol(protocolConfig); ref.setGroup(dubbo.getGroup()); ref.setFilter("clientProvider"); return ref; }
filter can add parameters~