GithubHelp home page GithubHelp logo

nety-rpc's Introduction

netty-rpc

Maven Central JDK SpringBoot Netty Protostuff

一个简单的RPC实现,基于Spring, Zookeeper, Netty

正在开发

  • 异常处理 √
  • AOP埋点监控 √
  • 集成断路器
  • 支持跨平台
  • 提供API文档支持,Restful调用
  • 更高的性能...

简易使用

  1. 创建一个RPC接口
public interface MathService {
    public int max(int a, int b);
}
  1. 实现该接口并加上@RpcService注解
@RpcService(MathService.class)
public class MathServiceImpl implements MathService {

    @Override
    public int max(int a, int b) {
        return Math.max(a, b);
    }
}
  1. 运行zookeeper
1. 官网下载最新的releases版本(源码版本是无法运行的) https://zookeeper.apache.org/releases.html
2. 运行zoopeeker,比如zookeeper默认运行在127.0.0.1:2181
  1. 启动服务
1.rpc-config.properties中修改rpc服务地址和zoopeeker地址
2. 启动ServerApplication即可
  1. 使用客户端
ServerDiscovery discovery = new ServerDiscovery("127.0.0.1:2181");
rpcClient rpcClient = new RpcClient(serviceDiscovery);

MathService mathService = rpcClient.create(MathService.class);
System.out.println("最大值:" + mathService.max(233, 19));

rpcClient.stop();

服务监控

引入了AOP数据埋点之后,可以记录客户发起的RPC请求,包括成功和失败的,通过服务监控类"com.zihua.rpc.aop.RpcMonitor"记录客户端对服务器发起的调用。

比如我用如下代码连续请求10次:

public static void main(String[] args) {
    ServiceDiscovery serviceDiscovery = new ServiceDiscovery("127.0.0.1:2181");
    RpcClient rpcClient = new RpcClient(serviceDiscovery);
    
    MathService mathService = rpcClient.create(MathService.class);
    for (int i = 1; i <= 10; i++) {
        System.out.println("最大值:" + mathService.max(233, 19));            
    }
    rpcClient.stop();
}

然后查看服务端日志:

image.png

如果我发起了错误的调用,那么也会记录,比如我将请求的包名改成错误的:

@MarkClassName(className = "com.zihua.rpc.demo.MathService123")
public interface MathService {
    public int max(int a, int b);
}

然后服务端会记录如下日志:

image.png

第一行的INFO包含出错的Throwable,第二行就是ERROR了。

可以根据实际场景把日志改成xml或者文本,

架构设计

服务端

image.png

客户端

image.png

注意

  1. 发起调用的接口须与服务端路径一致
比如服务端实例接口路径是"com.zihua.rpc.demo.MathService"
那么客户端就在"com.zihua.rpc.demo"下创建MathService
  1. 如果路径不一致的情况下使用@MarkClassName设置包路径
比如客户端类的路径是"com.zihua.rpc.MathService"
服务端的路径是"com.zihua.rpc.demo.MathService"
加上@MarkClassName注解后可以手动更正
@MarkClassName(className = "com.zihua.rpc.demo.MathService")
public interface MathService {
    public int max(int a, int b);
}
ServerDiscovery discovery = new ServerDiscovery("127.0.0.1:2181");
rpcClient rpcClient = new RpcClient(serviceDiscovery);

MathService mathService = rpcClient.create(MathService.class);
System.out.println("最大值:" + mathService.max(233, 19));

rpcClient.stop();

nety-rpc's People

Contributors

liuzihua699 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  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.