Code Monkey home page Code Monkey logo

netty-rpc-basic's Introduction

设计**

基于Dubbo+Zookeeper的设计理念,实现一个简单版本的RPC框架

NettyRpc-design

使用技术

基于Netty4.x版本实现RPC框架,主要包含的功能点

  • RPC通信
  • 负载均衡
  • 反射
  • Spring 扩展

本案例为基础版本,仅基于Netty实现了远程方法调用,进阶版本中提供了注解驱动、服务注册与发现等丰富的功能,详见:Netty进阶版本.

为什么要写?

平时我们在使用Dubbo或者一些其他的RPC框架时,并没有关心RPC框架底层的实现,仅仅停留在使用层面。 但是作为一个Java高级开发,必须要对底层实现有一定的了解,所以基于Netty实现了一个自己的RPC框架。 希望能够帮助大家更好的理解Netty以及RPC的原理。

项目结构说明

下面这两个项目,表示服务提供者,用来演示服务提供者和服务消费者通信的一个演示案例。

  1. netty-rpc-api , 对外提供的公共契约

  2. netty-rpc-provider , 服务提供者接口的实现

  3. netty-rpc-consumer, 服务消费者

provider发布服务,consumer通过自定义rpc协议实现和provider的通信。

下面这个项目模块是RPC的实现,netty-rpc-provider和netty-rpc-consumer分别依赖它来实现远程通信

netty-rpc-protocol , Netty实现RPC框架的核心库

使用方法

发布远程服务

这个版本的实现比较原始,是通过new NettyServer手动发布一个Netty远程服务。

@ComponentScan(basePackages = {"com.example.spring","com.example.service"})
@SpringBootApplication
public class NettyRpcProviderMain {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(NettyRpcProviderMain.class, args);
        new NettyServer("127.0.0.1",8080).startNettyServer();
    }

}

消费远程服务

服务消费,是基于RpcClientProxy实现的动态代理,完成远程通信机制。

public class MainTest {
    public static void main(String[] args) {
        RpcClientProxy rpcClientProxy=new RpcClientProxy();
        IUserService userService=rpcClientProxy.clientProxy(IUserService.class,"localhost",8080);
        System.out.println(userService.saveUser("Mic"));
    }
}

实现教程

联系方式

个人作品

  1. 2020年出版《Spring Cloud Alibaba微服务原理与实战》

  1. 2021年出版《Java并发编程深度解析与实战》

netty-rpc-basic's People

Contributors

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