公共代码
项目:rpc-common
商品类:
public class Product implements Serializable {
private Integer id;
private String name;
private Integer price;
}
店铺类:
public class Store implements Serializable {
private Integer id;
private String name;
}
请求:
public class Request implements Serializable {
// 类名
private String classType;
// 方法名
private String methodName;
// 参数类型
private Class<?>[] parameterTypes;
// 参数值
private Object[] args;
}
响应:
public class Response implements Serializable {
private Object data;
}
商品服务接口:
public interface IProductService {
Product findById(int id);
}
店铺服务接口:
public interface IStoreService {
Store findById(int id);
}
提供者
项目:rpc-provider
商品接口实现类:
public class ProductServiceImpl implements IProductService {
public Product findById(int id) {
return testData.get(id);
}
/**
* 测试数据
*/
private static Map<Integer, Product> testData = new HashMap();
static {
testData.put(1, new Product(1, "手机", 1000));
testData.put(2, new Product(2, "电脑", 2000));
}
}
店铺接口实现类:
public class StoreServiceImpl implements IStoreService {
public Store findById(int id) {
return testData.get(id);
}
/**
* 测试数据
*/
private static Map<Integer, Store> testData = new HashMap();
static {
testData.put(1, new Store(1, "小周的店铺"));
testData.put(2, new Store(2, "小王的店铺"));
}
}
注册bean
:
public class BeanFactory {
private static Map<String, Object> container = new HashMap();
static {
container.put("com.mncode.IProductService", new ProductServiceImpl());
container.put("com.mncode.IStoreService", new StoreServiceImpl());
}
public static Object getBean(String classType) {
Object service = container.get(classType);
return service;
}
}
处理请求-响应:
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8080);
while (true) {
Socket socket = ss.accept();
process(socket);
socket.close();
}
}
public static void process(Socket socket) throws Exception {
// 处理请求
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Request request = (Request) in.readObject();
String classType = request.getClassType();
String methodName = request.getMethodName();
Class[] parameterTypes = request.getParameterTypes();
Object[] args = request.getArgs();
// 业务处理
Object bean = BeanFactory.getBean(classType);
Method method = bean.getClass().getMethod(methodName, parameterTypes);
Object data = method.invoke(bean, args);
// 处理响应
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(new Response(data));
out.flush();
}
}
消费者
项目:rpc-consumer
Bean工厂:
public class BeanFactory {
public static <T> T getBean(final Class classType) {
InvocationHandler h = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket socket = new Socket("127.0.0.1", 8080);
// 发送请求
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
Request request = new Request();
request.setClassType(classType.getName());
request.setMethodName(method.getName());
request.setParameterTypes(method.getParameterTypes());
request.setArgs(args);
out.writeObject(request);
out.flush();
// 接受响应
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Response o = (Response) in.readObject();
return o.getData();
}
};
Object service = Proxy.newProxyInstance(classType.getClassLoader(), new Class[]{classType}, h);
return (T) service;
}
}
测试(先启动提供者,再启动消费者):
public class Client {
public static void main(String[] args) {
IProductService productService = BeanFactory.getBean(IProductService.class);
System.out.printf("商品1:%s\n", productService.findById(1));
System.out.printf("商品2:%s\n", productService.findById(2));
System.out.printf("商品3:%s\n", productService.findById(3));
IStoreService storeService = BeanFactory.getBean(IStoreService.class);
System.out.printf("店铺1:%s\n", storeService.findById(1));
System.out.printf("店铺2:%s\n", storeService.findById(2));
System.out.printf("店铺3:%s\n", storeService.findById(3));
}
}
输出结果:
商品1:Product{id=1, name='手机', price=1000}
商品2:Product{id=2, name='电脑', price=2000}
商品3:null
店铺1:Store{id=1, name='小周的店铺'}
店铺2:Store{id=2, name='小王的店铺'}
店铺3:null