基于kryonet的RPC,使用kryo进行序列化

Kryo是一个序列化框架。

Kryonet是一个基于kryo的RPC框架,它实现了一套高效简洁的API,它通过NIO实现了TCP和UDP通讯,目前还不支持Http。

自己写了一个测试代码,运行了下,感觉还不错,记录下来。

1、listener

package com.mytestcodes.kryonet;  
  
import com.esotericsoftware.kryonet.Connection;  
import com.esotericsoftware.kryonet.Listener;  
import com.mytestcodes.serialization.fulltest.Child;  
  
public class KListener extends Listener  
{  
    public KListener()  
    {  
  
    }  
  
    public KListener(String name)  
    {  
        this.name = name;  
    }  
  
    private String name;  
  
    public void received(Connection connection, Object object)  
    {  
        System.out.println(name + " has recive a message");  
  
        if (object instanceof Child)  
        {  
            Child child = (Child) object;  
            System.out.println(child.getChildName());  
            child.setChildName(name + " response");  
            connection.sendTCP(child);  
        }  
    }  
  
    public String getName()  
    {  
        return name;  
    }  
  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
  
}  

2、client

package com.mytestcodes.kryonet;  
  
  
import java.io.IOException;  
import java.nio.ByteBuffer;  
  
  
import com.esotericsoftware.kryonet.Client;  
import com.mytestcodes.serialization.fulltest.Baby;  
import com.mytestcodes.serialization.fulltest.Child;  
import com.mytestcodes.serialization.fulltest.Parent;  
  
  
public class KClient  
{  
    public static void main(String[] args)  
    {  
        Client client = new Client();  
        client.addListener(new KListener("Client"));  
        client.start();  
        try  
        {  
            client.connect(5000, "localhost", 54555, 54777);  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
  
  
        client.getKryo().setRegistrationOptional(true);  
        Child child = new Child();  
  
  
        client.sendTCP(child);  
    }  
  
  
    public static byte[] readBuf(ByteBuffer buf)  
    {  
        int size = buf.position();  
        byte[] newBuf = new byte[size];  
        for (int i = 0; i < size; i++)  
        {  
            newBuf[i] = buf.get(i);  
        }  
        return newBuf;  
    }  
} 

3、server

package com.mytestcodes.kryonet;  
  
  
import java.io.IOException;  
  
  
import com.esotericsoftware.kryonet.Server;  
  
  
public class KServer  
{  
    public static void main(String[] args)  
    {  
        Server server = new Server();  
        server.addListener(new KListener("server"));  
  
  
        server.getKryo().setRegistrationOptional(true);  
        server.start();  
        try  
        {  
            // TCP port 54555 and UDP port 54777  
            server.bind(54555, 54777);  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
    }  
}  

 

4、child:

package com.mytestcodes.serialization.fulltest;  
  
import java.io.Serializable;  
  
public class Baby implements Serializable  
{  
  
    /**  
     *   
     */  
    private static final long serialVersionUID = 8882758100866916676L;  
  
    private String babyName = "baby";  
  
    public String getBabyName()  
    {  
        return babyName;  
    }  
  
    public void setBabyName(String babyName)  
    {  
        this.babyName = babyName;  
    }  
  
}
package com.mytestcodes.serialization.fulltest;  
  
import java.io.Serializable;  
  
public class Parent implements Serializable  
{  
  
    /**  
     *   
     */  
    private static final long serialVersionUID = 6933088125784071832L;  
      
    private String parentName="parent";  
  
    public String getParentName()  
    {  
        return parentName;  
    }  
  
    public void setParentName(String parentName)  
    {  
        this.parentName = parentName;  
    }  
      
}  

 

 

posted on 2015-05-11 13:55  duanxz  阅读(2012)  评论(0编辑  收藏  举报