新增依赖和远程调用工具类
This commit is contained in:
parent
9a2cb4bad3
commit
8f23bca7e2
@ -152,6 +152,13 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- wgink end -->
|
<!-- wgink end -->
|
||||||
|
|
||||||
|
<!-- cglib start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cglib</groupId>
|
||||||
|
<artifactId>cglib</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- cglib end -->
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,45 @@
|
|||||||
|
package ink.wgink.util.remote.rest;
|
||||||
|
|
||||||
|
import net.sf.cglib.proxy.Enhancer;
|
||||||
|
import net.sf.cglib.proxy.MethodInterceptor;
|
||||||
|
import net.sf.cglib.proxy.MethodProxy;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName: RestRemoteRequest
|
||||||
|
* @Description: rest远程请求
|
||||||
|
* @Author: wanggeng
|
||||||
|
* @Date: 2021/9/18 4:23 下午
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class RestRemoteRequestDemo implements MethodInterceptor {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
|
||||||
|
return methodProxy.invokeSuper(o, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RestRemoteRequestFactory {
|
||||||
|
public static Object getProxy(Class<?> clazz) {
|
||||||
|
Enhancer enhancer = new Enhancer();
|
||||||
|
enhancer.setClassLoader(clazz.getClassLoader());
|
||||||
|
enhancer.setSuperclass(clazz);
|
||||||
|
enhancer.setCallback(new RestRemoteRequestDemo());
|
||||||
|
return enhancer.create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Demo {
|
||||||
|
void say(String msg) {
|
||||||
|
System.out.println(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Demo demo = (Demo) RestRemoteRequestFactory.getProxy(Demo.class);
|
||||||
|
demo.say("你好");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package ink.wgink.util.remote.rest.handler;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName: RestRemoterHandler
|
||||||
|
* @Description: Rest远程调用
|
||||||
|
* @Author: wanggeng
|
||||||
|
* @Date: 2021/9/18 5:14 下午
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class RestRemoteHandler implements InvocationHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||||
|
System.out.println(method.getDeclaringClass().getClassLoader());
|
||||||
|
// 否则执行统一接口处理方法
|
||||||
|
return method.invoke(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object run() {
|
||||||
|
return "run";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package ink.wgink.util.remote.rest.proxy;
|
||||||
|
|
||||||
|
import ink.wgink.util.remote.rest.handler.RestRemoteHandler;
|
||||||
|
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName: RestRemoteProxy
|
||||||
|
* @Description: Rest远程调用代理
|
||||||
|
* @Author: wanggeng
|
||||||
|
* @Date: 2021/9/18 5:40 下午
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class RestRemoteProxy {
|
||||||
|
|
||||||
|
public static <T> T getInstance(Class<T> cls) {
|
||||||
|
RestRemoteHandler restRemoteHandler = new RestRemoteHandler();
|
||||||
|
Object o = Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, restRemoteHandler);
|
||||||
|
return (T) o;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IDemo {
|
||||||
|
String say();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DemoImpl implements IDemo {
|
||||||
|
@Override
|
||||||
|
public String say() {
|
||||||
|
return "go";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IDemo demo = RestRemoteProxy.getInstance(IDemo.class);
|
||||||
|
System.out.println(demo.say());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user