Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]provider consumer 过滤器 #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.xxl.rpc.filter;

/**
* @author weizibin
* @since 2020/2/4 下午2:55
*/
public interface ConsumerFilter extends Filter {

}
14 changes: 14 additions & 0 deletions xxl-rpc-core/src/main/java/com/xxl/rpc/filter/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xxl.rpc.filter;

import com.xxl.rpc.remoting.net.params.XxlRpcRequest;
import com.xxl.rpc.remoting.net.params.XxlRpcResponse;

/**
* @author weizibin
* @since 2020/2/4 下午2:55
*/
public interface Filter {

XxlRpcResponse doFilter(XxlRpcRequest request, FilterChain chain) throws Exception;

}
52 changes: 52 additions & 0 deletions xxl-rpc-core/src/main/java/com/xxl/rpc/filter/FilterChain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.xxl.rpc.filter;

import com.xxl.rpc.remoting.net.params.XxlRpcRequest;
import com.xxl.rpc.remoting.net.params.XxlRpcResponse;

import java.util.List;

/**
* @author weizibin
* @since 2020/2/4 下午5:40
*/
public class FilterChain {

private final int index;

private final List<Filter> filters;
private final Delegate delegate;

public FilterChain(List<Filter> filters, Delegate delegate) {
this.filters = filters;
this.delegate = delegate;
this.index = 0;
}

private FilterChain(FilterChain parent, int index) {
this.filters = parent.getFilters();
this.delegate = parent.getDelegate();
this.index = index;
}

public List<Filter> getFilters() {
return filters;
}

public Delegate getDelegate() {
return delegate;
}

public XxlRpcResponse doFilter(XxlRpcRequest request) throws Exception {
if (this.index < filters.size()) {
Filter filter = filters.get(this.index);
FilterChain chain = new FilterChain(this, this.index + 1);
return filter.doFilter(request, chain);
} else {
return delegate.doInvoke(request);
}
}

public interface Delegate {
XxlRpcResponse doInvoke(XxlRpcRequest request) throws Exception;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.xxl.rpc.filter;

/**
* @author weizibin
* @since 2020/2/4 下午2:55
*/
public interface ProviderFilter extends Filter {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.xxl.rpc.filter.impl;

import com.xxl.rpc.filter.ConsumerFilter;
import com.xxl.rpc.filter.FilterChain;
import com.xxl.rpc.remoting.invoker.generic.XxlRpcGenericService;
import com.xxl.rpc.remoting.net.params.XxlRpcRequest;
import com.xxl.rpc.remoting.net.params.XxlRpcResponse;
import com.xxl.rpc.util.ClassUtil;

/**
* @author weizibin
* @since 2020/2/5 下午3:18
*/
public class GenericFilter implements ConsumerFilter {

@Override
public XxlRpcResponse doFilter(XxlRpcRequest request, FilterChain chain) throws Exception {
String className = request.getClassName();
String methodName = request.getMethodName();
Object[] args = request.getParameters();
if (className.equals(XxlRpcGenericService.class.getName()) && methodName.equals("invoke")) {
Class<?>[] paramTypes = null;
if (args[3]!=null) {
String[] paramTypes_str = (String[]) args[3];
if (paramTypes_str.length > 0) {
paramTypes = new Class[paramTypes_str.length];
for (int i = 0; i < paramTypes_str.length; i++) {
paramTypes[i] = ClassUtil.resolveClass(paramTypes_str[i]);
}
}
}

request.setClassName((String) args[0]);
request.setVersion((String) args[1]);
request.setMethodName((String) args[2]);
request.setParameterTypes(paramTypes);
request.setParameters((Object[]) args[4]);
}
return chain.doFilter(request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.xxl.rpc.filter.impl;

import com.xxl.rpc.filter.ConsumerFilter;
import com.xxl.rpc.filter.FilterChain;
import com.xxl.rpc.remoting.invoker.reference.XxlRpcReferenceBean;
import com.xxl.rpc.remoting.net.params.XxlRpcRequest;
import com.xxl.rpc.remoting.net.params.XxlRpcResponse;
import com.xxl.rpc.util.XxlRpcException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author weizibin
* @since 2020/2/5 下午3:18
*/
public class MethodFilter implements ConsumerFilter {

private static final Logger logger = LoggerFactory.getLogger(XxlRpcReferenceBean.class);

@Override
public XxlRpcResponse doFilter(XxlRpcRequest request, FilterChain chain) throws Exception {
String className = request.getClassName();
String methodName = request.getMethodName();
// filter method like "Object.toString()"
if (className.equals(Object.class.getName())) {
logger.info(">>>>>>>>>>> xxl-rpc proxy class-method not support [{}#{}]", className, methodName);
throw new XxlRpcException("xxl-rpc proxy class-method not support");
}
return chain.doFilter(request);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xxl.rpc.remoting.invoker.impl;

import com.xxl.rpc.filter.Filter;
import com.xxl.rpc.registry.ServiceRegistry;
import com.xxl.rpc.remoting.invoker.XxlRpcInvokerFactory;
import com.xxl.rpc.remoting.invoker.annotation.XxlRpcReference;
Expand All @@ -14,10 +15,13 @@
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -33,7 +37,11 @@ public class XxlRpcSpringInvokerFactory extends InstantiationAwareBeanPostProces

private Class<? extends ServiceRegistry> serviceRegistryClass; // class.forname
private Map<String, String> serviceRegistryParam;
private List<Filter> filters = Collections.EMPTY_LIST;

public void setFilters(List<Filter> filters) {
this.filters = filters;
}

public void setServiceRegistryClass(Class<? extends ServiceRegistry> serviceRegistryClass) {
this.serviceRegistryClass = serviceRegistryClass;
Expand Down Expand Up @@ -87,7 +95,7 @@ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessEx
referenceBean.setAccessToken(rpcReference.accessToken());
referenceBean.setInvokeCallback(null);
referenceBean.setInvokerFactory(xxlRpcInvokerFactory);

referenceBean.setFilters(filters);

// get proxyObj
Object serviceProxy = null;
Expand Down
Loading