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

quantzhu發表於2016-02-23

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

相關文章