什么是RPC?
优点:将功能解耦,性能稍高,可扩展性和可维护性,高可用(负载均衡,自动重试)
缺点:复杂,运维难度大,跨平台/语言调用麻烦
代码目录结构:
GreetingService接口
public interface GreeatingService {
public String sayHi(String s);
}
GreetingService接口实现类
public class GreetingServiceImpl implements GreeatingService {
@Override
public String sayHi(String s) {
return s;
}
}
客户端
public class Application {
public static void main(String[] args) throws IOException {
GreeatingService service = new MyDubboClient<>(GreeatingService.class).getRef();
System.out.println(service.sayHi("gebilaowang"));
}
}
服务端
public class Application {
public static void main(String[] args) throws IOException {
new MyDubboProvider<>(new GreetingServiceImpl()).start();
System.in.read();
}
}
客户端具体实现类
public class MyDubboClient<T> {
private Class<T> interfaceClass;
private Socket socket;
public MyDubboClient(Class<T> interfaceClass) throws IOException {
this.interfaceClass = interfaceClass;
this.socket = new Socket();
this.socket.connect(new InetSocketAddress("127.0.0.1", 8888));
}
public T getRef() {
return (T) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class[]{interfaceClass},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodInfo info = new MethodInfo(method.getName(),
Stream.of(args).collect(Collectors.toList()));
MyDubboClient.this.socket.getOutputStream().write((JSON.toJSON(info) + "\n").getBytes());
MyDubboClient.this.socket.getOutputStream().flush();
System.out.println("sent!");
String returnValueJSON = new BufferedReader(new InputStreamReader(MyDubboClient.this.socket.getInputStream())).readLine();
System.out.println("Get: " + returnValueJSON);
return JSON.parse(returnValueJSON);
}
});
}
}
服务端具体实现类:
package lib;
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
public class MyDubboProvider<T> {
private T serviceImpl;
private ServerSocket serverSocket;
public MyDubboProvider(T serviceImpl) throws IOException {
this.serviceImpl = serviceImpl;
this.serverSocket = new ServerSocket(8888);
}
public void start() throws IOException {
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Accept!");
new WorkerThread(socket).start();
}
}
private class WorkerThread extends Thread {
private Socket socket;
public WorkerThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
String line = new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine();
System.out.println(line);
MethodInfo methodInfo = JSON.parseObject(line, MethodInfo.class);
Method method = serviceImpl.getClass().getMethod(methodInfo.getMethodName(), methodInfo.getParams().stream().map(Object::getClass).toArray(Class[]::new));
Object returnValue = method.invoke(serviceImpl, methodInfo.params.toArray());
socket.getOutputStream().write((JSON.toJSONString(returnValue) + "\n").getBytes());
socket.getOutputStream().flush();
} catch (IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}