From d9e0810ba9497914a4298852dfc6e4c205a05ba6 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 25 Apr 2018 20:50:29 +0900 Subject: [PATCH] ing --- .../configuration/ContainerConfiguration.java | 3 +- .../overflow/container/service/Service.java | 47 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java b/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java index 5c79186..0a60188 100644 --- a/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java +++ b/src/main/java/com/loafle/overflow/container/configuration/ContainerConfiguration.java @@ -9,7 +9,6 @@ import com.google.gson.Gson; import com.loafle.commons.rpc.protocol.RPCServerCodec; import com.loafle.commons.rpc.protocol.json.JSONRPCServerCodec; import com.loafle.commons.rpc.registry.RPCRegistry; -import com.loafle.commons.rpc.registry.pojo.POJORPCRegistry; import com.loafle.commons.server.Server; import com.loafle.commons.server.socket.handler.codec.SocketServerProtocolHandler; import com.loafle.overflow.container.Container; @@ -72,7 +71,7 @@ public class ContainerConfiguration { @Bean public RPCRegistry rpcRegistry() { - return new POJORPCRegistry(); + return new RPCRegistry(); } @Bean(Server.CHANNEL_INITIALIZER) diff --git a/src/main/java/com/loafle/overflow/container/service/Service.java b/src/main/java/com/loafle/overflow/container/service/Service.java index e7cdce7..a5576a9 100644 --- a/src/main/java/com/loafle/overflow/container/service/Service.java +++ b/src/main/java/com/loafle/overflow/container/service/Service.java @@ -1,9 +1,12 @@ package com.loafle.overflow.container.service; +import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; import com.loafle.commons.rpc.RPCException; import com.loafle.commons.rpc.registry.RPCRegistry; +import com.loafle.overflow.core.annotation.ProbeAPI; import com.loafle.overflow.core.annotation.RPCService; import org.slf4j.Logger; @@ -41,12 +44,44 @@ public class Service implements InitializingBean, ApplicationContextAware { } services.forEach((name, bean) -> { - logger.debug("bean {}", bean.getClass().getName()); - try { - this.rpcRegistry.registerService(bean, bean.getClass(), name); - } catch (RPCException e) { - logger.error("RPCRegistry", e); - } + this.registerRPCService(bean, name); }); } + + private void registerRPCService(Object bean, String name) { + try { + Class clazz = bean.getClass(); + Method[] methods = clazz.getMethods(); + + Map methodMap = new HashMap<>(); + + for (Method method : methods) { + String methodName = this.getRPCMethodName(method); + if (null == methodName) { + continue; + } + methodMap.put(methodName, method); + } + + if (0 == methodMap.size()) { + return; + } + + this.rpcRegistry.registerService(bean, name, methodMap); + } catch (RPCException e) { + logger.error("RPCRegistry", e); + } + } + + private String getRPCMethodName(Method method) { + ProbeAPI a = method.getAnnotation(ProbeAPI.class); + if (null == a) { + return null; + } + String name = a.value(); + if ("" == name.trim()) { + name = method.getName(); + } + return name; + } } \ No newline at end of file