1.RPC
远程过程调用
2.demo
服务接口类:SayHelloService.java
package com.caicongyang.server;
/**
*
* <p>
* Title: SayHelloService.java
* Package com.caicongyang.server
* </p>
* <p>
* Description: 服务接口
* <p>
* @author Tom.Cai
* @created 2016-2-23 下午10:38:02
* @version V1.0
*
*/
public interface SayHelloService {
public String sayHello(String str);
}
服务实现类:SayHelloServiceImpl.java
package com.caicongyang.server;
/**
*
* <p>
* Title: SayHelloServiceImpl.java
* Package com.caicongyang.server
* </p>
* <p>
* Description: 服务实现类
* <p>
* @author Tom.Cai
* @created 2016-2-23 下午10:37:41
* @version V1.0
*
*/
public class SayHelloServiceImpl implements SayHelloService{
@Override
public String sayHello(String str) {
if("hello".equals(str)){
return "hello world!";
}else{
return "error!";
}
}
}
服务提供者类:Provider.java
package com.caicongyang.provider;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import com.caicongyang.server.SayHelloService;
import com.caicongyang.server.SayHelloServiceImpl;
/**
*
* <p>
* Title: Provider.java
* Package com.caicongyang.provider
* </p>
* <p>
* Description: RPC服务提供者,供客户端调用
* <p>
* @author Tom.Cai
* @created 2016-2-23 下午10:31:40
* @version V1.0
*
*/
public class Provider {
public static void main(String[] args) {
ServerSocket server = null;
ObjectOutputStream out = null;
try {
server = new ServerSocket(8080);
Socket socket =null;
while(true){
System.out.println("---开始监听---");
socket = server.accept();
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
String interfaceName = input.readUTF(); //接口名称
String methodName = input.readUTF(); //方法名称
Class<?>[] parameterType = (Class<?>[]) input.readObject(); //方法类型
Object[] arguments = (Object[]) input.readObject(); //参数列表
System.out.println("接收到的参数:"+Arrays.toString(arguments));
//根据接口名称获取class
Class<?> serviceInterfaceClass = Class.forName(interfaceName);
//根据方法名称和参数类型反射得到方法
Method method = serviceInterfaceClass.getMethod(methodName, parameterType);
//服务实例化(这里做简单处理,正常应该根据得到的接口名称serviceInterfaceClass获取对应的service,但本demo只提供一个服务)
SayHelloService service = new SayHelloServiceImpl();
//反射执行这个方法
Object result = method.invoke(service, arguments);
//写会处理结果
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(result);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
if(server!=null){
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
server = null;
}
}
}
}
客户端调用类:Consumer.java
package com.caicongyang.consumer;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.Socket;
import com.caicongyang.server.SayHelloService;
/**
*
* <p>
* Title: Consumer.java
* Package com.caicongyang.consumer
* </p>
* <p>
* Description: rpc服务消费者(即客户端)
* <p>
* @author Tom.Cai
* @created 2016-2-23 下午10:36:30
* @version V1.0
*
*/
public class Consumer {
public static void main(String[] args) {
try {
//接口名称
String interfaceName = SayHelloService.class.getName();
//接口方法
Method method = SayHelloService.class.getMethod("sayHello", java.lang.String.class);
//参数
Object[] arguments = {"hello"};
Socket socket = new Socket("127.0.0.1",8080);
//发送请求
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeUTF(interfaceName);
out.writeUTF(method.getName());
out.writeObject(method.getParameterTypes());
out.writeObject(arguments);
//获取结果
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
Object returnObject = inputStream.readObject();
System.out.println(returnObject.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.测试结果
结论:请求成功,完成了一个最简单的RPC远程过程调用demo!是不是跟常用的CXF,JWS等框架对应得起来
更多精彩内容请继续关注我的博客:http://blog.csdn.net/caicongyang
记录与分享,你我共成长 -from
caicongyang