GithubHelp home page GithubHelp logo

srpc's Introduction

使用Netty+Nacos+Protostuff制作RPC框架

简介

实现的功能

这个RPC实现了一些基本的功能:

  • 使用Netty来进行网络传输,效率比起传统的NIO要高很多。
  • 使用Nacos作为服务的注册中心,用于管理注册的服务,当客户端请求发过来时,Nacos会寻找合适的服务返回给客户端消费。
  • 实现了负载均衡的功能,,客户端对于Nacos返回的服务列表,会使用负载均衡算法,选择一个自己需要的服务加入,目前实现了轮询算法和随机选取算法。
  • 加入了心跳检测机制,并不会发送完消息立即结束,而是保持的长连接,提高效率,并且也可以结合Nacos做心跳检测。
  • 使用Protostuff作为对象的的序列化工具,效率更高,实现Netty中的编/解码的功能,提高了效率。
  • 实现了钩子函数,当服务端下线的时候会自动去Nacos注销服务。
  • 利用了并发编程的知识,使用CompletableFuture来接受客户端返回的结果。
  • 使用了设计模式,例如使用单例模式去保持一个Channel,使得代码更加简洁。



详细的描述写在了我的博客
https://pandalee99.github.io/2022/11/02/srpc/
下面是测试环节:

测试

由于使用Nacos,调试比较简单:
下载好Nacos,无论是win版还是linux版,在官网都有,比较方便;
但是由于Nacos一般都要配置数据库,为了方便测试,可以使用命令先进行单机运行

startup.cmd -m standalone

客户端:

public class NettyTestClient {
    public static void main(String[] args) {
        RpcClient client = new NettyClient(CommonSerializer.PROTOBUF_SERIALIZER);
        RpcClientProxy rpcClientProxy = new RpcClientProxy(client);
        HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
        HelloObject object = new HelloObject(114514, "Client send a Message");
        String res = helloService.hello(object);
        System.out.println(res);
    }
}

服务端:

@ServiceScan
public class NettyTestServer {

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

}

之后会有一个测试结果:
客户端收到信息
image.png
服务端收到信息
image.png

srpc's People

Contributors

pandalee99 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.