Git Product home page Git Product logo

lcrpc's Introduction

简易版的Dubbo基于Spring,Zookeeper,Netty实现

小手一点,给个Star~

  • 实现了同步调用(支持超时时间配置),异步回调调用(基于Future和CountDownLatch实现)。
@RpcAutowired//实现了类似@Reference的注解,可以直接注入Rpc服务
HelloService helloService;

/**
* 异步调用+callback支持
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void asyncTest() throws ExecutionException, InterruptedException {
  AsyncInvokeFuture future = helloService.asyncHello("World");
  future.addCallBack(new AsyncCallBack() {
    @Override
    public void success(RpcResponse response) {
      System.out.println("执行成功");
    }
    @Override
    public void fail(RpcResponse response) {
      System.out.println("执行失败");
    }
  });
  System.out.println("已发送请求");
  System.out.println(future.get());
}
/**
* 同步调用支持
*/
@Test
public void helloTest2() {
  String world = helloService.hello("World");
  System.out.println(world);
}
  • 实现了类似Dubbo的@Reference注解及@Service注解。主要通过在Spring的Bean加载过程中,使用定义好的代理类对象来替换掉域中已经注入的helloService对象。从而实现代理类对象自动注入。
@RpcAutowired
HelloService helloService;//在生产方只需这样注入服务即可

@RpcService(HelloService.class)//在消费者端只需加入这个注解即可
public class HelloServiceImpl implements HelloService
  • 利用ThreadLocal和TraceId简单实现了RPC框架的链路跟踪
public class RpcContext {
    private static final ThreadLocal<RpcContext> LOCAL = new ThreadLocal<RpcContext>() {
        @Override
        protected RpcContext initialValue() {
            return new RpcContext();
        }
    };

    protected RpcContext() {
    }

    public static RpcContext getContext() {
        return LOCAL.get();
    }

    private String curThreadId;
    private String traceId;
}
  • 实现了基于消费者端的负载均衡。目前使用轮询,后续支持包括:轮询、随机、LRU等;

quckStart

  1. 下载zookeeper并启动在默认端口。
  2. 启动server模块中的RpcBootstrap中的run方法即可
  3. 启动client模块中ServiceTest中的test方法即可测试。

Ref:

https://github.com/xuxueli/xxl-rpc

https://github.com/luxiaoxun/NettyRpc

https://github.com/tang-jie/NettyRPC

lcrpc's People

Contributors

appleliang96 avatar

Stargazers

JesseWang avatar Carol avatar  avatar  avatar  avatar  avatar zhongshunchao avatar 吃瓜网友赵大牛 avatar Roni avatar lxw avatar caixiaomao avatar Yuwei Xie · 谢妤薇 avatar  avatar  avatar  avatar  avatar 游走哈皮 avatar  avatar  avatar zuizeze avatar  avatar liuyulin avatar Heroin X avatar LearnToWorld avatar  avatar Bowen avatar  avatar HuerFu avatar  avatar  avatar

Watchers

James Cloos 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.