pom.xml
1 2 3 4 5
| <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.22</version> </dependency>
|
简单实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| @Slf4j @Activate( group = {"provider"}, order = 10010 ) public class DubboInvokeProxy implements Filter {
private Instance currentInstance; public InvokeServiceFilter() { ExtensionFactory beanFactory = ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension(); JedisCluster jedisCluster = beanFactory.getExtension(JedisCluster.class, "jedisCluster"); MongoTemplate mongoTemplate = beanFactory.getExtension(MongoTemplate.class, "mongoTemplate"); cacheStores.put(Cache.NosqlType.REDIS, new RedisCacheStore(jedisCluster)); cacheStores.put(Cache.NosqlType.MONGODB, new MongoCacheStore(mongoTemplate)); }
@SneakyThrows @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { if (!invoker.getInterface().isAssignableFrom(DataService.class)) { return invoker.invoke(invocation); } Class<?> cls = invoker.getInterface(); Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes()); Class<?>[] parameterTypes = invocation.getParameterTypes(); Object[] arguments = invocation.getArguments();
if (method.isAnnotationPresent(Cacheable.class)) { Method cacheMethod = org.springframework.util.ReflectionUtils.findMethod(CacheStore.class, method.getName(), parameterTypes); Object value = org.springframework.util.ReflectionUtils.invokeMethod(cacheMethod, cacheStore, arguments); return AsyncRpcResult.newDefaultAsyncResult(value, invocation); } return invoker.invoke(invocation); }
}
|
参考https://gentryhuang.com/posts/c5f1bd62/