zoukankan      html  css  js  c++  java
  • 基于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;  
        }  
          
    }  
  • 相关阅读:
    Hadoop 的版本问题
    SSH 端口转发原理
    KM算法
    最大流算法小结
    pku 2195 KM算法求最小权二分匹配
    SAP(最短增广路算法) 最大流模板
    最大流模板
    pku 1459 最大流 SAP
    pku Drainage Ditches 简单最大流 直接套模板 注意可能有重边
    推荐:吴军 谷歌黑板报 《浪潮之颠》
  • 原文地址:https://www.cnblogs.com/duanxz/p/4494395.html
Copyright © 2011-2022 走看看