- 最终版本,利用hessian实现rpc调用
- HessianUtil
package com.bill.rpc10;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @Auther: wangchunwen
* @Date: 2023/1/10 - 01 - 10 - 23:31
* @Description: com.bill.rpc10
* @version: 1.0
*/
public class HessianUtil {
public static byte[] serialize(Object o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Hessian2Output output = new Hessian2Output(baos);
output.writeObject(o);
byte[] bytes = baos.toByteArray();
baos.close();
output.close();
return bytes;
}
public static Object deseriabliz(byte[] bytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
Hessian2Input input = new Hessian2Input(bais);
Object o = input.readObject();
bais.close();
input.close();
return o;
}
}
package com.bill.rpc10;
import com.bill.rpc.common.Product;
/**
* @Auther: wangchunwen
* @Date: 2023/1/10 - 01 - 10 - 23:42
* @Description: com.bill.rpc10
* @version: 1.0
*/
public class ProductServiceImpl {
public Product findProduct(Integer id) {
return new Product(id,"321");
}
}
package com.bill.rpc10;
import com.bill.rpc.common.IProductService;
import com.bill.rpc07.Stub;
/**
* @Auther: wangchunwen
* @Date: 2023/1/10 - 01 - 10 - 23:41
* @Description: com.bill.rpc10
* @version: 1.0
*/
public class Client {
public static void main(String[] args) {
IProductService productService = (IProductService) Stub.getStub(IProductService.class);
System.out.println(productService.findProduct(12));
}
}
package com.bill.rpc10;
import com.bill.rpc07.ProductServiceImpl;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Auther: wangchunwen
* @Date: 2023/1/10 - 01 - 10 - 23:42
* @Description: com.bill.rpc10
* @version: 1.0
*/
public class Server {
private static boolean running = true;
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
ServerSocket ss = new ServerSocket(8888);
while (running){
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process (Socket s) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
ObjectInputStream ois = new ObjectInputStream(in);
String className = ois.readUTF();
String methodName = ois.readUTF();
Class[] parameterTypes = (Class[]) ois.readObject();
Object[] args = (Object[]) ois.readObject();
Class clazz = null;
// 从服务注册表找到具体的类
clazz = ProductServiceImpl.class;
Method method = clazz.getMethod(methodName,parameterTypes);
Object o = (Object) method.invoke(clazz.newInstance(),args);
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(o);
oos.flush();
}
}