- 只能支持Java语言,不能跨语言使用。
- 不好用,语法生硬
- 速度慢,序列化的字节数组长度比较长,所占空间大
1.FST同样是针对Java开发的序列化框架,因此也不能跨语言使用
1.也是针对Java开发的,不具有跨语言特性
反序列化性能比Kryo和FST差些
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();//开辟一块字节数组内存空间
DataOutputStream dos = new DataOutputStream(baos);//操作数据的流
dos.writeInt(123);//写死了
//写数据
s.getOutputStream().write(baos.toByteArray());//传输ID给服务器
s.getOutputStream().flush();
//读数据
DataInputStream dis = new DataInputStream(s.getInputStream());
int id = dis.readInt();
String name = dis.readUTF();
User user = new User(name,id);
System.out.println(user);
dos.close();
s.close();
}
}
--------------------------------------------------------------------------------
package com.demo.rpc01;
import com.demo.IUSerService;
import com.demo.User;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();//监听端口
process(s);//处理请求
s.close();//关闭连接
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
int id = dis.readInt();//读入ID
IUSerService serService = new USerServiceImpl();
User user = serService.getUSerById(id);
dos.writeInt(user.getId());
dos.writeUTF(user.getName());
dos.flush();
}
}
-----------------------------------------------------------------------------------
package com.demo.rpc01;
import com.demo.IUSerService;
import com.demo.User;
public class USerServiceImpl implements IUSerService {
@Override
public User getUSerById(Integer id) {
return new User("Alice",id);
}
}
package com.demo.rpc02;
public class Client {
public static void main(String[] args) throws Exception
{
Stub stub = new Stub();
System.out.println(stub.getUserById(123));
}
}
-----------------------------------------------------------------------------------
package com.demo.rpc02;
import com.demo.IUSerService;
import com.demo.User;
import com.demo.rpc01.USerServiceImpl;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
int id = dis.readInt();
IUSerService serService = new USerServiceImpl();
User user = serService.getUSerById(id);
dos.writeInt(user.getId());
dos.writeUTF(user.getName());
dos.flush();
}
}
-----------------------------------------------------------------------------------
package com.demo.rpc02;
import com.demo.IUSerService;
import com.demo.User;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class Stub {
public User getUserById(Integer id) throws Exception
{
Socket s = new Socket("127.0.0.1",8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(123);
s.getOutputStream().write(baos.toByteArray());
s.getOutputStream().flush();
DataInputStream dis = new DataInputStream(s.getInputStream());
int receivedId = dis.readInt();
String name = dis.readUTF();
User user = new User(name,id);
dos.close();
s.close();
return user;
}
}
package com.demo.rpc03;
import com.demo.IUSerService;
public class Client {
public static void main(String[] args) throws Exception
{
IUSerService stub = (IUSerService) Stub.getStub();
System.out.println(stub.getUSerById(123));
}
}
--------------------------------------------------------------------------------------------------
package com.demo.rpc03;
import com.demo.IUSerService;
import com.demo.User;
import com.demo.rpc01.USerServiceImpl;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
int id = dis.readInt();
IUSerService serService = new USerServiceImpl();
User user = serService.getUSerById(id);
dos.writeInt(user.getId());
dos.writeUTF(user.getName());
dos.flush();
}
}
------------------------------------------------------------------------------------------
package com.demo.rpc03;
import com.demo.IUSerService;
import com.demo.User;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;
public class Stub {
public static Object getStub(){
InvocationHandler h = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket s = new Socket("127.0.0.1",8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(123);
s.getOutputStream().write(baos.toByteArray());
s.getOutputStream().flush();
DataInputStream dis = new DataInputStream(s.getInputStream());
int id = dis.readInt();
String name = dis.readUTF();
User user = new User(name,id);
dos.close();
s.close();
return user;
}
};
Object o = Proxy.newProxyInstance(IUSerService.class.getClassLoader(),new Class[]{IUSerService.class},h);//生成代理类
return (IUSerService)o;
}
}
package com.demo.rpc04;
import com.demo.IUSerService;
public class Client {
public static void main(String[] args) throws Exception
{
Object stub = Stub.getStub();
System.out.println(((IUSerService)stub).getUSerById(123));
}
}
--------------------------------------------------------------------------------------
package com.demo.rpc04;
import com.demo.IUSerService;
import com.demo.User;
import com.demo.rpc01.USerServiceImpl;
import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
ObjectInputStream oos = new ObjectInputStream(in);
String methodName = oos.readUTF();//读入方法名
Class[] parameterTypes = (Class[]) oos.readObject();//读入方法参数类型
Object[] args = (Object[]) oos.readObject();//读入方法参数
IUSerService serService = new USerServiceImpl();
Method method = serService.getClass().getMethod(methodName,parameterTypes);//调用方法
User user = (User)method.invoke(serService,args);
dos.writeInt(user.getId());
dos.writeUTF(user.getName());
dos.flush();
}
}
-------------------------------------------------------------------------------------------
package com.demo.rpc04;
import com.demo.IUSerService;
import com.demo.User;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;
public class Stub {
public static Object getStub(){
InvocationHandler h = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket s = new Socket("127.0.0.1",8888);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
String methodName = method.getName();
Class[] parameterTypes = method.getParameterTypes();//获取参数类型数组,这是防止重载带来的错误
oos.writeUTF(methodName);
oos.writeObject(parameterTypes);
oos.writeObject(args);//方法参数
oos.flush();
DataInputStream dis = new DataInputStream(s.getInputStream());
int id = dis.readInt();
String name = dis.readUTF();
User user = new User(name,id);
oos.close();
s.close();
return user;
}
};
Object o = Proxy.newProxyInstance(IUSerService.class.getClassLoader(),new Class[]{IUSerService.class},h);//生成代理类
return o;
}
}
package com.demo.rpc05;
import com.demo.IUSerService;
public class Client {
public static void main(String[] args) throws Exception
{
IUSerService stub = Stub.getStub();
System.out.println(stub.getUSerById(123));
}
}
---------------------------------------------------------------------------------------
package com.demo.rpc05;
import com.demo.IUSerService;
import com.demo.User;
import com.demo.rpc01.USerServiceImpl;
import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
ObjectInputStream ois = new ObjectInputStream(in);
ObjectOutputStream oos = new ObjectOutputStream(out);
String methodName = ois.readUTF();//读入方法名
Class[] parameterTypes = (Class[]) ois.readObject();//读入方法参数类型
Object[] args = (Object[]) ois.readObject();//读入方法参数
IUSerService serService = new USerServiceImpl();
Method method = serService.getClass().getMethod(methodName,parameterTypes);//调用方法
User user = (User)method.invoke(serService,args);
oos.writeObject(user);
dos.flush();
}
}
-----------------------------------------------------------------------------------------------------
package com.demo.rpc05;
import com.demo.IUSerService;
import com.demo.User;
import java.io.DataInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;
public class Stub {
public static IUSerService getStub(){
InvocationHandler h = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket s = new Socket("127.0.0.1",8888);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
String methodName = method.getName();
Class[] parameterTypes = method.getParameterTypes();//获取参数类型数组,这是防止重载带来的错误
oos.writeUTF(methodName);
oos.writeObject(parameterTypes);
oos.writeObject(args);//方法参数
oos.flush();
User user = (User)ois.readObject();
oos.close();
ois.close();
s.close();
return user;
}
};
Object o = Proxy.newProxyInstance(IUSerService.class.getClassLoader(),new Class[]{IUSerService.class},h);//生成代理类
return (IUSerService) o;
}
}
package com.demo.rpc06;
import com.demo.IUSerService;
public class Client {
public static void main(String[] args) throws Exception
{
IUSerService serService = (IUSerService) Stub.getStub(IUSerService.class);
System.out.println(serService.getUSerById(123));
}
}
---------------------------------------------------------------------------------------------------
package com.demo.rpc06;
import com.demo.IUSerService;
import com.demo.User;
import com.demo.rpc01.USerServiceImpl;
import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
ObjectInputStream ois = new ObjectInputStream(in);
ObjectOutputStream oos = new ObjectOutputStream(out);
String className = ois.readUTF();//读入类名
String methodName = ois.readUTF();//读入方法名
Class[] parameterTypes = (Class[]) ois.readObject();//读入方法参数类型
Object[] args = (Object[]) ois.readObject();//读入方法参数
Class clazz = null;
//从服务注册表找到具体类
clazz = USerServiceImpl.class;
Method method = clazz.getMethod(methodName,parameterTypes);//调用方法
User user = (User)method.invoke(clazz.getDeclaredConstructor().newInstance(),args);
oos.writeObject(user);
dos.flush();
}
}
----------------------------------------------------------------------------------------------------
package com.demo.rpc06;
import com.demo.HessionUtils.HessianUtils;
import com.demo.IUSerService;
import com.demo.User;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;
public class Stub {
public static Object getStub(Class clazz){
InvocationHandler h = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket s = new Socket("127.0.0.1",8888);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
String clazzName = clazz.getName();
String methodName = method.getName();
Class[] parameterTypes = method.getParameterTypes();//获取参数类型数组,这是防止重载带来的错误
oos.writeUTF(clazzName);
oos.writeUTF(methodName);
oos.writeObject(parameterTypes);
oos.writeObject(args);//方法参数
oos.flush();
Object o = HessianUtils.deserialize(ois.readAllBytes());
oos.close();
ois.close();
s.close();
return o;
}
};
Object o = Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},h);//生成代理类
return o;
}
}
package com.demo.rpc07;
import com.demo.IProductService;
import com.demo.IUSerService;
import com.demo.rpc06.Stub;
public class Client {
public static void main(String[] args) throws Exception
{
IProductService serService = (IProductService) Stub.getStub(IProductService.class);
System.out.println(serService.getProById(123));
}
}
-------------------------------------------------------------------------------------
package com.demo.rpc07;
import com.demo.Product;
import com.demo.User;
import com.demo.rpc01.USerServiceImpl;
import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static boolean running = true;
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(running)
{
Socket s = ss.accept();
process(s);
s.close();
}
ss.close();
}
private static void process(Socket s) throws Exception
{
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
ObjectInputStream ois = new ObjectInputStream(in);
ObjectOutputStream oos = new ObjectOutputStream(out);
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);//调用方法
Product product = (Product)method.invoke(clazz.getDeclaredConstructor().newInstance(),args);
oos.writeObject(product);
dos.flush();
}
}
---------------------------------------------------------------------------------------------------------
package com.demo.rpc07;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;
public class Stub {
public static Object getStub(Class clazz){
InvocationHandler h = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket s = new Socket("127.0.0.1",8888);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
String clazzName = clazz.getName();
String methodName = method.getName();
Class[] parameterTypes = method.getParameterTypes();//获取参数类型数组,这是防止重载带来的错误
oos.writeUTF(clazzName);
oos.writeUTF(methodName);
oos.writeObject(parameterTypes);
oos.writeObject(args);//方法参数
oos.flush();
Object o = ois.readObject();
oos.close();
ois.close();
s.close();
return o;
}
};
Object o = Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},h);//生成代理类
return o;
}
}
package com.demo.rpc08_Hessian01;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.demo.User;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
public class HelloHessian {
public static void main(String[] args) throws Exception
{
User user = new User("zhangsan",1);
System.out.println("hession:"+serialize(user).length);
}
public static byte[] serialize(Object o) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Hessian2Output output = new Hessian2Output(baos);
output.writeObject(o);
output.flush();
byte[] bytes = baos.toByteArray();
baos.close();
output.close();
return bytes;
}
public static Object deserialize(byte[] bytes) throws Exception
{
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
Hessian2Input input = new Hessian2Input(bais);
Object o = input.readObject();
bais.close();
input.close();
return o;
}
}
package com.demo.rpc09_Hessian02;
import com.caucho.hessian.io.Hessian2Output;
import com.demo.User;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;
public class HessianVsJdk {
public static void main(String[] args) throws Exception
{
User user = new User("zhangsan",1);
System.out.println("Hessian:"+hessianSerial(user).length);
System.out.println("JDK:"+jdkSerial(user).length);
}
public static byte[] hessianSerial(Object o) throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
Hessian2Output output = new Hessian2Output(out);
output.writeObject(o);
output.flush();
byte[] bytes = out.toByteArray();
out.close();
output.close();
return bytes;
}
public static byte[] jdkSerial(Object o) throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(o);
oos.flush();
byte[] bytes = out.toByteArray();
out.close();
oos.close();
return bytes;
}
}