分享自己做的一个JAVA RPC 框架 longio

longio

longio 是一个支持同步和异步调用,并且支持客户端负载均衡,服务发现的RPC框架,底层基于netty,传输层支持websocket,http和原始socket,序列化层支持JSON,MessagePack,protobuf,支持服务端推送消息和客户端对推送消息的路由功能。

longio-spring

longio-spring 将longio集成到spring-boot可以基于注解对rpc框架进行配置更方便,更easy。

RPC配置

    @Bean(name="cmdLookup")
    CmdLookup getCmdLookup() {
        return new DefaultCmdLookup(); 
    }

    @Bean(name="appLookup")
    AppLookup getAppLookup(@Qualifier("environment") Environment env) {
        return new DefaultAppLookup(new EnvProperties(env)); 
    }

    @Bean
    BeanFactoryPostProcessor getLioBeanBeanFactoryPostProcessor(
            @Qualifier("appLookup") AppLookup appLookup, 
            @Qualifier("cmdLookup")CmdLookup cmdLookup) {
        return new LongioBeanFactoryPostProcessor(appLookup, cmdLookup);
    }

    @Boot(port = 5000, pt = ProtocolType.JSONARRAY, tt = TransportType.HTTP)
    @Boot(port = 5002, pt = ProtocolType.JSON, tt = TransportType.HTTP)
    @Boot(port = 5001, pt = ProtocolType.MESSAGE_PACK, tt = TransportType.SOCKET)
    @Bean(name = "longio.bootstrap")
    public LioBootstrap getLioBootstrap() {
        return new LioBootstrap();
    }

Server端

@Lservice(path = "com.lehuihome")
public class TestService {
    @Lio(cmd = "getUser")
    @Unpack("com.zhucode.longio.example.message.UserMsg")
    public Map<String, Map<String, UserMsg>>getUser(@Key("user_id")long userId) {
        System.out.println("++++++++++++++++++++++++++++++++++++++");
        JSONObject ret = new JSONObject();
        ret.put("status", "success");
        Map<String, UserMsg> rm = new HashMap<>();
        UserMsg um = new UserMsg();
        um.user_id = 9999;
        rm.put("1234", um);

        Map<String, Map<String, UserMsg>> m = new HashMap<>();
        m.put("1234", rm);
        return m;
        //return new String[]{"status", "true", "dddd"};
    }

    @Lio(cmd = "getUser1")
    @Unpack("com.zhucode.longio.example.message.User$Data")
    public Res.Data getUser1(@Key("user_id")long userId) {
        System.out.println("++++++++++++++++++++++++++++++++++++++");
        return Res.Data.newBuilder().setStatus("success").build();
    }

    @Lio(cmd = "getVoid")
    public void testVoid() {
        System.out.println("++++++++++++test void+++++++++++++++");
    }

    @Lio(cmd = "getInt")
    public int testInt() {
        System.out.println("++++++++++++test int+++++++++++++++");
        return 98800;
    }

    @Lio(cmd = "getString")
    public String testString() {
        System.out.println("++++++++++++test string+++++++++++++++");
        return "dddddddddfvvvv";
    }

    @Lio(cmd = "compute_add")
    public int add(int a, int b) {
        System.out.println("++++++++++++test add+++++++++++++++");
        return a+b;
    }

}

客户端

@LsAutowired(app = "com.lehuihome", path = "com.lehuihome", tt=TransportType.SOCKET, ip="127.0.0.1", port=9001, pt=ProtocolType.MESSAGE_PACK)
public interface ClientService {

    @Lio(cmd = "getUser")
    @Pack("com.zhucode.longio.example.message.UserMsg")
    public Map<String, Map<String, UserMsg>> getUser(@Key("user_id")int userId);

    @Lio(cmd = "getVoid")
    public void testVoid();

    @Lio(cmd = "getInt")
    public int testInt();

    @Lio(cmd = "getString")
    public String testString();
    
    @Lio(cmd = "add")
    public int add(int a, int b);
    
    @Lio(cmd = "add")
    public void asynAdd(int a, int b, MessageCallback callback);


}

负载均衡

暂时支持权重轮询和权重随机

@LsAutowired(app = "com.lehuihome", lb=LoadBalance.Roll, ...)

源码地址

https://github.com/zhujinxian/longio

    原文作者:quantzhu
    原文地址: https://segmentfault.com/a/1190000004480453
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞