This commit is contained in:
crusader 2018-04-25 20:50:29 +09:00
parent 11e08c8e9a
commit d9e0810ba9
2 changed files with 42 additions and 8 deletions

View File

@ -9,7 +9,6 @@ import com.google.gson.Gson;
import com.loafle.commons.rpc.protocol.RPCServerCodec; import com.loafle.commons.rpc.protocol.RPCServerCodec;
import com.loafle.commons.rpc.protocol.json.JSONRPCServerCodec; import com.loafle.commons.rpc.protocol.json.JSONRPCServerCodec;
import com.loafle.commons.rpc.registry.RPCRegistry; 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.Server;
import com.loafle.commons.server.socket.handler.codec.SocketServerProtocolHandler; import com.loafle.commons.server.socket.handler.codec.SocketServerProtocolHandler;
import com.loafle.overflow.container.Container; import com.loafle.overflow.container.Container;
@ -72,7 +71,7 @@ public class ContainerConfiguration {
@Bean @Bean
public RPCRegistry rpcRegistry() { public RPCRegistry rpcRegistry() {
return new POJORPCRegistry(); return new RPCRegistry();
} }
@Bean(Server.CHANNEL_INITIALIZER) @Bean(Server.CHANNEL_INITIALIZER)

View File

@ -1,9 +1,12 @@
package com.loafle.overflow.container.service; package com.loafle.overflow.container.service;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.loafle.commons.rpc.RPCException; import com.loafle.commons.rpc.RPCException;
import com.loafle.commons.rpc.registry.RPCRegistry; import com.loafle.commons.rpc.registry.RPCRegistry;
import com.loafle.overflow.core.annotation.ProbeAPI;
import com.loafle.overflow.core.annotation.RPCService; import com.loafle.overflow.core.annotation.RPCService;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -41,12 +44,44 @@ public class Service implements InitializingBean, ApplicationContextAware {
} }
services.forEach((name, bean) -> { services.forEach((name, bean) -> {
logger.debug("bean {}", bean.getClass().getName()); this.registerRPCService(bean, name);
try {
this.rpcRegistry.registerService(bean, bean.getClass(), name);
} catch (RPCException e) {
logger.error("RPCRegistry", e);
}
}); });
} }
private void registerRPCService(Object bean, String name) {
try {
Class<?> clazz = bean.getClass();
Method[] methods = clazz.getMethods();
Map<String, Method> 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;
}
} }