-
Stub已经实现:可以任意新增接口和方法都可以通过这个stub代理出一个对应的对象。新增一个IProductService接口和方法;如下:
-
IProductService接口
package com.bill.rpc.common;
/**
* @Auther: wangchunwen
* @Date: 2023/1/7 - 01 - 07 - 21:32
* @Description: com.bill.rpc.common
* @version: 1.0
*/
public interface IProductService {
public Product findProduct(Integer id);
}
- Product:注意要实现Serializable接口才能进行传输
package com.bill.rpc.common;
import java.io.Serializable;
/**
* @Auther: wangchunwen
* @Date: 2023/1/7 - 01 - 07 - 21:34
* @Description: com.bill.rpc.common
* @version: 1.0
*/
public class Product implements Serializable {
private Integer id;
private String name;
public Product(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "{id="+id+",name="+name+"}";
}
}
- ProductServiceImpl实现类
package com.bill.rpc07;
import com.bill.rpc.common.IProductService;
import com.bill.rpc.common.Product;
/**
* @Auther: wangchunwen
* @Date: 2023/1/7 - 01 - 07 - 21:24
* @Description: com.bill.rpc07
* @version: 1.0
*/
public class ProductServiceImpl implements IProductService {
public Product findProduct(Integer id) {
return new Product(id,"321");
}
}
- client
package com.bill.rpc07;
import com.bill.rpc.common.IProductService;
/**
* @Auther: wangchunwen
* @Date: 2023/1/7 - 01 - 07 - 21:24
* @Description: com.bill.rpc07
* @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));
}
}
- server,注意生成具体的类,从服务注册表找到具体的类
package com.bill.rpc07;
import com.bill.rpc01.UserServiceImpl;
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/7 - 01 - 07 - 21:24
* @Description: com.bill.rpc07
* @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();
}
}
- 调用结果