![JAR search and dependency download from the Maven repository](/logo.png)
com.msa.rpc.client.support.DefaultInvocationProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of msa-rpc-client Show documentation
Show all versions of msa-rpc-client Show documentation
Lightweight RPC communication framework based on netty and protostuff.
The newest version!
package com.msa.rpc.client.support;
import com.google.common.net.HostAndPort;
import com.google.common.reflect.Reflection;
import com.msa.rpc.client.RpcClient;
import com.msa.rpc.common.bean.RpcRequest;
import com.msa.rpc.common.bean.RpcResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.UUID;
/**
* The type Default invocation proxy.
*/
@Slf4j
public class DefaultInvocationProxy implements InvocationProxy {
/**
* The Client.
*/
private RpcClient client;
/**
* Instantiates a new Default invocation proxy.
*
* @param client the client
*/
public DefaultInvocationProxy(RpcClient client) {
this.client = client;
}
/**
* New instance t.
*
* @param the type parameter
* @param target the target
* @return the t
*/
public T newInstance(final Class target) {
return Reflection.newProxy(target, new RpcInvocationHandler(target, client));
}
/**
* New instance t.
*
* @param the type parameter
* @param type the type
* @param interfaceClazz the interface clazz
* @return the t
*/
public T newInstance(final Class type, final Class interfaceClazz) {
return Reflection.newProxy(type, new RpcInvocationHandler(interfaceClazz, client));
}
/**
* The type Rpc invocation handler.
*
* @param the type parameter
*/
@Slf4j
public static class RpcInvocationHandler implements InvocationHandler {
/**
* The Interface clazz.
*/
private Class interfaceClazz;
/**
* The Client.
*/
private RpcClient client;
/**
* Instantiates a new Rpc invocation handler.
*
* @param interfaceClazz the interface clazz
* @param client the client
*/
public RpcInvocationHandler(Class interfaceClazz, RpcClient client) {
this.interfaceClazz = interfaceClazz;
this.client = client;
}
/**
* Invoke object.
*
* @param proxy the proxy
* @param method the method
* @param args the args
* @return the object
* @throws Throwable the throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String serviceName = interfaceClazz.getName();
String interfaceName = serviceName;
RpcRequest request = RpcRequest.builder()
.requestId(UUID.randomUUID().toString())
.interfaceName(interfaceName)
.methodName(method.getName())
.parameterType(method.getParameterTypes())
.parameters(args)
.build();
String hostAndPort = client.loadBalance(serviceName);
log.debug(">>>>>>>>===discover service: {} ===> {}", serviceName, hostAndPort);
if (StringUtils.isEmpty(hostAndPort)) {
throw new RuntimeException(">>>>>>>>===server address is empty");
}
HostAndPort var0 = HostAndPort.fromString(hostAndPort);
String host = var0.getHost();
int port = var0.getPort();
RpcResponse response = client.sendRequest(host, port, request);
if (Objects.isNull(response)) {
log.error(">>>>>>>>===send request failure", new IllegalStateException("response is null"));
return null;
}
if (response.hasException()) {
log.error(">>>>>>>>===response has exception", response.getException());
return null;
}
// 返回结果
return response.getResult();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy