ing
This commit is contained in:
parent
2b4aeea3e6
commit
8625e10e00
|
@ -1,12 +1,14 @@
|
|||
package com.loafle.overflow.central.commons.service;
|
||||
|
||||
import com.loafle.overflow.core.exception.OverflowException;
|
||||
|
||||
public interface MessagePublisher {
|
||||
void publishToDomainMembers(final long domainID, final String method, final Object... params);
|
||||
void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params);
|
||||
void publishToDomainMembers(final long domainID, final String method, final Object... params) throws OverflowException;
|
||||
void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) throws OverflowException;
|
||||
|
||||
void publishToMember(final String memberID, final String method, final Object... params);
|
||||
void publishToMemberSession(final String memberSessionID, final String method, final Object... params);
|
||||
void publishToMember(final String memberID, final String method, final Object... params) throws OverflowException;
|
||||
void publishToMemberSession(final String memberSessionID, final String method, final Object... params) throws OverflowException;
|
||||
|
||||
void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params);
|
||||
void publishToProbe(final String probeKey, final String method, final Object... params);
|
||||
void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException;
|
||||
void publishToProbe(final String probeKey, final String method, final Object... params) throws OverflowException;
|
||||
}
|
||||
|
|
|
@ -37,83 +37,61 @@ public class CentralDiscoveryService implements DiscoveryService {
|
|||
// this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey());
|
||||
}
|
||||
|
||||
// public boolean testDiscovery(int types, String obj) {
|
||||
|
||||
// Domain domain = new Domain();
|
||||
// domain.setId(1);
|
||||
|
||||
// // int typeInt = Integer.valueOf(types);
|
||||
// switch (types) {
|
||||
// case 1 :
|
||||
// messagePublisher.publishToDomainMembers(domain, "DiscoveryService.discoveryIngHost", obj);
|
||||
// break;
|
||||
// case 2 :
|
||||
// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj);
|
||||
// break;
|
||||
// case 3 :
|
||||
// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj);
|
||||
// break;
|
||||
// }
|
||||
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
@WebappAPI
|
||||
public void discoverZone(String probeID, DiscoverZone discoveryZone) {
|
||||
public void discoverZone(String probeID, DiscoverZone discoveryZone) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone);
|
||||
}
|
||||
|
||||
@WebappAPI
|
||||
public void discoverHost(String probeID, Zone zone, DiscoverHost discoveryHost) {
|
||||
public void discoverHost(String probeID, Zone zone, DiscoverHost discoveryHost) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost);
|
||||
}
|
||||
|
||||
@WebappAPI
|
||||
public void discoverPort(String probeID, Host host, DiscoverPort discoveryPort) {
|
||||
public void discoverPort(String probeID, Host host, DiscoverPort discoveryPort) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort);
|
||||
}
|
||||
|
||||
@WebappAPI
|
||||
public void discoverService(String probeID, Port port, com.loafle.overflow.model.discovery.DiscoverService discoveryService) {
|
||||
public void discoverService(String probeID, Port port, com.loafle.overflow.model.discovery.DiscoverService discoveryService) throws OverflowException {
|
||||
String requesterSessionID = SessionMetadata.getSessionID();
|
||||
|
||||
messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoverStart(String requesterSessionID, Date startDate) {
|
||||
public void discoverStart(String requesterSessionID, Date startDate) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStart", startDate);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoverStop(String requesterSessionID, Date stopDate) {
|
||||
public void discoverStop(String requesterSessionID, Date stopDate) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveryStop", stopDate);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredZone(String requesterSessionID, Zone zone) {
|
||||
public void discoveredZone(String requesterSessionID, Zone zone) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredHost(String requesterSessionID, Host host) {
|
||||
public void discoveredHost(String requesterSessionID, Host host) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredPort(String requesterSessionID, Port port) {
|
||||
public void discoveredPort(String requesterSessionID, Port port) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port);
|
||||
}
|
||||
|
||||
@ProbeAPI
|
||||
public void discoveredService(String requesterSessionID, com.loafle.overflow.model.discovery.Service service) {
|
||||
public void discoveredService(String requesterSessionID, com.loafle.overflow.model.discovery.Service service) throws OverflowException {
|
||||
messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredService", service);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,8 @@ package com.loafle.overflow.central.proxy;
|
|||
|
||||
import com.google.gson.internal.Primitives;
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.loafle.overflow.central.proxy.exception.InvalidParameterException;
|
||||
import com.loafle.overflow.central.proxy.exception.InvalidRequestException;
|
||||
import com.loafle.overflow.central.proxy.exception.NoSuchMethodException;
|
||||
import com.loafle.overflow.central.proxy.exception.NoSuchServiceException;
|
||||
import io.grpc.Status;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
import com.loafle.overflow.core.exception.OverflowException;
|
||||
|
||||
import org.codehaus.jackson.map.DeserializationConfig;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.JavaType;
|
||||
|
@ -101,6 +97,7 @@ public class ServiceInvoker {
|
|||
private Object bean;
|
||||
private Map<String, MethodCache> methodCacheMap;
|
||||
}
|
||||
|
||||
private static class MethodCache {
|
||||
private Method method;
|
||||
private Class returnClazz;
|
||||
|
@ -108,7 +105,7 @@ public class ServiceInvoker {
|
|||
}
|
||||
}
|
||||
|
||||
private Cache getServiceCache(String serviceName) throws NoSuchServiceException {
|
||||
private Cache getServiceCache(String serviceName) throws java.lang.NoSuchMethodException {
|
||||
Cache serviceCache = serviceCacheMap.get(serviceName);
|
||||
if (null != serviceCache) {
|
||||
return serviceCache;
|
||||
|
@ -118,26 +115,25 @@ public class ServiceInvoker {
|
|||
serviceCache = new Cache(bean);
|
||||
serviceCacheMap.put(serviceName, serviceCache);
|
||||
} catch (BeansException e) {
|
||||
throw new NoSuchServiceException();
|
||||
throw new NoSuchMethodException();
|
||||
}
|
||||
|
||||
return serviceCache;
|
||||
}
|
||||
|
||||
private Object getValue(Type parameterType, String json) throws InvalidParameterException {
|
||||
private Object getValue(Type parameterType, String json) throws IllegalArgumentException {
|
||||
JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType);
|
||||
if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) {
|
||||
try {
|
||||
return objectMapper.readValue(json, targetType);
|
||||
} catch (IOException e) {
|
||||
throw new InvalidParameterException();
|
||||
throw new IllegalArgumentException("");
|
||||
}
|
||||
}
|
||||
return objectMapper.convertValue(json, targetType);
|
||||
}
|
||||
|
||||
|
||||
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws InvalidParameterException {
|
||||
private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws IllegalArgumentException {
|
||||
if (null == parameterTypes || null == params) {
|
||||
return null;
|
||||
}
|
||||
|
@ -155,18 +151,20 @@ public class ServiceInvoker {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
public String invoke(String serviceName, String methodName, List<ByteString> params) throws OverflowException, OverflowRuntimeException {
|
||||
public String invoke(String serviceName, String methodName, List<ByteString> params)
|
||||
throws NoSuchMethodException, IllegalAccessException, OverflowException {
|
||||
Cache serviceCache = getServiceCache(serviceName);
|
||||
Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName);
|
||||
|
||||
if (null == methodCache.parameterTypes) {
|
||||
if (null != params && 0 < params.size()) {
|
||||
throw new InvalidParameterException();
|
||||
throw new IllegalArgumentException(
|
||||
String.format("size of parameters is not valid target[%d], source[%d]", 0, params.size()));
|
||||
}
|
||||
} else {
|
||||
if (methodCache.parameterTypes.length != params.size()) {
|
||||
throw new InvalidParameterException();
|
||||
throw new IllegalArgumentException(String.format("size of parameters is not valid target[%d], source[%d]",
|
||||
methodCache.parameterTypes.length, params.size()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,19 +178,19 @@ public class ServiceInvoker {
|
|||
try {
|
||||
result = methodCache.method.invoke(serviceCache.getBean(), parameters);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new InvalidRequestException();
|
||||
throw new IllegalAccessException(String.format("method[%s.%s] is not valid", serviceName, methodName));
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable t = e.getTargetException();
|
||||
if (t instanceof OverflowRuntimeException) {
|
||||
throw (OverflowRuntimeException)t;
|
||||
if (t instanceof OverflowException) {
|
||||
throw (OverflowException) t;
|
||||
}
|
||||
throw new InvalidRequestException();
|
||||
throw new OverflowException("internal error", e);
|
||||
}
|
||||
|
||||
try {
|
||||
jsonInString = objectMapper.writeValueAsString(result);
|
||||
} catch (IOException e) {
|
||||
throw new InvalidRequestException();
|
||||
throw new OverflowException("internal error", e);
|
||||
}
|
||||
|
||||
return jsonInString;
|
||||
|
|
|
@ -3,7 +3,11 @@ package com.loafle.overflow.central.proxy;
|
|||
import com.loafle.overflow.api.OverflowApiServerGrpc;
|
||||
import com.loafle.overflow.api.ServerInput;
|
||||
import com.loafle.overflow.api.ServerOutput;
|
||||
import com.loafle.overflow.core.exception.OverflowException;
|
||||
|
||||
import io.grpc.*;
|
||||
import io.grpc.Status.Code;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
|
@ -26,8 +30,7 @@ public class ServiceProxy {
|
|||
|
||||
server = ServerBuilder.forPort(port)
|
||||
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor))
|
||||
.build()
|
||||
.start();
|
||||
.build().start();
|
||||
logger.info("Server started, listening on " + port);
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
|
@ -61,44 +64,37 @@ public class ServiceProxy {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void exec(ServerInput request,
|
||||
io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
|
||||
public void exec(ServerInput request, io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
|
||||
|
||||
String result = null;
|
||||
try {
|
||||
result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList());
|
||||
|
||||
ServerOutput reply = ServerOutput.newBuilder()
|
||||
.setResult(result)
|
||||
.build();
|
||||
try {
|
||||
result = this.serviceInvoker.invoke(request.getTarget(), request.getMethod(),
|
||||
request.getParamsList().asByteStringList());
|
||||
|
||||
ServerOutput reply = ServerOutput.newBuilder().setResult(result).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
|
||||
} catch (OverflowException e) {
|
||||
logger.warning(getExceptionMessage(request, e));
|
||||
responseObserver.onError(convertException(e));
|
||||
} catch (OverflowRuntimeException e) {
|
||||
logger.warning(getExceptionMessage(request, e));
|
||||
responseObserver.onError(convertException(e));
|
||||
responseObserver.onError(this.getException(request, Status.Code.INTERNAL, e));
|
||||
return;
|
||||
} catch (NoSuchMethodException e) {
|
||||
responseObserver.onError(this.getException(request, Status.Code.NOT_FOUND, e));
|
||||
return;
|
||||
} catch (IllegalAccessException e) {
|
||||
responseObserver.onError(this.getException(request, Status.Code.INVALID_ARGUMENT, e));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private StatusException getException(ServerInput request, Code code, Throwable cause) {
|
||||
|
||||
private String getExceptionMessage(ServerInput request, Exception e) {
|
||||
return String.format("Target: %s, Method:%s, Params:%s, ex:%s", request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList(), e.toString());
|
||||
}
|
||||
|
||||
protected StatusRuntimeException convertException(OverflowRuntimeException e) {
|
||||
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
|
||||
Status status = Status.fromCode(Status.Code.INTERNAL).withDescription(message);
|
||||
return new StatusRuntimeException(status);
|
||||
}
|
||||
|
||||
protected StatusException convertException(OverflowException e) {
|
||||
String message = String.format("%s|%s", e.getClass().getSimpleName(), e.getMessage());
|
||||
Status status = Status.fromCode(e.getCode()).withDescription(message);
|
||||
String msg = String.format("Target: %s, Method:%s, Params:%s, message:%s", request.getTarget(),
|
||||
request.getMethod(), request.getParamsList().asByteStringList(), cause.getMessage());
|
||||
Status status = Status.fromCode(code).withDescription(msg).withCause(cause);
|
||||
return new StatusException(status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class InvalidParameterException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.INVALID_ARGUMENT;
|
||||
public InvalidParameterException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public InvalidParameterException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class InvalidRequestException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.UNKNOWN;
|
||||
public InvalidRequestException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public InvalidRequestException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class NoSuchMethodException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
|
||||
public NoSuchMethodException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public NoSuchMethodException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.loafle.overflow.central.proxy.exception;
|
||||
|
||||
import com.loafle.overflow.central.commons.exception.OverflowException;
|
||||
import io.grpc.Status;
|
||||
|
||||
public class NoSuchServiceException extends OverflowException {
|
||||
private static final Status.Code code = Status.Code.UNIMPLEMENTED;
|
||||
public NoSuchServiceException() {
|
||||
super(code);
|
||||
}
|
||||
|
||||
public NoSuchServiceException(String message) {
|
||||
super(code, message);
|
||||
}
|
||||
}
|
|
@ -41,40 +41,40 @@ public class RedisMessagePublisher implements MessagePublisher {
|
|||
this.topics = topics;
|
||||
}
|
||||
|
||||
public void publishToDomainMembers(final long domainID, final String method, final Object... params) {
|
||||
public void publishToDomainMembers(final long domainID, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||
message.setTargets(getMemberListByDomainID(domainID));
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) {
|
||||
public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||
message.setTargets(getMemberListByProbeKey(probeKey));
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
|
||||
public void publishToMember(final String memberID, final String method, final Object... params) {
|
||||
public void publishToMember(final String memberID, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER);
|
||||
message.addTarget(memberID);
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) {
|
||||
public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION);
|
||||
message.addTarget(memberSessionID);
|
||||
this.publish(CHANNEL_WEBAPP, message, method, params);
|
||||
}
|
||||
|
||||
public void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) {
|
||||
public void publishToNoAuthProbe(final String tempProbeKey, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.PROBE);
|
||||
message.addTarget(tempProbeKey);
|
||||
this.publish(CHANNEL_NOAUTH_PROBE, message, method, params);
|
||||
}
|
||||
|
||||
public void publishToProbe(final String probeKey, final String method, final Object... params) {
|
||||
public void publishToProbe(final String probeKey, final String method, final Object... params) throws OverflowException {
|
||||
PublishMessage message = new PublishMessage();
|
||||
message.setTargetType(PublishMessage.TargetType.PROBE);
|
||||
message.addTarget(probeKey);
|
||||
|
|
Loading…
Reference in New Issue
Block a user