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

LongIO是一个支持同步和异步调用的RPC框架,具备客户端负载均衡和服务发现功能。它基于Netty,支持WebSocket、HTTP等多种传输层协议,并提供JSON、MessagePack等多种序列化方式。该框架还支持服务端推送消息及客户端路由功能。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值