基于jdk序列化和tcp的rpc调用
基于jdk序列化和tcp的rpc调用
RPC调用图
1、2和服务注册未实现
文件说明
AbstractService 抽象服务类
RpcRequest 服务请求对象
RpcResponse 服务返回结果对象
RpcClient rpc客户端
RpcServer rpc服务端
UserService 用户服务
User 用户对象
UserParam 用户参数对象
代码展示
AbstractService
public abstract class AbstractService<T extends RpcRequest, F extends RpcResponse> implements Serializable {
public abstract F execute(T request);
}
RpcRequest
public class RpcRequest<T extends Serializable> implements Serializable{
private T params;
public T getParams() {
return params;
}
public void setParams(T params) {
this.params = params;
}
}
RpcResponse
public class RpcResponse<T extends Serializable> implements Serializable {
private int code = 200;
private String message = "success";
private T result;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
}
UserService
public class UserService extends AbstractService<RpcRequest<UserParam>,RpcResponse<User>> {
@Override
public RpcResponse<User> execute(RpcRequest<UserParam> request) {
UserParam params = request.getParams();
String name = params.getName();
RpcResponse<User> response = new RpcResponse<>();
User user = new User();
if ("wanghao".equals(name)) {
user.setName("wanghao");
user.setAge(29);
response.setResult(user);
} else {
response.setCode(-200);
response.setMessage("no the user");
}
return response;
}
}
User
public class User implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
UserParam
public class UserParam implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
RpcClient
public class RpcClient {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Socket socket = new Socket("10.2.6.20", 8888);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(outputStream);
RpcRequest<UserParam> request = new RpcRequest<>();
UserParam param = new UserParam();
param.setName("sakyawang");
request.setParams(param);
stream.writeObject(request);
stream.flush();
InputStream inputStream = socket.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
RpcResponse<User> response = (RpcResponse<User>) objectInputStream.readObject();
System.out.println(response.getMessage());
System.out.println(response.getResult().getAge());
}
}
RpcServer
public class RpcServer {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(8888));
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
ObjectInputStream stream = new ObjectInputStream(inputStream);
RpcRequest<UserParam> request = (RpcRequest<UserParam>) stream.readObject();
UserService userService = new UserService();
RpcResponse<User> response = userService.execute(request);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(response);
objectOutputStream.flush();
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 编程私厨!