1 Star 0 Fork 0

程序员Eric / My-RPC-Framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

My-RPC-Framework

Build Status GitHub jdk

My-RPC-Framework 是一款基于 Nacos 实现的 RPC 框架。网络传输实现了基于 Java 原生 Socket 与 Netty 版本,并且实现了多种序列化与负载均衡算法。

架构

系统架构

消费者调用提供者的方式取决于消费者的客户端选择,如选用原生 Socket 则该步调用使用 BIO,如选用 Netty 方式则该步调用使用 NIO。如该调用有返回值,则提供者向消费者发送返回值的方式同理。

特性

  • 实现了基于 Java 原生 Socket 传输与 Netty 传输两种网络传输方式
  • 实现了四种序列化算法,Json 方式、Kryo 算法、Hessian 算法与 Google Protobuf 方式(默认采用 Kryo方式序列化)
  • 实现了两种负载均衡算法:随机算法与轮转算法
  • 使用 Nacos 作为注册中心,管理服务提供者信息
  • 消费端如采用 Netty 方式,会复用 Channel 避免多次连接
  • 如消费端和提供者都采用 Netty 方式,会采用 Netty 的心跳机制,保证连接
  • 接口抽象良好,模块耦合度低,网络传输、序列化器、负载均衡算法可配置
  • 实现自定义的通信协议
  • 服务提供侧自动注册服务

项目模块概览

  • roc-api —— 通用接口
  • rpc-common —— 实体对象、工具类等公用类
  • rpc-core —— 框架的核心实现
  • test-client —— 测试用消费侧
  • test-server —— 测试用提供侧

传输协议(MRF协议)

调用参数与返回值的传输采用了如下 MRF 协议( My-RPC-Framework 首字母)以防止粘包:

+---------------+---------------+-----------------+-------------+
|  Magic Number |  Package Type | Serializer Type | Data Length |
|    4 bytes    |    4 bytes    |     4 bytes     |   4 bytes   |
+---------------+---------------+-----------------+-------------+
|                          Data Bytes                           |
|                   Length: ${Data Length}                      |
+---------------------------------------------------------------+
字段 解释
Magic Number 魔数,表识一个 MRF 协议包,0xCAFEBABE
Package Type 包类型,标明这是一个调用请求还是调用响应
Serializer Type 序列化器类型,标明这个包的数据的序列化方式
Data Length 数据字节的长度
Data Bytes 传输的对象,通常是一个RpcRequestRpcClient对象,取决于Package Type字段,对象的序列化方式取决于Serializer Type字段。

使用

定义调用接口

package top.guoziyang.rpc.api;

public interface HelloService {
    String hello(String name);
}

在服务提供侧实现该接口

package top.guoziyang.test;

import top.guoziyang.rpc.api.HelloService;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello, " + name;
    }
}

编写服务提供者

package top.guoziyang.test;

import top.guoziyang.rpc.api.HelloService;
import top.guoziyang.rpc.serializer.CommonSerializer;
import top.guoziyang.rpc.transport.netty.server.NettyServer;

@ServiceScan
public class NettyTestServer {
    public static void main(String[] args) {
        NettyServer server = new NettyServer("127.0.0.1", 9999, CommonSerializer.PROTOBUF_SERIALIZER);
        server.start();
    }
}

这里选用 Netty 传输方式,并且指定序列化方式为 Google Protobuf 方式。

在服务消费侧远程调用

package top.guoziyang.test;

import top.guoziyang.rpc.api.HelloService;
import top.guoziyang.rpc.serializer.CommonSerializer;
import top.guoziyang.rpc.transport.RpcClient;
import top.guoziyang.rpc.transport.RpcClientProxy;
import top.guoziyang.rpc.transport.netty.client.NettyClient;

public class NettyTestClient {

    public static void main(String[] args) {
        RpcClient client = new NettyClient(CommonSerializer.KRYO_SERIALIZER, new RoundRobinLoadBalancer());
        RpcClientProxy rpcClientProxy = new RpcClientProxy(client);
        HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
        String res = helloService.hello("ziyang");
        System.out.println(res);
    }
}

这里客户端也选用了 Netty 的传输方式,序列化方式采用 Kryo 方式,负载均衡策略指定为轮转方式。

启动

在此之前请确保 Nacos 运行在本地 8848 端口。

首先启动服务提供者,再启动消费者,在消费侧会输出Hello, ziyang

TODO

  • 配置文件

LICENSE

My-RPC-Framework is under the MIT license. See the LICENSE file for details.

MIT License Copyright (c) 2020 CN-GuoZiyang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/cecurio/My-RPC-Framework.git
git@gitee.com:cecurio/My-RPC-Framework.git
cecurio
My-RPC-Framework
My-RPC-Framework
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891